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 4 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.Character;
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 = "Character controller", description = "Endpoints for API")
@RestController
@RequestMapping(value = "/characters")
public class CharacterController {
@Autowired
private RickMortyService rickMortyService;

@Operation(summary = "Find all characters by name", description = "Get all characters by name")
@GetMapping("/by-name")

Choose a reason for hiding this comment

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

URI convention violation.
see:
https://restfulapi.net/resource-naming/

public List<Character> getAllByName(@RequestParam("name") String name) {
return rickMortyService.getAllCharactersByName(name);
}

@Operation(summary = "Find random character", description = "Get random character")
@GetMapping("/random-character")

Choose a reason for hiding this comment

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

/characters/random-character ? Too many "character"

Suggested change
@GetMapping("/random-character")
@GetMapping("/random")

public Character getRandomCharacter() {

Choose a reason for hiding this comment

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

URI convention violation.
see:
https://restfulapi.net/resource-naming/

return rickMortyService.getRandomCharacter();
}
}
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 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,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 CharactersDtoResponse {
private List<CharacterDtoResponse> results;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.rickandmorty.mappers;

import mate.academy.rickandmorty.dto.CharacterDtoResponse;
import mate.academy.rickandmorty.models.Character;

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 {

Character toModel(CharacterDtoResponse 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.CharacterDtoResponse;
import mate.academy.rickandmorty.models.Character;
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 Character toModel(CharacterDtoResponse rickMortyDtoRequest) {
if (rickMortyDtoRequest == null) {
return null;
} else {
Character character = new Character();
if (rickMortyDtoRequest.getExternalId() != null) {
character.setExternalId(rickMortyDtoRequest.getExternalId());
}

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

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

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

return character;
}
}
}

21 changes: 21 additions & 0 deletions src/main/java/mate/academy/rickandmorty/models/Character.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 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.Character;
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<Character, Long> {

Choose a reason for hiding this comment

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

Suggested change
public interface RickMortyRepository extends JpaRepository<Character, Long> {
public interface CharacterRepository extends JpaRepository<Character, Long> {

List<Character> findRickMortiesByNameContainingIgnoreCase(String name);

Character 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.Character;

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<Character> getAllCharactersByName(String name);

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

import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import mate.academy.rickandmorty.dto.CharactersDtoResponse;
import mate.academy.rickandmorty.mappers.RickMortyMapper;
import mate.academy.rickandmorty.models.Character;
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 CHARACTER_URL = "https://rickandmortyapi.com/api/character";
private final Random random;
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;
random = new Random();
}

@PostConstruct
private void downloadToDB() {
CharactersDtoResponse charactersDtoResponse = restTemplate.getForObject(CHARACTER_URL,
CharactersDtoResponse.class);
assert charactersDtoResponse != null;
rickMortyRepository.saveAll(charactersDtoResponse.getResults()
.stream()
.map(rickMortyMapper::toModel)
.collect(Collectors.toList()));
}

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

public Character getRandomCharacter() {
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