diff --git a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java index 57ea5401..370417fc 100644 --- a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java +++ b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java @@ -2,12 +2,10 @@ import java.util.List; import lombok.RequiredArgsConstructor; -import mate.academy.rickandmorty.dto.CharacterDto; import mate.academy.rickandmorty.dto.ResponseDto; import mate.academy.rickandmorty.service.CharacterService; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -24,7 +22,7 @@ public List getCharactersByName(@RequestParam String name, Pageable } @GetMapping("/random") - public CharacterDto getRandomCharacter(@PathVariable Long id) { + public ResponseDto getRandomCharacter() { return service.getRandomCharacter(); } } diff --git a/src/main/java/mate/academy/rickandmorty/dto/ExternalCharacterDto.java b/src/main/java/mate/academy/rickandmorty/dto/ExternalCharacterDto.java index f243cdf9..30a1959e 100644 --- a/src/main/java/mate/academy/rickandmorty/dto/ExternalCharacterDto.java +++ b/src/main/java/mate/academy/rickandmorty/dto/ExternalCharacterDto.java @@ -4,23 +4,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; -import java.util.List; - -@JsonIgnoreProperties(ignoreUnknown = true) @Data +@JsonIgnoreProperties(ignoreUnknown = true) public class ExternalCharacterDto { private Long id; private String name; private String status; - private String species; - private String type; private String gender; - private List origin; - private List location; - private String image; - private List episode; - private String url; - private String created; @JsonProperty("id") public Long getExternalId() { diff --git a/src/main/java/mate/academy/rickandmorty/dto/ResponseDto.java b/src/main/java/mate/academy/rickandmorty/dto/ResponseDto.java index fed74569..f2b6b159 100644 --- a/src/main/java/mate/academy/rickandmorty/dto/ResponseDto.java +++ b/src/main/java/mate/academy/rickandmorty/dto/ResponseDto.java @@ -1,9 +1,14 @@ package mate.academy.rickandmorty.dto; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.Data; + +@Data +@JsonPropertyOrder({"id", "externalId", "name", "status", "gender"}) public class ResponseDto { private Long id; private String externalId; private String name; private String status; private String gender; -} \ No newline at end of file +} diff --git a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java index e2b045bd..0ca8b315 100644 --- a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java +++ b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java @@ -7,16 +7,23 @@ import mate.academy.rickandmorty.model.Character; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.Mappings; import org.springframework.stereotype.Component; @Mapper(config = MapperConfig.class) @Component public interface CharacterMapper { - public CharacterDto toDto(Character character); + CharacterDto toCharacterDto(Character character); - @Mapping(target = "externalId", source = "id") - @Mapping(target = "id", ignore = true) + @Mappings({ + @Mapping(target = "externalId", source = "id"), + @Mapping(target = "id", ignore = true) // DB auto-generated ID + }) Character toEntity(ExternalCharacterDto dto); + @Mappings({ + @Mapping(target = "externalId", source = "externalId"), + @Mapping(target = "id", source = "id") + }) ResponseDto toResponseDto(Character character); } diff --git a/src/main/java/mate/academy/rickandmorty/model/Character.java b/src/main/java/mate/academy/rickandmorty/model/Character.java index 23a9a58e..61d19411 100644 --- a/src/main/java/mate/academy/rickandmorty/model/Character.java +++ b/src/main/java/mate/academy/rickandmorty/model/Character.java @@ -8,8 +8,8 @@ import lombok.Data; @Entity -@Data @Table(name = "characters") +@Data public class Character { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java b/src/main/java/mate/academy/rickandmorty/service/CharacterService.java index af203af6..1c37eeae 100644 --- a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java +++ b/src/main/java/mate/academy/rickandmorty/service/CharacterService.java @@ -1,12 +1,11 @@ package mate.academy.rickandmorty.service; import java.util.List; -import mate.academy.rickandmorty.dto.CharacterDto; import mate.academy.rickandmorty.dto.ResponseDto; import org.springframework.data.domain.Pageable; public interface CharacterService { List getCharactersByName(String name, Pageable pageable); - CharacterDto getRandomCharacter(); + ResponseDto getRandomCharacter(); } diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java index e2060cc5..85abd987 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.Random; import lombok.RequiredArgsConstructor; -import mate.academy.rickandmorty.dto.CharacterDto; import mate.academy.rickandmorty.dto.ResponseDto; import mate.academy.rickandmorty.exception.EntityNotFoundException; import mate.academy.rickandmorty.mapper.CharacterMapper; @@ -29,8 +28,8 @@ public List getCharactersByName(String name, Pageable pageable) { } @Override - public CharacterDto getRandomCharacter() { - return mapper.toDto(repository.findById(random.nextLong(countCharactersInDb())) + public ResponseDto getRandomCharacter() { + return mapper.toResponseDto(repository.findById(random.nextLong(countCharactersInDb())) .orElseThrow(() -> new EntityNotFoundException( "Failed to get random Character from DB"))); }