Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 88 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -19,6 +19,8 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -41,13 +43,67 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>net.adrianoluis</groupId>
<artifactId>rickandmortyapi-java</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>rickandmortyapi-java-mvn-repo</id>
<url>https://raw.githubusercontent.com/adrianoluis/rickandmortyapi-java/mvn-repo/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -66,9 +122,39 @@
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
</project>
10 changes: 10 additions & 0 deletions src/main/java/mate/academy/rickandmorty/Application.java
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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/mate/academy/rickandmorty/dto/CharacterDto.java
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) {
}
8 changes: 8 additions & 0 deletions src/main/java/mate/academy/rickandmorty/dto/MetaInfo.java
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);
}
26 changes: 26 additions & 0 deletions src/main/java/mate/academy/rickandmorty/model/Character.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeneratedValue(strategy = GenerationType.IDENTITY)

@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotNull
private Long externalId;

Choose a reason for hiding this comment

The 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> {

Choose a reason for hiding this comment

The 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");

Choose a reason for hiding this comment

The 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);
}
Loading