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

first commit #29

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
64 changes: 63 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,46 @@
<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>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand All @@ -31,7 +69,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -45,6 +82,31 @@

<build>
<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.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mate/academy/rickandmorty/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mate.academy.rickandmorty.controllers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import mate.academy.rickandmorty.models.RickMorty;
import mate.academy.rickandmorty.service.RickMortyService;
import org.springframework.beans.factory.annotation.Autowired;
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 = "Product controller", description = "Endpoints for API")
@RestController
@RequestMapping(value = "/rick_morty")

Choose a reason for hiding this comment

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

fix all URLs according to convention

public class ApplicationController {

Choose a reason for hiding this comment

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

Suggested change
public class ApplicationController {
public class CharacterController {

@Autowired
private RickMortyService rickMortyService;

@Operation(summary = "Find all characters by name", description = "Get all characters by name")
@GetMapping("/all_characters_by_name")
public List<RickMorty> getAllByName(@RequestParam("name") String name) {
return rickMortyService.getAllCharactersByName(name);
}

@Operation(summary = "Find random character", description = "Get random character")
@GetMapping("/random_character")
public RickMorty getRandomCharacter() {
return rickMortyService.getRandomCharacter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mate.academy.rickandmorty.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CharacterDtoResponse {
private List<RickMortyDtoResponse> results;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mate.academy.rickandmorty.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class RickMortyDtoResponse {

Choose a reason for hiding this comment

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

looking at class field CharacterDtoResponse is more suitable name here

Suggested change
public class RickMortyDtoResponse {
public class CharacterDtoResponse {

@JsonProperty(value = "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,9 @@
package mate.academy.rickandmorty.mappers;

import mate.academy.rickandmorty.dto.RickMortyDtoResponse;
import mate.academy.rickandmorty.models.RickMorty;

public interface RickMortyMapper {

Choose a reason for hiding this comment

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

Suggested change
public interface RickMortyMapper {
public interface CharacterMapper {

RickMorty toModel(RickMortyDtoResponse rickMortyDtoRequest);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mate.academy.rickandmorty.mappers;

import mate.academy.rickandmorty.dto.RickMortyDtoResponse;
import mate.academy.rickandmorty.models.RickMorty;
import org.springframework.stereotype.Component;

@Component
public class RickMortyMapperImpl implements RickMortyMapper {
public RickMortyMapperImpl() {
}

Choose a reason for hiding this comment

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

Why do you need it?

Copy link
Author

Choose a reason for hiding this comment

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

I don't undestand the question! I need this implementation because we must convert responce object to model))

Choose a reason for hiding this comment

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

mapstruct should create Mapper impl - or why do you need its dependency in pom.xml?

Copy link
Author

Choose a reason for hiding this comment

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

I have a problem with this!)) I did with Mapper, but during maven test I have checkstyle error in target package, MapperImpl class(which generates in the runtime) and I can't fix this!


public RickMorty toModel(RickMortyDtoResponse rickMortyDtoRequest) {
if (rickMortyDtoRequest == null) {
return null;
} else {
RickMorty rickMorty = new RickMorty();
if (rickMortyDtoRequest.getExternalId() != null) {
rickMorty.setExternalId(rickMortyDtoRequest.getExternalId());
}

if (rickMortyDtoRequest.getName() != null) {
rickMorty.setName(rickMortyDtoRequest.getName());
}

if (rickMortyDtoRequest.getStatus() != null) {
rickMorty.setStatus(rickMortyDtoRequest.getStatus());
}

if (rickMortyDtoRequest.getGender() != null) {
rickMorty.setGender(rickMortyDtoRequest.getGender());
}

return rickMorty;
}
}
}

21 changes: 21 additions & 0 deletions src/main/java/mate/academy/rickandmorty/models/RickMorty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mate.academy.rickandmorty.models;

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 = "characters")
public class RickMorty {

Choose a reason for hiding this comment

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

Suggested change
public class RickMorty {
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,17 @@
package mate.academy.rickandmorty.repository;

import java.util.List;
import mate.academy.rickandmorty.models.RickMorty;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface RickMortyRepository extends JpaRepository<RickMorty, Long> {
List<RickMorty> findRickMortiesByNameContainingIgnoreCase(String name);

RickMorty findRickMortyById(Long externalId);

@Query(value = "SELECT MAX(id) FROM characters;",nativeQuery = true)
Long getMaxId();
}
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.models.RickMorty;

public interface RickMortyService {

Choose a reason for hiding this comment

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

Suggested change
public interface RickMortyService {
public interface CharacterService {

List<RickMorty> getAllCharactersByName(String name);

RickMorty getRandomCharacter();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mate.academy.rickandmorty.service.implementation;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import mate.academy.rickandmorty.dto.CharacterDtoResponse;
import mate.academy.rickandmorty.mappers.RickMortyMapper;
import mate.academy.rickandmorty.models.RickMorty;
import mate.academy.rickandmorty.repository.RickMortyRepository;
import mate.academy.rickandmorty.service.RickMortyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RickMortyServiceImpl implements RickMortyService {
private static final String characterURL = "https://rickandmortyapi.com/api/character";

Choose a reason for hiding this comment

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

uppercase

private static boolean isInitialized = false;

Choose a reason for hiding this comment

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

Suggested change
private static boolean isInitialized = false;

private final RestTemplate restTemplate;
private final RickMortyRepository rickMortyRepository;
private final RickMortyMapper rickMortyMapper;

@Autowired
public RickMortyServiceImpl(RestTemplate restTemplate,
RickMortyRepository rickMortyRepository,
RickMortyMapper rickMortyMapper) {
this.restTemplate = restTemplate;
this.rickMortyRepository = rickMortyRepository;
this.rickMortyMapper = rickMortyMapper;
downloadToDB();

Choose a reason for hiding this comment

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

use PostCunstruct

Suggested change
downloadToDB();

}

private void downloadToDB() {
if (!isInitialized) {
CharacterDtoResponse characterDtoResponse = restTemplate.getForObject(characterURL,
CharacterDtoResponse.class);
assert characterDtoResponse != null;
rickMortyRepository.saveAll(characterDtoResponse.getResults()
.stream()
.map(rickMortyMapper::toModel)
.collect(Collectors.toList()));
}
isInitialized = true;
}

public List<RickMorty> getAllCharactersByName(String name) {
return rickMortyRepository.findRickMortiesByNameContainingIgnoreCase(name);
}

public RickMorty getRandomCharacter() {
Random random = new Random();

Choose a reason for hiding this comment

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

make it class fied

Long maxId = rickMortyRepository.getMaxId();
return rickMortyRepository.findRickMortyById(random.nextLong(1, maxId));
}
}
7 changes: 6 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/rick_morty
spring.datasource.username=serg
spring.datasource.password=22062020
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Choose a reason for hiding this comment

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

Is this property required to be used?
should be removed if not.

Suggested change
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.show-sql= true