-
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
Did the task -Rick and Morty- #146
Changes from 1 commit
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,5 @@ | ||
package mate.academy.rickandmorty.client; | ||
|
||
public interface CharacterClient { | ||
void getAndSaveCharacters(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,60 @@ | ||||||
package mate.academy.rickandmorty.client.impl; | ||||||
|
||||||
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.ArrayList; | ||||||
import java.util.List; | ||||||
import lombok.RequiredArgsConstructor; | ||||||
import mate.academy.rickandmorty.client.CharacterClient; | ||||||
import mate.academy.rickandmorty.dto.external.CharacterResponseDto; | ||||||
import mate.academy.rickandmorty.dto.external.ExternalCharacterDto; | ||||||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||||||
import mate.academy.rickandmorty.model.Character; | ||||||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||||||
import org.springframework.stereotype.Component; | ||||||
|
||||||
@RequiredArgsConstructor | ||||||
@Component | ||||||
public class CharacterClientImpl implements CharacterClient { | ||||||
private static final String API_URL = "https://rickandmortyapi.com/api/character"; | ||||||
private final CharacterRepository characterRepository; | ||||||
private final CharacterMapper characterMapper; | ||||||
private final ObjectMapper objectMapper; | ||||||
|
||||||
@Override | ||||||
public void getAndSaveCharacters() { | ||||||
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. |
||||||
HttpClient httpClient = HttpClient.newHttpClient(); | ||||||
String url = API_URL; | ||||||
List<ExternalCharacterDto> externalCharacterDtoList = new ArrayList<>(); | ||||||
|
||||||
do { | ||||||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||||||
.GET() | ||||||
.uri(URI.create(url)) | ||||||
.build(); | ||||||
|
||||||
try { | ||||||
HttpResponse<String> response = httpClient.send(httpRequest, | ||||||
HttpResponse.BodyHandlers.ofString()); | ||||||
CharacterResponseDto dataDto = objectMapper | ||||||
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
|
||||||
.readValue(response.body(), CharacterResponseDto.class); | ||||||
|
||||||
url = dataDto.getInfo().getNextUrl(); | ||||||
externalCharacterDtoList.addAll(dataDto.getResult().stream().toList()); | ||||||
|
||||||
} catch (IOException | InterruptedException e) { | ||||||
throw new RuntimeException(e); | ||||||
} | ||||||
} while (url != null); | ||||||
|
||||||
Iterable<Character> characters = externalCharacterDtoList.stream() | ||||||
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
|
||||||
.map(characterMapper::toModel) | ||||||
.toList(); | ||||||
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. Mapstruct supports mapping collections from the box |
||||||
|
||||||
characterRepository.saveAll(characters); | ||||||
} | ||||||
} |
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,32 @@ | ||||||
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.internal.InternalCharacterDto; | ||||||
import mate.academy.rickandmorty.service.CharacterService; | ||||||
import org.springframework.web.bind.annotation.GetMapping; | ||||||
import org.springframework.web.bind.annotation.RequestMapping; | ||||||
import org.springframework.web.bind.annotation.RestController; | ||||||
|
||||||
@Tag(name = "Character management", description = "Endpoints for managing products") | ||||||
@RequiredArgsConstructor | ||||||
@RestController | ||||||
@RequestMapping(value = "/character") | ||||||
public class CharacterController { | ||||||
private final CharacterService characterService; | ||||||
|
||||||
@GetMapping("/random") | ||||||
@Operation(summary = "Get randomly character", description = "Return a character by random id") | ||||||
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
Also |
||||||
public InternalCharacterDto getRandomlyCharacter() { | ||||||
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
|
||||||
return characterService.getRandomlyCharacter(); | ||||||
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 | ||||||
@Operation(summary = "Filter by the given name", | ||||||
description = "Returns a list of all characters whose name contains the search string") | ||||||
public List<InternalCharacterDto> getCharacterByName(String 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
|
||||||
return characterService.getCharacterByName(name); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class CharacterResponseDto { | ||
private InfoResponseDto info; | ||
private List<ExternalCharacterDto> result; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class ExternalCharacterDto { | ||
@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,13 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class InfoResponseDto { | ||
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. You can use @DaTa with Dtos instead of these annotations |
||
private String nextUrl; | ||
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. Have you checked the result? I don't think |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class InternalCharacterDto { | ||
private Long 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,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class CharacterNotFoundException extends RuntimeException { | ||
public CharacterNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.ExternalCharacterDto; | ||
import mate.academy.rickandmorty.dto.internal.InternalCharacterDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
Character toModel(ExternalCharacterDto externalCharacterDto); | ||
|
||
InternalCharacterDto toDto(Character character); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
@Entity | ||
@Table(name = "characters") | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long 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,11 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
List<Character> findAllByName(String name); | ||
} |
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.dto.internal.InternalCharacterDto; | ||
|
||
public interface CharacterService { | ||
InternalCharacterDto getRandomlyCharacter(); | ||
|
||
List<InternalCharacterDto> getCharacterByName(String name); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
package mate.academy.rickandmorty.service.impl; | ||||||
|
||||||
import java.util.List; | ||||||
import java.util.Random; | ||||||
import lombok.RequiredArgsConstructor; | ||||||
import mate.academy.rickandmorty.dto.internal.InternalCharacterDto; | ||||||
import mate.academy.rickandmorty.exception.CharacterNotFoundException; | ||||||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||||||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||||||
import mate.academy.rickandmorty.service.CharacterService; | ||||||
import org.springframework.stereotype.Service; | ||||||
|
||||||
@RequiredArgsConstructor | ||||||
@Service | ||||||
public class CharacterServiceImpl implements CharacterService { | ||||||
private final CharacterRepository characterRepository; | ||||||
private final CharacterMapper characterMapper; | ||||||
private final Random random; | ||||||
|
||||||
@Override | ||||||
public InternalCharacterDto getRandomlyCharacter() { | ||||||
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. @transactional. You make more than one call to DB |
||||||
Long randomId = random.nextLong(characterRepository.count()) + 1; | ||||||
return characterMapper.toDto(characterRepository.findById(randomId) | ||||||
.orElseThrow(() | ||||||
-> new CharacterNotFoundException("Can`t find a character by id: " | ||||||
+ randomId))); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public List<InternalCharacterDto> getCharacterByName(String 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
|
||||||
return characterRepository.findAllByName(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. |
||||||
.stream().map(characterMapper::toDto) | ||||||
.toList(); | ||||||
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. Mapstruct supports mapping collections from the box |
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
|
||
spring.datasource.url=jdbc:mysql://localhost:3306/rick_and_morty | ||
spring.datasource.username=root | ||
spring.datasource.password=6855 | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.show-sql=true | ||
spring.jpa.open-in-view=false |
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.
Your spring application is already scanning this package, so this line is redundant
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.
not fixed, doesn't it work without it?