Skip to content

Commit

Permalink
implemented solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YevheniiVlasenko committed May 6, 2024
1 parent a4d95e6 commit ba7f612
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +22,7 @@ public List<ResponseDto> getCharactersByName(@RequestParam String name, Pageable
}

@GetMapping("/random")
public CharacterDto getRandomCharacter(@PathVariable Long id) {
public ResponseDto getRandomCharacter() {
return service.getRandomCharacter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> origin;
private List<String> location;
private String image;
private List<String> episode;
private String url;
private String created;

@JsonProperty("id")
public Long getExternalId() {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/mate/academy/rickandmorty/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.Data;

@Entity
@Data
@Table(name = "characters")
@Data
public class Character {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResponseDto> getCharactersByName(String name, Pageable pageable);

CharacterDto getRandomCharacter();
ResponseDto getRandomCharacter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +28,8 @@ public List<ResponseDto> 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")));
}
Expand Down

0 comments on commit ba7f612

Please sign in to comment.