-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#7) - implementação do controller e do service
- Loading branch information
1 parent
5c9bac4
commit 210ac25
Showing
6 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/ayty/hatcher/api/v1/competence/controller/CompetenceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.ayty.hatcher.api.v1.competence.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.ayty.hatcher.api.v1.competence.dto.CompetenceDTO; | ||
import org.ayty.hatcher.api.v1.competence.model.Competence; | ||
import org.ayty.hatcher.api.v1.competence.service.CompetenceService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/competence") | ||
public class CompetenceController { | ||
|
||
@Autowired | ||
CompetenceService service; | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping | ||
public List<Competence> getAll() { | ||
return service.getAll(); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("/{id}") | ||
public Competence getById(@PathVariable Integer id) { | ||
return service.getById(id); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping | ||
public Competence save(@RequestBody CompetenceDTO competenceDto) { | ||
return service.save(competenceDto); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@PutMapping("/{id}") | ||
public Competence edit(@PathVariable Integer id, @RequestBody CompetenceDTO competenceDto) { | ||
return service.edit(id, competenceDto); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Integer id ) { | ||
service.delete(id); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/ayty/hatcher/api/v1/competence/dto/CompetenceDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.ayty.hatcher.api.v1.competence.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CompetenceDTO { | ||
|
||
private String name; | ||
private String description; | ||
private String type; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/ayty/hatcher/api/v1/competence/service/CompetenceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.ayty.hatcher.api.v1.competence.service; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import org.ayty.hatcher.api.v1.competence.dto.CompetenceDTO; | ||
import org.ayty.hatcher.api.v1.competence.jpa.CompetenceRepository; | ||
import org.ayty.hatcher.api.v1.competence.model.Competence; | ||
import org.ayty.hatcher.api.v1.competence.model.Type; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CompetenceService { | ||
|
||
@Autowired | ||
CompetenceRepository repository; | ||
|
||
public List<Competence> getAll() { | ||
return repository.findAll(); | ||
} | ||
|
||
public Competence save(CompetenceDTO competenceDto) { | ||
Competence competence = Competence | ||
.builder() | ||
.name(competenceDto.getName()) | ||
.description(competenceDto.getDescription()) | ||
.type(Type.valueOf(competenceDto.getType().toUpperCase())) | ||
.build(); | ||
|
||
return repository.save(competence); | ||
} | ||
|
||
public void delete(Integer id) { | ||
repository.deleteById(id); | ||
} | ||
|
||
public Competence getById(Integer id) { | ||
|
||
Competence competence = repository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("Competência não encontrada")); | ||
|
||
return competence; | ||
} | ||
|
||
public Competence edit(Integer id, CompetenceDTO competenceDto) { | ||
|
||
Competence competence = (Competence) repository.findById(id) | ||
.map(c -> { | ||
c.setDescription(competenceDto.getDescription()); | ||
c.setName(competenceDto.getName()); | ||
c.setType(Type.valueOf(competenceDto.getType().toUpperCase())); | ||
return c; | ||
}).orElseThrow(() -> new NoSuchElementException("Competência não encontrada")); | ||
|
||
return repository.save(competence); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,3 @@ security: | |
token: | ||
secret-key: 8xATUx4r2L52 | ||
expire-length: 604800000 #1 week | ||
|