Repository navigation
[PETCONTROL-001] Adicionar a criação de pets #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b94ccaf
[PETCONTROL-001] feat: adicionar tabelas pets e pet_tags
gabicastrum c5f894d
[PETCONTROL-001] feat: implementar a criação dos pets
gabicastrum 6e9382b
[PETCONTROL-001] test: adicionar testes unitários e integração para f…
gabicastrum acfd638
[PETCONTROL-001] refactor: implementada validações e ajustes nos testes
gabicastrum 79710ba
[PETCONTROL-001] refactor: realizado ajustes da implementação
gabicastrum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/br/com/db/petcontrol/controller/PetsController.java
This file contains hidden or 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,31 @@ | ||
| package br.com.db.petcontrol.controller; | ||
|
|
||
| import br.com.db.petcontrol.dto.request.PetRequestDTO; | ||
| import br.com.db.petcontrol.dto.response.PetResponseDTO; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
|
|
||
| @Tag(name = "Pets", description = "Operações para gerenciamento de pets") | ||
| @RequestMapping("/pets") | ||
| public interface PetsController { | ||
|
|
||
| @Operation(summary = "Cadastrar pet", description = "Cadastra um novo pet no sistema") | ||
| @ApiResponse( | ||
| responseCode = "201", | ||
| description = "Pet cadastrado com sucesso", | ||
| content = | ||
| @Content( | ||
| mediaType = "application/json", | ||
| schema = @Schema(implementation = PetResponseDTO.class))) | ||
| @ApiResponse(responseCode = "404", description = "Espécie não encontrada") | ||
| @PostMapping | ||
| ResponseEntity<PetResponseDTO> create(@Valid @RequestBody PetRequestDTO dto); | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/br/com/db/petcontrol/controller/impl/PetsControllerImpl.java
This file contains hidden or 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,23 @@ | ||
| package br.com.db.petcontrol.controller.impl; | ||
|
|
||
| import br.com.db.petcontrol.controller.PetsController; | ||
| import br.com.db.petcontrol.dto.request.PetRequestDTO; | ||
| import br.com.db.petcontrol.dto.response.PetResponseDTO; | ||
| import br.com.db.petcontrol.service.PetsService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class PetsControllerImpl implements PetsController { | ||
|
|
||
| private final PetsService petsService; | ||
|
|
||
| @Override | ||
| public ResponseEntity<PetResponseDTO> create(PetRequestDTO dto) { | ||
| PetResponseDTO response = petsService.create(dto); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(response); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/br/com/db/petcontrol/dto/request/PetRequestDTO.java
This file contains hidden or 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,22 @@ | ||
| package br.com.db.petcontrol.dto.request; | ||
|
|
||
| import br.com.db.petcontrol.enums.PetStatus; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record PetRequestDTO( | ||
| @NotBlank @Schema(description = "Nome do pet", example = "Lily") String name, | ||
| @NotNull | ||
| @Schema(description = "ID da espécie", example = "dd3af030-9823-40ff-bc6e-4de9eae55894") | ||
| UUID specieId, | ||
| @NotNull @Schema(description = "Status do pet", example = "AVAILABLE") PetStatus status, | ||
| @Schema( | ||
| description = "IDs das tags do pet", | ||
| example = | ||
| "[\"6231f3b7-e6c3-4e93-8890-4fc7dbc06101\", \"171402a5-71fd-42db-a680-df25187ac0c0\"]") | ||
| List<UUID> tagsIds) {} |
19 changes: 19 additions & 0 deletions
19
src/main/java/br/com/db/petcontrol/dto/response/PetResponseDTO.java
This file contains hidden or 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,19 @@ | ||
| package br.com.db.petcontrol.dto.response; | ||
|
|
||
| import br.com.db.petcontrol.enums.PetStatus; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record PetResponseDTO( | ||
| @Schema( | ||
| description = "Identificador único do pet", | ||
| example = "d6acb364-305e-4883-964f-0e7aee360684") | ||
| UUID id, | ||
| @Schema(description = "Nome do pet", example = "Lily") String name, | ||
| @Schema(description = "Status do pet", example = "AVAILABLE") PetStatus status, | ||
| @Schema(description = "Espécie do pet", example = "Gato") String species, | ||
| @Schema(description = "Tags do pet", example = "[\"castrado\", \"vacinado\"]") | ||
| List<String> tags) {} |
This file contains hidden or 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,7 @@ | ||
| package br.com.db.petcontrol.enums; | ||
|
|
||
| public enum PetStatus { | ||
| AVAILABLE, | ||
| IN_ANALYSIS, | ||
| ADOPTED | ||
| } |
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
src/main/java/br/com/db/petcontrol/exception/NotFoundException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| package br.com.db.petcontrol.exception; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| private final List<String> messages; | ||
|
|
||
| public NotFoundException(String message) { | ||
| super(message); | ||
| this.messages = List.of(message); | ||
| } | ||
|
|
||
| public NotFoundException(List<String> messages) { | ||
| super(messages.isEmpty() ? "Not found" : messages.get(0)); | ||
| this.messages = messages; | ||
| } | ||
|
|
||
| public List<String> getMessages() { | ||
| return messages; | ||
| } | ||
| } |
This file contains hidden or 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,30 @@ | ||
| package br.com.db.petcontrol.mapper; | ||
|
|
||
| import br.com.db.petcontrol.dto.request.PetRequestDTO; | ||
| import br.com.db.petcontrol.dto.response.PetResponseDTO; | ||
| import br.com.db.petcontrol.model.Pets; | ||
| import br.com.db.petcontrol.model.Species; | ||
| import br.com.db.petcontrol.model.Tags; | ||
| import java.util.List; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| public interface PetsMapper { | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| @Mapping(target = "name", source = "dto.name") | ||
| @Mapping(target = "status", source = "dto.status") | ||
| Pets toEntity(PetRequestDTO dto, Species species, List<Tags> tags); | ||
|
|
||
| @Mapping(target = "species", source = "species.name") | ||
| @Mapping(target = "tags", source = "tags") | ||
| PetResponseDTO toResponse(Pets pet); | ||
|
|
||
| default List<String> map(List<Tags> tags) { | ||
| if (tags == null) { | ||
| return null; | ||
| } | ||
| return tags.stream().map(Tags::getName).toList(); | ||
| } | ||
| } | ||
This file contains hidden or 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,53 @@ | ||
| package br.com.db.petcontrol.model; | ||
|
|
||
| import br.com.db.petcontrol.enums.PetStatus; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.JoinTable; | ||
| import jakarta.persistence.ManyToMany; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Table(name = "tb_pets") | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class Pets { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| private UUID id; | ||
|
|
||
| @Column(nullable = false) | ||
| private String name; | ||
|
|
||
| @Column(name = "status", nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| private PetStatus status; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "species_id", nullable = false) | ||
| private Species species; | ||
|
|
||
| @ManyToMany(fetch = FetchType.LAZY) | ||
| @JoinTable( | ||
| name = "tb_pet_tags", | ||
| joinColumns = @JoinColumn(name = "pet_id"), | ||
| inverseJoinColumns = @JoinColumn(name = "tag_id")) | ||
| private List<Tags> tags; | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/br/com/db/petcontrol/repository/PetsRepository.java
This file contains hidden or 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,9 @@ | ||
| package br.com.db.petcontrol.repository; | ||
|
|
||
| import br.com.db.petcontrol.model.Pets; | ||
| import java.util.UUID; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface PetsRepository extends JpaRepository<Pets, UUID> {} |
This file contains hidden or 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,8 @@ | ||
| package br.com.db.petcontrol.service; | ||
|
|
||
| import br.com.db.petcontrol.dto.request.PetRequestDTO; | ||
| import br.com.db.petcontrol.dto.response.PetResponseDTO; | ||
|
|
||
| public interface PetsService { | ||
| PetResponseDTO create(PetRequestDTO dto); | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/main/java/br/com/db/petcontrol/service/impl/PetsServiceImpl.java
This file contains hidden or 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,66 @@ | ||
| package br.com.db.petcontrol.service.impl; | ||
|
|
||
| import br.com.db.petcontrol.dto.request.PetRequestDTO; | ||
| import br.com.db.petcontrol.dto.response.PetResponseDTO; | ||
| import br.com.db.petcontrol.exception.NotFoundException; | ||
| import br.com.db.petcontrol.mapper.PetsMapper; | ||
| import br.com.db.petcontrol.model.Pets; | ||
| import br.com.db.petcontrol.model.Species; | ||
| import br.com.db.petcontrol.model.Tags; | ||
| import br.com.db.petcontrol.repository.PetsRepository; | ||
| import br.com.db.petcontrol.repository.SpeciesRepository; | ||
| import br.com.db.petcontrol.repository.TagsRepository; | ||
| import br.com.db.petcontrol.service.PetsService; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PetsServiceImpl implements PetsService { | ||
|
|
||
| private final PetsRepository petRepository; | ||
| private final SpeciesRepository speciesRepository; | ||
| private final TagsRepository tagRepository; | ||
| private final PetsMapper petsMapper; | ||
|
|
||
| public PetResponseDTO create(PetRequestDTO dto) { | ||
| List<String> errors = new ArrayList<>(); | ||
|
|
||
| Species species = speciesRepository.findById(dto.specieId()).orElse(null); | ||
| if (species == null) { | ||
| errors.add("Species not found: " + dto.specieId()); | ||
| } | ||
|
|
||
| List<Tags> tags = findTagsOrCollectErrors(dto.tagsIds(), errors); | ||
|
|
||
| if (!errors.isEmpty()) { | ||
| throw new NotFoundException(errors); | ||
| } | ||
|
|
||
| Pets pet = petsMapper.toEntity(dto, species, tags); | ||
| Pets savedPet = petRepository.saveAndFlush(pet); | ||
| return petsMapper.toResponse(savedPet); | ||
| } | ||
|
|
||
| private List<Tags> findTagsOrCollectErrors(List<UUID> ids, List<String> errors) { | ||
| if (ids == null || ids.isEmpty()) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| List<Tags> foundTags = tagRepository.findAllById(ids); | ||
|
|
||
| if (foundTags.size() != ids.size()) { | ||
| List<UUID> notFoundIds = | ||
| ids.stream() | ||
| .filter(id -> foundTags.stream().noneMatch(tag -> tag.getId().equals(id))) | ||
| .toList(); | ||
|
|
||
| errors.add("Tags not found: " + notFoundIds); | ||
| } | ||
|
|
||
| return foundTags; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/resources/db/migrations/V20260427110455__create_table_pets.sql
This file contains hidden or 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,7 @@ | ||
| CREATE TABLE IF NOT EXISTS tb_pets ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| name VARCHAR(100) NOT NULL, | ||
| status VARCHAR(50) NOT NULL, | ||
| species_id UUID NOT NULL, | ||
| CONSTRAINT fk_pet_species FOREIGN KEY (species_id) REFERENCES tb_species(id) | ||
| ); |
7 changes: 7 additions & 0 deletions
7
src/main/resources/db/migrations/V20260427110525__create_table_pet_tags.sql
This file contains hidden or 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,7 @@ | ||
| CREATE TABLE IF NOT EXISTS tb_pet_tags ( | ||
| pet_id UUID NOT NULL, | ||
| tag_id UUID NOT NULL, | ||
| PRIMARY KEY (pet_id, tag_id), | ||
| CONSTRAINT fk_pet_id FOREIGN KEY (pet_id) REFERENCES tb_pets(id), | ||
| CONSTRAINT fk_tag_id FOREIGN KEY (tag_id) REFERENCES tb_tags(id) | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.