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

Home work #132

Open
wants to merge 7 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"gender": "Male"
}
```
NOTE: `externalId` field should save the original character ID received from the external API. `id` field should
represent the identifier of entire `Character` entity, that is associated with internal DB.

NOTE: `externalId` field should save the original character ID received from the external API. `id` field should
represent the identifier of entire `Character` entity, that is associated with internal DB.

2. The request takes a string as an argument, and returns a list of all characters whose name contains the search
string.
Expand Down
84 changes: 83 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.2.3</version>
<relativePath/>
</parent>
<groupId>mate.academy</groupId>
Expand All @@ -15,6 +15,8 @@
<description>jv-rick-and-morty</description>
<properties>
<java.version>17</java.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
Expand All @@ -25,12 +27,51 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
Expand All @@ -45,6 +86,43 @@

<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.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>
<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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand All @@ -62,6 +140,10 @@
</execution>
</executions>
<configuration>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/mate/academy/rickandmorty/Application.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package mate.academy.rickandmorty;

import mate.academy.rickandmorty.service.CharacterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
@Autowired
private CharacterService characterService;

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

Choose a reason for hiding this comment

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

Suggested change

}

@Bean
public CommandLineRunner commandLineRunner() {
return args -> characterService.saveCharactersToDb();
}
Comment on lines +20 to 23

Choose a reason for hiding this comment

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

Remember to move Bean initialisations into @configuration class

}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.rickandmorty.config;

import org.mapstruct.InjectionStrategy;
import org.mapstruct.NullValueCheckStrategy;

@org.mapstruct.MapperConfig(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
implementationPackage = "<PACKAGE_NAME>.impl"
)
public class MapperConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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.CharacterResponseDto;
import mate.academy.rickandmorty.service.CharacterService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 = "For managing characters")
@RequiredArgsConstructor
@RestController
@RequestMapping("/characters")
public class CharacterController {
private final CharacterService characterService;

@GetMapping("/random")
@Operation(summary = "Get random character",
description = "You get a random character")
public CharacterResponseDto getRandomCharacters() {
return characterService.getRandomCharacter();
}

@GetMapping("/by-name")
@Operation(summary = "Find characters by name",
description = "You can find characters by name")
public List<CharacterResponseDto> findAllByName(@RequestParam String name) {
return characterService.findAllByName(name);
}

@GetMapping
@Operation(summary = "Get all characters",
description = "You get all characters, default parameters: page=0, size=20, sort=id")
public List<CharacterResponseDto> findAll(Pageable pageable) {
return characterService.findAll(pageable);
}

@GetMapping("/{id}")
@Operation(summary = "Find character by id",
description = "You can find character by id")
public CharacterResponseDto findById(@PathVariable Long id) {
return characterService.findById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mate.academy.rickandmorty.dto.external;

import lombok.Data;

@Data
public class ApiCharacterDto {
private Long id;
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.external;

import lombok.Data;

@Data
public class ApiInfoDto {
private Long count;
private Long pages;
private String next;
private String prev;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mate.academy.rickandmorty.dto.external;

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

@Data
public class ApiResponseDto {
@JsonProperty("info")
private ApiInfoDto apiInfoDto;
private List<ApiCharacterDto> results;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.rickandmorty.dto.internal;

import lombok.Data;
import mate.academy.rickandmorty.model.Gender;
import mate.academy.rickandmorty.model.Status;

@Data
public class CharacterResponseDto {
private Long id;
private Long externalId;
private String name;
private Status status;
private Gender gender;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mate.academy.rickandmorty.mapper;

import mate.academy.rickandmorty.dto.external.ApiCharacterDto;
import mate.academy.rickandmorty.dto.internal.CharacterResponseDto;
import mate.academy.rickandmorty.model.Character;
import mate.academy.rickandmorty.model.Gender;
import mate.academy.rickandmorty.model.Status;
import org.springframework.stereotype.Component;

@Component
public class CharacterMapper {

Choose a reason for hiding this comment

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

Consider using MapStruct to remove all boiler code :)

public CharacterResponseDto toResponseDto(Character character) {

Choose a reason for hiding this comment

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

be consistant with naming. It's either toCharacterResponseDto() and to Character() or toDto() and toModel()

Suggested change
public CharacterResponseDto toResponseDto(Character character) {
public CharacterResponseDto toDto(Character character) {

CharacterResponseDto characterDto = new CharacterResponseDto();
characterDto.setId(character.getId());
characterDto.setExternalId(character.getExternalId());
characterDto.setName(character.getName());
characterDto.setStatus(character.getStatus());
characterDto.setGender(character.getGender());
return characterDto;
}

public Character toModel(ApiCharacterDto dtoForDb) {
Character character = new Character();
character.setExternalId(dtoForDb.getId());
character.setName(dtoForDb.getName());
character.setGender(Gender.valueOf(dtoForDb.getGender().toUpperCase()));
character.setStatus(Status.valueOf(dtoForDb.getStatus().toUpperCase()));
return character;
}
}
74 changes: 74 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,74 @@
package mate.academy.rickandmorty.model;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "characters")
public class Character {

Choose a reason for hiding this comment

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

Suggested change
public class Character {
@Getter
@Setter
@ToString
public class Character {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long externalId;
private String name;
@Enumerated(EnumType.STRING)
private Status status;
@Enumerated(EnumType.STRING)
private Gender gender;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getExternalId() {
return externalId;
}

public void setExternalId(Long externalId) {
this.externalId = externalId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
this.gender = gender;
}

@Override
public String toString() {
return "Character{"
+ "id=" + id
+ ", externalId=" + externalId
+ ", name='" + name + '\''
+ ", status=" + status
+ ", gender=" + gender
+ '}';
}
}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/rickandmorty/model/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.rickandmorty.model;

public enum Gender {
FEMALE("Female"),
MALE("Male"),
GENDERLESS("Genderless"),
UNKNOWN("unknown");
private String value;

Gender(String value) {
this.value = value;
}
}
Loading
Loading