-
Notifications
You must be signed in to change notification settings - Fork 240
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
task done, to review #19
Changes from 1 commit
ceb3a62
af38232
f567549
859c68f
5f81de9
fa33641
ce14b83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@org.mapstruct.MapperConfig( | ||
componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl" | ||
) | ||
public class MapperConfig { | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,53 @@ | ||||||
package mate.academy.rickandmorty.controller; | ||||||
|
||||||
import io.swagger.v3.oas.annotations.Operation; | ||||||
import io.swagger.v3.oas.annotations.tags.Tag; | ||||||
import java.util.List; | ||||||
import lombok.RequiredArgsConstructor; | ||||||
import mate.academy.rickandmorty.dto.external.ExternalCharResponseDto; | ||||||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||||||
import mate.academy.rickandmorty.model.Character; | ||||||
import mate.academy.rickandmorty.service.CharacterService; | ||||||
import mate.academy.rickandmorty.service.RickAndMortyClient; | ||||||
import org.springframework.web.bind.annotation.GetMapping; | ||||||
import org.springframework.web.bind.annotation.RequestMapping; | ||||||
import org.springframework.web.bind.annotation.RequestParam; | ||||||
import org.springframework.web.bind.annotation.RestController; | ||||||
|
||||||
@Tag(name = "Character Management", description = "endpoints to manage client character data base") | ||||||
@RestController | ||||||
@RequestMapping("/characters") | ||||||
@RequiredArgsConstructor | ||||||
public class CharacterController { | ||||||
private final RickAndMortyClient client; | ||||||
private final CharacterService service; | ||||||
private final CharacterMapper mapper; | ||||||
|
||||||
@Operation(summary = "show random character", | ||||||
description = "show and save random character from client db") | ||||||
@GetMapping("/random") | ||||||
public ExternalCharResponseDto getRandomCharacter() { | ||||||
return client.getRandomCharacter(); | ||||||
} | ||||||
|
||||||
@Operation(summary = "saving all characters from client to db", | ||||||
description = "save all char info from client db to yours," | ||||||
+ " only to use once for the first time with empty table") | ||||||
@GetMapping("/save") | ||||||
public void save() { | ||||||
service.save(); | ||||||
} | ||||||
|
||||||
@Operation(summary = "show character by search name parameter", | ||||||
description = "show character by string occurrence in his/her/its name") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
@GetMapping | ||||||
public List<ExternalCharResponseDto> getCharactersBySearchString( | ||||||
@RequestParam String searchString | ||||||
) { | ||||||
List<Character> charactersBySearchString = | ||||||
service.getCharactersBySearchString(searchString); | ||||||
return charactersBySearchString.stream() | ||||||
.map(mapper::toDto) | ||||||
.toList(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public record ExternalCharResponseDto(Long id, | ||
Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
public record InfoDto(Long count, | ||
Long pages) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class InternalCharListDto { | ||
@JsonProperty("results") | ||
private List<InternalCharResponseDto> results; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
public record InternalCharResponseDto(Long id, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.ExternalCharResponseDto; | ||
import mate.academy.rickandmorty.dto.internal.InternalCharResponseDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
default Character toEntity(InternalCharResponseDto responseDto) { | ||
Character character = new Character(); | ||
character.setExternalId(responseDto.id()); | ||
character.setName(responseDto.name()); | ||
character.setStatus(responseDto.status()); | ||
character.setGender(responseDto.gender()); | ||
return character; | ||
} | ||
|
||
default ExternalCharResponseDto toDto(Character character) { | ||
return new ExternalCharResponseDto( | ||
character.getId(), | ||
character.getExternalId(), | ||
character.getName(), | ||
character.getStatus(), | ||
character.getGender() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Table(name = "characters") | ||
@Data | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@JsonProperty("id") | ||
private Long externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
@Query(value = "SELECT * FROM characters c WHERE c.external_id = :id", nativeQuery = true) | ||
Character findByExternalId(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
|
||
public interface CharacterService { | ||
void save(); | ||
|
||
List<Character> getCharactersBySearchString(String searchString); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Random; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.ExternalCharResponseDto; | ||
import mate.academy.rickandmorty.dto.internal.InternalCharListDto; | ||
import mate.academy.rickandmorty.dto.internal.InternalCharResponseDto; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RickAndMortyClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All data from the public API should be fetched once, and only once, when the Application context is created |
||
private static final String GET_CHAR_BASE_URL = "https://rickandmortyapi.com/api/character/%s"; | ||
private static final String GET_ALL_BASE_URL = "https://rickandmortyapi.com/api/character"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save the API in properties instead of constants
|
||
private final CharacterMapper characterMapper; | ||
private final ObjectMapper objectMapper; | ||
private final CharacterRepository characterRepository; | ||
|
||
public ExternalCharResponseDto getRandomCharacter() { | ||
Random random = new Random(); | ||
int maxCharAmount = 826; | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
String url = GET_CHAR_BASE_URL.formatted(String.valueOf(random.nextInt(maxCharAmount))); | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(url)) | ||
.build(); | ||
try { | ||
HttpResponse<String> response = httpClient | ||
.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
InternalCharResponseDto internalCharResponseDto = objectMapper.readValue( | ||
response.body(), InternalCharResponseDto.class | ||
); | ||
if (!characterRepository.findAll().isEmpty()) { | ||
for (Character c : characterRepository.findAll()) { | ||
if (c.getExternalId().equals(internalCharResponseDto.id())) { | ||
return characterMapper.toDto(c); | ||
} | ||
} | ||
} | ||
Character saved = characterRepository | ||
.save(characterMapper.toEntity(internalCharResponseDto)); | ||
return characterMapper.toDto(saved); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public InternalCharListDto getAllCharacters(Integer page) { | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
String url = UriComponentsBuilder | ||
.fromHttpUrl(GET_ALL_BASE_URL) | ||
.queryParam("page", page) | ||
.toUriString(); | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(url)) | ||
.build(); | ||
try { | ||
HttpResponse<String> response = httpClient | ||
.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
return objectMapper.readValue( | ||
response.body(), | ||
InternalCharListDto.class | ||
); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyone who finds this endpoint may spam external api on your behalf. Please do not leave such obvious vulnerabilities in code
use
@PostConstruct
in client service instead of endpoint/save