-
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
implemented rick and morty hw #30
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package mate.academy.rickandmorty; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.service.CharacterClient; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@RequiredArgsConstructor | ||
@SpringBootApplication | ||
public class Application { | ||
private final CharacterClient characterClient; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@PostConstruct | ||
void initDataBase() { | ||
characterClient.getCharacters(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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.CharacterDto; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springdoc.core.annotations.ParameterObject; | ||
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; | ||
|
||
@Tag(name = "Characters management", | ||
description = "Endpoints for receiving Rick and Morty characters from DB") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(value = "characters") | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@Operation(summary = "Getting random character", | ||
description = "Randomly generates a wiki about one character " | ||
+ "in the universe the animated series Rick & Morty") | ||
@GetMapping(value = "/random") | ||
public CharacterDto getRandomCharacter() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@Operation(summary = "Getting character by name containing string", | ||
description = "Returns a list of all characters " | ||
+ "whose name contains the search string") | ||
@GetMapping(value = "/search") | ||
public List<CharacterDto> searchCharactersByName( | ||
@ParameterObject @PageableDefault(size = 5) Pageable pageable, | ||
@RequestParam String name) { | ||
return characterService.getCharacterByName(name, pageable); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
public record CharacterDto(Long id, | ||
Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record CharacterResponseDto(@JsonProperty("id") Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import java.util.List; | ||
|
||
public record CharacterResponseDtoList(MetaInfo info, List<CharacterResponseDto> results) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
public record MetaInfo( | ||
Integer count, | ||
Integer pages, | ||
String next, | ||
String prev) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
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 { | ||
@ExceptionHandler(DataProcessingException.class) | ||
protected ResponseEntity<String> handleEntityNotFoundExceptions(DataProcessingException ex) { | ||
return new ResponseEntity<>("can't load data: " | ||
+ ex.getMessage(), HttpStatus.NOT_FOUND); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.dto.CharacterDto; | ||
import mate.academy.rickandmorty.dto.CharacterResponseDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
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 CharacterMapper { | ||
CharacterDto toDto(Character character); | ||
|
||
Character toCharacter(CharacterResponseDto characterResponseDto); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Table(name = "characters") | ||
@Data | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
@NotNull | ||
private Long externalId; | ||
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. Could externalId, name and status be null? |
||
@NotBlank | ||
private String name; | ||
@NotBlank | ||
private String status; | ||
private String gender; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
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 CharacterRepository extends JpaRepository<Character, Long> { | ||
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. Annotation Repository is missing |
||
List<Character> findCharacterByNameContaining(String name, Pageable pageable); | ||
|
||
@Query("from Character ORDER BY RAND() limit 1") | ||
Character getRandomCharacter(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.ArrayList; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.CharacterResponseDto; | ||
import mate.academy.rickandmorty.dto.CharacterResponseDtoList; | ||
import mate.academy.rickandmorty.exception.DataProcessingException; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CharacterClient { | ||
@Value("${rick-and-morty-url}") | ||
private String url; | ||
private final CharacterMapper characterMapper; | ||
private final CharacterRepository characterRepository; | ||
private final RestTemplate restTemplate; | ||
|
||
public void getCharacters() { | ||
try { | ||
CharacterResponseDtoList characterResponseDtoList | ||
= restTemplate.getForObject(url, CharacterResponseDtoList.class); | ||
ArrayList<CharacterResponseDto> characters | ||
= new ArrayList<>(characterResponseDtoList.results()); | ||
String nextUrl = characterResponseDtoList.info().next(); | ||
while (nextUrl != null) { | ||
characterResponseDtoList | ||
= restTemplate.getForObject(nextUrl, CharacterResponseDtoList.class); | ||
characters.addAll(characterResponseDtoList.results()); | ||
nextUrl = characterResponseDtoList.info().next(); | ||
} | ||
characterRepository.saveAll(characters.stream() | ||
.map(characterMapper::toCharacter) | ||
.toList()); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't load characters"); | ||
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. I didn't find GlobalExceptionHandler where you handle this exception |
||
} | ||
} | ||
} |
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.CharacterDto; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CharacterService { | ||
CharacterDto getRandomCharacter(); | ||
|
||
List<CharacterDto> getCharacterByName(String name, Pageable pageable); | ||
} |
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.
@GeneratedValue(strategy = GenerationType.IDENTITY)