-
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 solution/mark.1 #156
Open
andriimikhaylovskiy
wants to merge
2
commits into
mate-academy:main
Choose a base branch
from
andriimikhaylovskiy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
This file contains 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,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 { | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/mate/academy/rickandmorty/controller/CharacterController.java
This file contains 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,33 @@ | ||
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.CharacterDto; | ||
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.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Rick and Marty character management", description = | ||
"Endpoints for managing characters from Ricki and Marty") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/characters") | ||
public class CharacterController { | ||
private final CharacterService service; | ||
|
||
@Operation(summary = "Get character", description = "Get random character from db") | ||
@GetMapping("/random") | ||
public CharacterDto getRandomCharacter() { | ||
return service.getRandomCharacter(); | ||
} | ||
|
||
@Operation(summary = "Get list characters", description = "Get list characters by name from db") | ||
@GetMapping() | ||
public List<CharacterDto> findAllByName(@RequestParam String name) { | ||
return service.findAllByName(name); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/dto/external/CharacterInfoDataDto.java
This file contains 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 mate.academy.rickandmorty.dto.external; | ||
|
||
public record CharacterInfoDataDto( | ||
Integer count, | ||
Integer pages, | ||
String next, | ||
String prev | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/dto/external/CharacterResponseDataDto.java
This file contains 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 mate.academy.rickandmorty.dto.external; | ||
|
||
import java.util.List; | ||
|
||
public record CharacterResponseDataDto( | ||
CharacterInfoDataDto info, | ||
List<CreateCharacterRequestDto> results | ||
) { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/external/CreateCharacterRequestDto.java
This file contains 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,12 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record CreateCharacterRequestDto( | ||
@JsonProperty("id") | ||
String externalId, | ||
String name, | ||
String status, | ||
String gender | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/dto/internal/CharacterDto.java
This file contains 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,10 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
public record CharacterDto( | ||
Long id, | ||
String externalId, | ||
String name, | ||
String status, | ||
String gender | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/CanNotGetDataFromExternalApi.java
This file contains 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 mate.academy.rickandmorty.exception; | ||
|
||
public class CanNotGetDataFromExternalApi extends RuntimeException { | ||
public CanNotGetDataFromExternalApi(String message, Exception e) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java
This file contains 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,14 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.CreateCharacterRequestDto; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.model.CharacterModel; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
CharacterDto toDto(CharacterModel characterModel); | ||
|
||
CharacterModel toModel(CreateCharacterRequestDto requestDto); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/mate/academy/rickandmorty/model/CharacterModel.java
This file contains 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 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; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "characters") | ||
public class CharacterModel { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java
This file contains 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,13 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.CharacterModel; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface CharacterRepository extends JpaRepository<CharacterModel, Long> { | ||
List<CharacterModel> findAllByNameContainingIgnoreCase(String name); | ||
|
||
@Query(value = "FROM CharacterModel ORDER BY RAND() LIMIT 1") | ||
CharacterModel getRandomCharacter(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/service/CharacterService.java
This file contains 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,13 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.external.CreateCharacterRequestDto; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
|
||
public interface CharacterService { | ||
CharacterDto getRandomCharacter(); | ||
|
||
List<CharacterDto> findAllByName(String name); | ||
|
||
CharacterDto save(CreateCharacterRequestDto requestDto); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java
This file contains 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,39 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.CreateCharacterRequestDto; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.CharacterModel; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CharacterServiceImpl implements CharacterService { | ||
private final CharacterRepository repository; | ||
private final CharacterMapper mapper; | ||
|
||
@Override | ||
public CharacterDto getRandomCharacter() { | ||
return mapper.toDto(repository.getRandomCharacter()); | ||
} | ||
|
||
@Override | ||
public List<CharacterDto> findAllByName(String name) { | ||
return repository.findAllByNameContainingIgnoreCase(name) | ||
.stream() | ||
.map(mapper::toDto) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public CharacterDto save(CreateCharacterRequestDto requestDto) { | ||
CharacterModel characterModel = mapper.toModel(requestDto); | ||
CharacterModel savedCharacterModel = repository.save(characterModel); | ||
return mapper.toDto(savedCharacterModel); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/mate/academy/rickandmorty/service/impl/CharactersClient.java
This file contains 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,52 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto; | ||
import mate.academy.rickandmorty.exception.CanNotGetDataFromExternalApi; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class CharactersClient { | ||
private static final String BASE_URL = "https://rickandmortyapi.com/api/character"; | ||
private final HttpClient httpClient = HttpClient.newHttpClient(); | ||
private final ObjectMapper objectMapper; | ||
private final CharacterService characterService; | ||
|
||
@PostConstruct | ||
public void init() { | ||
getListWithExternalApi(); | ||
} | ||
|
||
private void getListWithExternalApi() { | ||
String url = BASE_URL; | ||
while (url != null) { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(url)) | ||
.build(); | ||
try { | ||
HttpResponse<String> response = httpClient.send( | ||
request, | ||
HttpResponse.BodyHandlers.ofString()); | ||
Comment on lines
+37
to
+39
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. are you sure that you parse all pages, not the first one? |
||
CharacterResponseDataDto characterResponseDataDto = objectMapper.readValue( | ||
response.body(), | ||
CharacterResponseDataDto.class); | ||
characterResponseDataDto.results() | ||
.stream().map(characterService::save) | ||
.toList(); | ||
url = characterResponseDataDto.info().next(); | ||
} catch (IOException | InterruptedException e) { | ||
throw new CanNotGetDataFromExternalApi("Can't get data from API", e); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 +1,9 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimeZone=UTC | ||
spring.datasource.username=root | ||
spring.datasource.password=Mikhay@987AO | ||
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 | ||
|
||
logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG |
This file contains 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 |
---|---|---|
|
@@ -6,8 +6,7 @@ | |
@SpringBootTest | ||
class ApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
} |
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.
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.