-
Notifications
You must be signed in to change notification settings - Fork 256
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
init structure, add liquibase #9
Open
teract10s
wants to merge
5
commits into
mate-academy:main
Choose a base branch
from
teract10s:hw
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
5 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/api/util/ApiClientConfiguration.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.api.util; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class ApiClientConfiguration { | ||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/rickandmorty/api/util/PersonageDatabasePopulator.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,22 @@ | ||
package mate.academy.rickandmorty.api.util; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.repository.PersonageRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class PersonageDatabasePopulator { | ||
private final RickAndMortyClient rickAndMortyApiClient; | ||
private final PersonageRepository personageRepository; | ||
|
||
@PostConstruct | ||
public void init() { | ||
if (personageRepository.count() != 0) { | ||
personageRepository.deleteAll(); | ||
} | ||
personageRepository.saveAll(rickAndMortyApiClient.getAllPersonages()); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/mate/academy/rickandmorty/api/util/RickAndMortyClient.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.api.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.ExternalResponseDto; | ||
import mate.academy.rickandmorty.dto.external.Result; | ||
import mate.academy.rickandmorty.mapper.PersonageMapper; | ||
import mate.academy.rickandmorty.model.Personage; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RickAndMortyClient { | ||
private final PersonageMapper personageMapper; | ||
private final RestTemplate restTemplate; | ||
@Value("${rick-and-morty.api.url}") | ||
private String baseCharacterUrl; | ||
|
||
public List<Personage> getAllPersonages() { | ||
List<Result> resultList = new ArrayList<>(); | ||
ExternalResponseDto externalResponseDto = | ||
restTemplate.getForEntity(baseCharacterUrl, ExternalResponseDto.class).getBody(); | ||
String nextUrl = externalResponseDto.info().next(); | ||
while (nextUrl != null) { | ||
resultList.addAll(externalResponseDto.resultList()); | ||
externalResponseDto = restTemplate | ||
.getForEntity(nextUrl, ExternalResponseDto.class) | ||
.getBody(); | ||
nextUrl = externalResponseDto.info().next(); | ||
} | ||
return resultList | ||
.stream() | ||
.map(personageMapper::toPersonage) | ||
.toList(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.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,41 @@ | ||
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.PersonageResponseDto; | ||
import mate.academy.rickandmorty.service.PersonageService; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableDefault; | ||
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; | ||
|
||
@RestController | ||
@Tag(name = "Controller to get personages", | ||
description = "In this controller you can get random personages and personages by name") | ||
@RequiredArgsConstructor | ||
@RequestMapping("/rick-and-morty") | ||
public class RickAndMortyController { | ||
private final PersonageService personageRepository; | ||
|
||
@GetMapping("/random-personage") | ||
@Operation(summary = "Get random personage", | ||
description = "Get random personage from Rick and Morty universe") | ||
public PersonageResponseDto getRandomPersonage() { | ||
return personageRepository.getRandomPersonage(); | ||
} | ||
|
||
@GetMapping("/search") | ||
@Operation(summary = "Get personages by name", | ||
description = "Get from Rick and Morty universe " | ||
+ "personages witch has name in their name") | ||
public List<PersonageResponseDto> getPersonageByName( | ||
@PageableDefault(page = 0, size = 10, sort = "name") Pageable pageable, | ||
@RequestParam String name | ||
) { | ||
return personageRepository.getPersonageByNameLike(pageable, name); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/dto/external/ExternalResponseDto.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.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
|
||
public record ExternalResponseDto( | ||
Info info, | ||
@JsonProperty("results") List<Result> resultList | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/dto/external/Info.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 Info( | ||
Long count, | ||
Integer pages, | ||
String next, | ||
String prev | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/external/Result.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,11 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record Result( | ||
@JsonProperty("id") Long externalId, | ||
String name, | ||
String status, | ||
String gender | ||
) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/dto/internal/PersonageResponseDto.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,8 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
public record PersonageResponseDto(Long id, | ||
Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/rickandmorty/excpetion/CustomGlobalExceptionHandler.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,16 @@ | ||
package mate.academy.rickandmorty.excpetion; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
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. don't you need |
||
@ExceptionHandler(EntityNotFoundException.class) | ||
public ResponseEntity<Object> handleEntityNotFound(EntityNotFoundException ex) { | ||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/mate/academy/rickandmorty/mapper/PersonageMapper.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,18 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.dto.external.Result; | ||
import mate.academy.rickandmorty.dto.internal.PersonageResponseDto; | ||
import mate.academy.rickandmorty.model.Personage; | ||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@Mapper(componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl") | ||
public interface PersonageMapper { | ||
PersonageResponseDto toDto(Personage personage); | ||
|
||
Personage toPersonage(Result result); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/mate/academy/rickandmorty/model/Personage.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,29 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "personages") | ||
public class Personage { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(name = "external_id", nullable = false) | ||
private Long externalId; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private String status; | ||
|
||
private String gender; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/mate/academy/rickandmorty/repository/PersonageRepository.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,17 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.rickandmorty.model.Personage; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface PersonageRepository extends JpaRepository<Personage, Long> { | ||
List<Personage> findAllByNameContains(Pageable pageable, String name); | ||
|
||
@Query("from Personage p order by RAND() limit 1") | ||
Optional<Personage> findRandomPersonage(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/service/PersonageService.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,11 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.internal.PersonageResponseDto; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface PersonageService { | ||
List<PersonageResponseDto> getPersonageByNameLike(Pageable pageable, String name); | ||
|
||
PersonageResponseDto getRandomPersonage(); | ||
} |
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.
I don't see any Swagger documentation :)
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.
Let's add Tag annotation