Skip to content

Commit e594f47

Browse files
committed
created API
1 parent c3bbe60 commit e594f47

20 files changed

+430
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Task: Create API. It should contain two methods:
66

7-
1. The request randomly generates a wiki about one character in the universe the animated series Rick & Morty.
7+
1. The request randomly generates a wiki about one personage in the universe the animated series Rick & Morty.
88
Response example:
99

1010
```json
@@ -17,7 +17,7 @@
1717
}
1818
```
1919

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

2323
2. The request takes a string as an argument, and returns a list of all characters whose name contains the search

pom.xml

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.1.4</version>
8+
<version>3.4.1</version>
99
<relativePath/>
1010
</parent>
1111
<groupId>mate.academy</groupId>
@@ -19,30 +19,80 @@
1919
<maven.checkstyle.plugin.configLocation>
2020
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
2121
</maven.checkstyle.plugin.configLocation>
22+
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
23+
<mapstruct.version>1.6.3</mapstruct.version>
24+
<springdoc.version>2.8.4</springdoc.version>
2225
</properties>
2326
<dependencies>
2427
<dependency>
2528
<groupId>org.springframework.boot</groupId>
2629
<artifactId>spring-boot-starter</artifactId>
2730
</dependency>
28-
2931
<dependency>
3032
<groupId>org.springframework.boot</groupId>
3133
<artifactId>spring-boot-starter-test</artifactId>
3234
<scope>test</scope>
3335
</dependency>
34-
3536
<dependency>
3637
<groupId>org.springframework.boot</groupId>
3738
<artifactId>spring-boot-starter-data-jpa</artifactId>
3839
</dependency>
39-
4040
<dependency>
4141
<groupId>com.h2database</groupId>
4242
<artifactId>h2</artifactId>
4343
</dependency>
44+
<dependency>
45+
<groupId>com.mysql</groupId>
46+
<artifactId>mysql-connector-j</artifactId>
47+
<scope>runtime</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<scope>annotationProcessor</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.hibernate.orm</groupId>
56+
<artifactId>hibernate-core</artifactId>
57+
<version>6.6.4.Final</version>
58+
<scope>compile</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.mapstruct</groupId>
62+
<artifactId>mapstruct</artifactId>
63+
<version>${mapstruct.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.mapstruct</groupId>
67+
<artifactId>mapstruct-processor</artifactId>
68+
<version>1.5.1.Final</version>
69+
<scope>provided</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.fasterxml.jackson.core</groupId>
73+
<artifactId>jackson-databind</artifactId>
74+
<version>2.18.2</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.fasterxml.jackson.core</groupId>
78+
<artifactId>jackson-core</artifactId>
79+
<version>2.18.2</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.fasterxml.jackson.core</groupId>
83+
<artifactId>jackson-annotations</artifactId>
84+
<version>2.18.2</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-web</artifactId>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.springdoc</groupId>
92+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
93+
<version>${springdoc.version}</version>
94+
</dependency>
4495
</dependencies>
45-
4696
<build>
4797
<plugins>
4898
<plugin>
@@ -62,6 +112,7 @@
62112
</execution>
63113
</executions>
64114
<configuration>
115+
<sourceDirectories>src</sourceDirectories>
65116
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
66117
<consoleOutput>true</consoleOutput>
67118
<failsOnError>true</failsOnError>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mate.academy.rickandmorty.config;
2+
3+
import org.mapstruct.InjectionStrategy;
4+
import org.mapstruct.NullValueCheckStrategy;
5+
6+
@org.mapstruct.MapperConfig(
7+
componentModel = "spring",
8+
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
9+
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
10+
implementationPackage = "<PACKAGE_NAME>.impl"
11+
)
12+
public class MapperConfig {
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package mate.academy.rickandmorty.controller;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.tags.Tag;
5+
import java.util.List;
6+
import lombok.RequiredArgsConstructor;
7+
import mate.academy.rickandmorty.dto.internal.PersonageDto;
8+
import mate.academy.rickandmorty.service.PersonageService;
9+
import org.springframework.data.domain.Pageable;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@Tag(name = "Book management", description = "Endpoints for managing books.")
17+
@RestController
18+
@RequestMapping("personages")
19+
@RequiredArgsConstructor
20+
public class PersonageController {
21+
private final PersonageService personageService;
22+
23+
@GetMapping
24+
@ResponseStatus(HttpStatus.OK)
25+
@Operation(summary = "Get a random personage.")
26+
public PersonageDto getPersonage() {
27+
return personageService.getRandomPersonage();
28+
}
29+
30+
@GetMapping("/search")
31+
@ResponseStatus(HttpStatus.OK)
32+
@Operation(summary = "Filtering and sorting personages by name.")
33+
public List<PersonageDto> searchPersonage(String name, Pageable pageable) {
34+
return personageService.searchPersonage(name, pageable);
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package mate.academy.rickandmorty.dto.external;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public record InfoDto(int count, int pages, String next, String prev) {
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package mate.academy.rickandmorty.dto.external;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import java.util.List;
5+
import lombok.Data;
6+
import mate.academy.rickandmorty.dto.internal.CreatePersonageRequestDto;
7+
import org.springframework.stereotype.Component;
8+
9+
@Data
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
@Component
12+
public class ResponseDto {
13+
private List<CreatePersonageRequestDto> results;
14+
private InfoDto info;
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package mate.academy.rickandmorty.dto.internal;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
import jakarta.validation.constraints.NotBlank;
6+
import jakarta.validation.constraints.Positive;
7+
import lombok.Data;
8+
9+
@Data
10+
@Schema(description = "Request DTO for creating a new personage.")
11+
@JsonIgnoreProperties(ignoreUnknown = true)
12+
public class CreatePersonageRequestDto {
13+
@Positive
14+
@Schema(description = "The ID of the personage.", example = "12")
15+
private Long id;
16+
17+
@NotBlank
18+
@Schema(description = "The name of the personage.", example = "Beth Smith")
19+
private String name;
20+
21+
@NotBlank
22+
@Schema(description = "The status of the personage.", example = "Alive")
23+
private String status;
24+
25+
@NotBlank
26+
@Schema(description = "The gender of the personage.", example = "Female")
27+
private String gender;
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package mate.academy.rickandmorty.dto.internal;
2+
3+
public record PersonageDto(Long id,
4+
Long externalId,
5+
String name,
6+
String status,
7+
String gender) {
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package mate.academy.rickandmorty.mapper;
2+
3+
import mate.academy.rickandmorty.config.MapperConfig;
4+
import mate.academy.rickandmorty.dto.internal.CreatePersonageRequestDto;
5+
import mate.academy.rickandmorty.dto.internal.PersonageDto;
6+
import mate.academy.rickandmorty.model.Personage;
7+
import org.mapstruct.Mapper;
8+
import org.mapstruct.Mapping;
9+
10+
@Mapper(config = MapperConfig.class)
11+
public interface PersonageMapper {
12+
PersonageDto intoDto(Personage personage);
13+
14+
@Mapping(target = "id", ignore = true)
15+
@Mapping(target = "externalId", source = "id")
16+
Personage intoModel(CreatePersonageRequestDto requestDto);
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mate.academy.rickandmorty.model;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.Table;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
import org.hibernate.annotations.SQLDelete;
12+
import org.hibernate.annotations.SQLRestriction;
13+
14+
@Entity
15+
@Table(name = "personages")
16+
@SQLDelete(sql = "UPDATE characters SET is_deleted = true WHERE id = ?")
17+
@SQLRestriction(value = "is_deleted = false")
18+
@Getter
19+
@Setter
20+
public class Personage {
21+
@Id
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
23+
private Long id;
24+
25+
@Column(nullable = false)
26+
private Long externalId;
27+
28+
@Column(nullable = false)
29+
private String name;
30+
31+
@Column(nullable = false)
32+
private String status;
33+
34+
@Column(nullable = false)
35+
private String gender;
36+
37+
@Column(nullable = false)
38+
private boolean isDeleted = false;
39+
}

0 commit comments

Comments
 (0)