ip program test
This commit is contained in:
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/mvnw text eol=lf
|
||||
*.cmd text eol=crlf
|
||||
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
83
pom.xml
Normal file
83
pom.xml
Normal file
@@ -0,0 +1,83 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>ru.ip</groupId>
|
||||
<artifactId>example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>example</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<url/>
|
||||
<licenses>
|
||||
<license/>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer/>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection/>
|
||||
<developerConnection/>
|
||||
<tag/>
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>1.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>1.6.3</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
20
src/main/java/ru/ip/example/ExampleApplication.java
Normal file
20
src/main/java/ru/ip/example/ExampleApplication.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ru.ip.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import ru.ip.example.config.SwaggerConfig;
|
||||
import ru.ip.example.controller.FilmController;
|
||||
import ru.ip.example.mapper.FilmMapper;
|
||||
import ru.ip.example.repository.FilmRepository;
|
||||
import ru.ip.example.service.FilmService;
|
||||
|
||||
@ComponentScan(basePackageClasses = {FilmMapper.class, FilmController.class, FilmService.class, FilmRepository.class, SwaggerConfig.class})
|
||||
@SpringBootApplication
|
||||
public class ExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExampleApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
67
src/main/java/ru/ip/example/config/SwaggerConfig.java
Normal file
67
src/main/java/ru/ip/example/config/SwaggerConfig.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package ru.ip.example.config;
|
||||
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
import org.springdoc.core.customizers.OpenApiCustomizer;
|
||||
import org.springdoc.core.models.GroupedOpenApi;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// путь к свагеру http://localhost:8080/swagger-ui/index.html
|
||||
@Configuration
|
||||
public class SwaggerConfig {
|
||||
|
||||
private static final String title = "Фильмы";
|
||||
private static final String description = "Тестовый сервис";
|
||||
private static final String version = "0.0.1";
|
||||
private static final String contactEmail = "email";
|
||||
private static final String contactName = "developer";
|
||||
private static final String taskLink = "http://TO-BE-INSERTED";
|
||||
private static final String openApiGroup = "public-api-v1";
|
||||
private static final String pathsToMatch = "/**";
|
||||
|
||||
@Bean
|
||||
public OpenApiCustomizer openApiCustomiser() {
|
||||
return openApi -> openApi.getComponents().getSchemas()
|
||||
.values().forEach(s -> s.setAdditionalProperties(false));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GroupedOpenApi publicApiV1(OpenApiCustomizer openApiCustomiser) {
|
||||
return GroupedOpenApi.builder()
|
||||
.group(openApiGroup)
|
||||
.pathsToMatch(pathsToMatch)
|
||||
.addOpenApiCustomizer(openApiCustomiser)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OpenAPI api() {
|
||||
final var bearerSecuritySchema = new SecurityScheme()
|
||||
.type(SecurityScheme.Type.HTTP);
|
||||
|
||||
return new OpenAPI()
|
||||
.openapi("3.0.1")
|
||||
.info(new Info()
|
||||
.title(title)
|
||||
.description(description)
|
||||
.version(version)
|
||||
.contact(new Contact().email(contactEmail).name(contactName).url(taskLink))
|
||||
);
|
||||
}
|
||||
|
||||
private List<Server> servers() {
|
||||
final Server dso = new Server()
|
||||
.url("http://localhost:8080")
|
||||
.description("test");
|
||||
return List.of(dso);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
54
src/main/java/ru/ip/example/controller/FilmController.java
Normal file
54
src/main/java/ru/ip/example/controller/FilmController.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package ru.ip.example.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ip.example.domain.FilmDto;
|
||||
import ru.ip.example.service.FilmService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@Tag(name = "FilmsController")
|
||||
public class FilmController {
|
||||
|
||||
private final FilmService filmService;
|
||||
|
||||
@Autowired
|
||||
public FilmController(FilmService filmService) {
|
||||
this.filmService = filmService;
|
||||
}
|
||||
|
||||
@GetMapping("/films")
|
||||
@Operation(description = "Возвращает список всех фильмов")
|
||||
public List<FilmDto> getAll() {
|
||||
return filmService.findAllFilms();
|
||||
}
|
||||
|
||||
@GetMapping("/films/{id}")
|
||||
@Operation(description = "Возвращает фильм по id")
|
||||
public FilmDto getById(@PathVariable("id") Integer id) {
|
||||
return filmService.findFilmById(id);
|
||||
}
|
||||
|
||||
@PostMapping("/films")
|
||||
@Operation(description = "Сохраняет фильм")
|
||||
public FilmDto save(@RequestBody FilmDto filmDto) {
|
||||
return filmService.saveFilm(filmDto);
|
||||
}
|
||||
|
||||
@PutMapping("/films/{id}")
|
||||
@Operation(description = "Обновляет фильм")
|
||||
public FilmDto update(@Schema(description = "Id фильма") @PathVariable("id") Integer id, @RequestBody FilmDto filmDto) {
|
||||
return filmService.updateFilm(id, filmDto);
|
||||
}
|
||||
|
||||
@DeleteMapping("/films/{id}")
|
||||
@Operation(description = "Удаляет фильм")
|
||||
public void deleteById(@PathVariable("id") Integer id) {
|
||||
filmService.deleteById(id);
|
||||
}
|
||||
}
|
||||
50
src/main/java/ru/ip/example/domain/FilmDto.java
Normal file
50
src/main/java/ru/ip/example/domain/FilmDto.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package ru.ip.example.domain;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
//dto - data transfer object
|
||||
@Schema(description = "Информация о фильме")
|
||||
public class FilmDto {
|
||||
|
||||
@Schema(description = "Название фильма")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "Категория фильма")
|
||||
private String category;
|
||||
|
||||
@Schema(description = "Год выпуска")
|
||||
private Integer releaseYear;
|
||||
|
||||
public FilmDto(String title, String category, Integer releaseYear) {
|
||||
this.title = title;
|
||||
this.category = category;
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
|
||||
public FilmDto() {
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Integer getReleaseYear() {
|
||||
return releaseYear;
|
||||
}
|
||||
|
||||
public void setReleaseYear(Integer releaseYear) {
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
}
|
||||
79
src/main/java/ru/ip/example/domain/FilmEntity.java
Normal file
79
src/main/java/ru/ip/example/domain/FilmEntity.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package ru.ip.example.domain;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FilmEntity {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String category;
|
||||
|
||||
private Integer releaseYear;
|
||||
|
||||
public FilmEntity(Integer id, String title, String category, Integer releaseYear) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.category = category;
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
|
||||
public FilmEntity() {
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Integer getReleaseYear() {
|
||||
return releaseYear;
|
||||
}
|
||||
|
||||
public void setReleaseYear(Integer releaseYear) {
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
FilmEntity that = (FilmEntity) o;
|
||||
return Objects.equals(id, that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FilmEntity{" +
|
||||
"id=" + id +
|
||||
", title='" + title + '\'' +
|
||||
", category='" + category + '\'' +
|
||||
", releaseYear=" + releaseYear +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
14
src/main/java/ru/ip/example/mapper/FilmMapper.java
Normal file
14
src/main/java/ru/ip/example/mapper/FilmMapper.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ru.ip.example.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import ru.ip.example.domain.FilmDto;
|
||||
import ru.ip.example.domain.FilmEntity;
|
||||
|
||||
//чтобы spring инициализировал маппер
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface FilmMapper {
|
||||
|
||||
FilmDto toDto(FilmEntity entity);
|
||||
|
||||
FilmEntity toEntity(FilmDto dto);
|
||||
}
|
||||
16
src/main/java/ru/ip/example/repository/FilmRepository.java
Normal file
16
src/main/java/ru/ip/example/repository/FilmRepository.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package ru.ip.example.repository;
|
||||
|
||||
import ru.ip.example.domain.FilmEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FilmRepository {
|
||||
|
||||
FilmEntity save(FilmEntity entity);
|
||||
|
||||
List<FilmEntity> findAll();
|
||||
|
||||
FilmEntity findById(Integer id);
|
||||
|
||||
void deleteById(Integer id);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package ru.ip.example.repository;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ip.example.domain.FilmEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FilmRepositoryDao implements FilmRepository {
|
||||
|
||||
private static Integer idSequence = 0;
|
||||
|
||||
private static List<FilmEntity> films = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public FilmEntity save(FilmEntity entity) {
|
||||
if (entity.getId() == null) {
|
||||
entity.setId(idSequence++);
|
||||
} else if (films.contains(entity)) {
|
||||
films.remove(entity);
|
||||
}
|
||||
films.add(entity);
|
||||
System.out.println("save/upd film: " + entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FilmEntity> findAll() {
|
||||
return films;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FilmEntity findById(Integer id) {
|
||||
for (FilmEntity film : films) {
|
||||
if (film.getId().equals(id)) {
|
||||
return film;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//equals реализован только с id, поэтому для remove в entity проставляем только id
|
||||
@Override
|
||||
public void deleteById(Integer id) {
|
||||
FilmEntity filmEntity = new FilmEntity();
|
||||
filmEntity.setId(id);
|
||||
films.remove(filmEntity);
|
||||
}
|
||||
}
|
||||
18
src/main/java/ru/ip/example/service/FilmService.java
Normal file
18
src/main/java/ru/ip/example/service/FilmService.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ru.ip.example.service;
|
||||
|
||||
import ru.ip.example.domain.FilmDto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FilmService {
|
||||
|
||||
FilmDto saveFilm(FilmDto dto);
|
||||
|
||||
FilmDto updateFilm(Integer id, FilmDto dto);
|
||||
|
||||
List<FilmDto> findAllFilms();
|
||||
|
||||
FilmDto findFilmById(Integer id);
|
||||
|
||||
void deleteById(Integer id);
|
||||
}
|
||||
62
src/main/java/ru/ip/example/service/FilmServiceImpl.java
Normal file
62
src/main/java/ru/ip/example/service/FilmServiceImpl.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package ru.ip.example.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ip.example.domain.FilmDto;
|
||||
import ru.ip.example.domain.FilmEntity;
|
||||
import ru.ip.example.mapper.FilmMapper;
|
||||
import ru.ip.example.repository.FilmRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FilmServiceImpl implements FilmService {
|
||||
|
||||
private final FilmRepository filmRepository;
|
||||
|
||||
private final FilmMapper filmMapper;
|
||||
|
||||
@Autowired
|
||||
public FilmServiceImpl(FilmRepository filmRepository, FilmMapper filmMapper) {
|
||||
this.filmRepository = filmRepository;
|
||||
this.filmMapper = filmMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FilmDto saveFilm(FilmDto dto) {
|
||||
FilmEntity entity = filmMapper.toEntity(dto);
|
||||
FilmEntity savedFilm = filmRepository.save(entity);
|
||||
return filmMapper.toDto(savedFilm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FilmDto updateFilm(Integer id, FilmDto dto) {
|
||||
FilmEntity filmEntity = filmMapper.toEntity(dto);
|
||||
filmEntity.setId(id);
|
||||
FilmEntity updatedEntity = filmRepository.save(filmEntity);
|
||||
return filmMapper.toDto(updatedEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FilmDto> findAllFilms() {
|
||||
List<FilmEntity> filmEntities = filmRepository.findAll();
|
||||
List<FilmDto> films = new ArrayList<>();
|
||||
for (FilmEntity entity : filmEntities) {
|
||||
FilmDto dto = filmMapper.toDto(entity);
|
||||
films.add(dto);
|
||||
}
|
||||
return films;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FilmDto findFilmById(Integer id) {
|
||||
FilmEntity film = filmRepository.findById(id);
|
||||
return filmMapper.toDto(film);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Integer id) {
|
||||
filmRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
spring.application.name=example
|
||||
13
src/test/java/ru/ip/example/ExampleApplicationTests.java
Normal file
13
src/test/java/ru/ip/example/ExampleApplicationTests.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ru.ip.example;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ExampleApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user