This commit is contained in:
Максим Яковлев 2024-03-17 16:58:24 +04:00
parent 12c7246800
commit 787a543098
20 changed files with 785 additions and 87 deletions

View File

@ -1,46 +0,0 @@
package com.example.demo;
import java.util.List;
import java.util.ArrayList;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/genre")
public class ApiController {
private final List<GenreDto> genres = new ArrayList<>();
@PostMapping
public boolean createGenre(@RequestBody GenreDto value){
return genres.add(value);
}
@GetMapping
public List<GenreDto> getAll(){
return genres;
}
@GetMapping("/{id}")
public GenreDto getGenre(@PathVariable(name = "id") int id){
return genres.get(id);
}
@DeleteMapping("/{id}")
public GenreDto delGenre(@PathVariable(name = "id") int id){
return genres.remove(id);
}
@PutMapping
public boolean updGenre(@RequestBody GenreDto genreDto){
genres.remove(genreDto.getId());
return genres.add(genreDto);
}
}

View File

@ -1,26 +0,0 @@
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class GenreDto {
private int id;
private String name;
public GenreDto(){
}
@JsonCreator
public GenreDto(@JsonProperty(value="id") int id,
@JsonProperty(value="name") String name){
this.id = id;
this.name = name;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
}

View File

@ -1,15 +0,0 @@
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(@NonNull CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
}

View File

@ -0,0 +1,74 @@
package com.example.demo.games.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.service.GameService;
import com.example.demo.genres.service.GenreService;
import com.example.demo.types.service.TypeService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/game")
public class GameController {
private final GameService gameService;
private final TypeService typeService;
private final GenreService genreService;
private final ModelMapper modelMapper;
public GameController(GameService gameService, TypeService typeService, GenreService genreService, ModelMapper modelMapper){
this.gameService = gameService;
this.genreService = genreService;
this.modelMapper = modelMapper;
this.typeService = typeService;
}
private GameDto toDto(GameEntity entity){
return modelMapper.map(entity, GameDto.class);
}
private GameEntity toEntity(GameDto dto){
final GameEntity entity = modelMapper.map(dto, GameEntity.class);
entity.setType(typeService.get(dto.getTypeId()));
entity.setGenre(genreService.get(dto.getGenreId()));
return entity;
}
@GetMapping
public List<GameDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId, @RequestParam(name = "genreId", defaultValue = "0") Long genreId){
return gameService.getAll(typeId, genreId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public GameDto get(@PathVariable(name = "id") Long id){
return toDto(gameService.get(id));
}
@PostMapping
public GameDto create(@RequestBody @Valid GameDto dto){
return toDto(gameService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public GameDto update(@PathVariable(name = "id") Long id, @RequestBody GameDto dto){
return toDto(gameService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public GameDto delete(@PathVariable(name = "id") Long id){
return toDto(gameService.delete(id));
}
}

View File

@ -0,0 +1,73 @@
package com.example.demo.games.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class GameDto {
private Long id;
@NotNull
@Min(1)
private Long typeId;
@NotNull
@Min(1)
private Long genreId;
@NotNull
@Min(1)
private Double price;
@NotNull
private String name;
@NotNull
private String description;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public Long getTypeId(){
return typeId;
}
public void setTypeId(Long typeId){
this.typeId = typeId;
}
public Long getGenreId(){
return genreId;
}
public void setGenreId(Long genreId){
this.genreId = genreId;
}
public Double getPrice(){
return price;
}
public void setPrice(Double price){
this.price = price;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
}

View File

@ -0,0 +1,88 @@
package com.example.demo.games.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.types.model.TypeEntity;
public class GameEntity extends BaseEntity{
private String name;
private Double price;
private String description;
private GenreEntity genre;
private TypeEntity type;
public GameEntity(){
super();
}
public GameEntity(Long id, TypeEntity type, String name, Double price, String description, GenreEntity genre){
super(id);
this.type = type;
this.name = name;
this.price = price;
this.description = description;
this.genre = genre;
}
public TypeEntity getType(){
return type;
}
public void setType(TypeEntity type){
this.type = type;
}
public Double getPrice(){
return price;
}
public void setPrice(Double price){
this.price = price;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public GenreEntity getGenre(){
return genre;
}
public void setGenre(GenreEntity genre){
this.genre = genre;
}
@Override
public int hashCode() {
return Objects.hash(id, type, price, genre, description, name);
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null || getClass() != obj.getClass())
return false;
final GameEntity other = (GameEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getGenre(), genre)
&& Objects.equals(other.getDescription(), description)
&& Objects.equals(other.getName(), name);
}
}

View File

@ -0,0 +1,11 @@
package com.example.demo.games.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.games.model.GameEntity;
@Repository
public class GameRepository extends MapRepository<GameEntity>{
}

View File

@ -0,0 +1,56 @@
package com.example.demo.games.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.repository.GameRepository;
@Service
public class GameService {
private final GameRepository repository;
public GameService(GameRepository repository){
this.repository = repository;
}
public List<GameEntity> getAll(Long typeId, Long genreId){
if(!Objects.equals(typeId, 0L) && !Objects.equals(genreId, 0L)){
return repository.getAll().stream().filter(game -> game.getType().getId().equals(typeId) && game.getGenre().getId().equals(genreId)).toList();
}
if(Objects.equals(typeId, 0L) && !Objects.equals(genreId, 0L)){
return repository.getAll().stream().filter(game -> game.getGenre().getId().equals(genreId)).toList();
}
if(!Objects.equals(typeId, 0L) && Objects.equals(genreId, 0L)){
return repository.getAll().stream().filter(game -> game.getType().getId().equals(typeId)).toList();
}
return repository.getAll();
}
public GameEntity get(Long id){
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public GameEntity create(GameEntity entity){
return repository.create(entity);
}
public GameEntity update(Long id, GameEntity entity){
final GameEntity existEntity = get(id);
existEntity.setName(entity.getName());
existEntity.setPrice(entity.getPrice());
existEntity.setDescription(entity.getDescription());
existEntity.setType(entity.getType());
existEntity.setGenre(entity.getGenre());
return repository.update(existEntity);
}
public GameEntity delete(Long id){
final GameEntity existEntity = get(id);
return repository.delete(existEntity);
}
}

View File

@ -0,0 +1,64 @@
package com.example.demo.genres.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.genres.service.GenreService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/genre")
public class GenreController {
private final GenreService genreService;
private final ModelMapper modelMapper;
public GenreController(GenreService genreService, ModelMapper modelMapper){
this.genreService = genreService;
this.modelMapper = modelMapper;
}
private GenreDto toDto(GenreEntity entity){
return modelMapper.map(entity, GenreDto.class);
}
private GenreEntity toEntity(GenreDto dto){
return modelMapper.map(dto, GenreEntity.class);
}
@GetMapping
public List<GenreDto> getAll(){
return genreService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public GenreDto get(@PathVariable(name="id") Long id){
return toDto(genreService.get(id));
}
@PostMapping
public GenreDto create(@RequestBody @Valid GenreDto dto){
return toDto(genreService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public GenreDto update(@PathVariable(name="id") Long id, @RequestBody GenreDto dto){
return toDto(genreService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public GenreDto delete(@PathVariable(name="id") Long id){
return toDto(genreService.delete(id));
}
}

View File

@ -0,0 +1,29 @@
package com.example.demo.genres.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class GenreDto {
private Long id;
@NotNull
@Min(1)
private String name;
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}

View File

@ -0,0 +1,41 @@
package com.example.demo.genres.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
public class GenreEntity extends BaseEntity {
private String name;
public GenreEntity(){
super();
}
public GenreEntity(Long id, String name){
super(id);
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public int hashCode(){
return Objects.hash(id, name);
}
@Override
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final GenreEntity other = (GenreEntity) obj;
return Objects.equals(other.getId(), id) && Objects.equals(other.getName(), name);
}
}

View File

@ -0,0 +1,10 @@
package com.example.demo.genres.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.genres.model.GenreEntity;
@Repository
public class GenreRepository extends MapRepository<GenreEntity> {
}

View File

@ -0,0 +1,42 @@
package com.example.demo.genres.service;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.genres.repository.GenreRepository;
@Service
public class GenreService {
private final GenreRepository repository;
public GenreService(GenreRepository repository){
this.repository = repository;
}
public List<GenreEntity> getAll(){
return repository.getAll();
}
public GenreEntity get(Long id){
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public GenreEntity create(GenreEntity entity){
return repository.create(entity);
}
public GenreEntity update(Long id, GenreEntity entity){
final GenreEntity exisEntity = get(id);
exisEntity.setName(entity.getName());
return repository.update(exisEntity);
}
public GenreEntity delete(Long id){
final GenreEntity existEntity = get(id);
return repository.delete(existEntity);
}
}

View File

@ -0,0 +1,64 @@
package com.example.demo.types.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/type")
public class TypeController {
private final TypeService typeService;
private final ModelMapper modelMapper;
public TypeController(TypeService typeService, ModelMapper modelMapper){
this.typeService = typeService;
this.modelMapper = modelMapper;
}
private TypeDto toDto(TypeEntity entity){
return modelMapper.map(entity, TypeDto.class);
}
private TypeEntity toEntity(TypeDto dto){
return modelMapper.map(dto, TypeEntity.class);
}
@GetMapping
public List<TypeDto> getAll(){
return typeService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public TypeDto get(@PathVariable(name = "id") Long id){
return toDto(typeService.get(id));
}
@PostMapping
public TypeDto create(@RequestBody @Valid TypeDto dto){
return toDto(typeService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto){
return toDto(typeService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public TypeDto delete(@PathVariable(name = "id") Long id){
return toDto(typeService.delete(id));
}
}

View File

@ -0,0 +1,28 @@
package com.example.demo.types.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
public class TypeDto {
private Long id;
@NotBlank
private String name;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}

View File

@ -0,0 +1,41 @@
package com.example.demo.types.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
public class TypeEntity extends BaseEntity{
private String name;
public TypeEntity(){
super();
}
public TypeEntity(Long id, String name){
super(id);
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public int hashCode(){
return Objects.hash(id,name);
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final TypeEntity other = (TypeEntity) obj;
return Objects.equals(other.getId(), id) && Objects.equals(other.getName(), name);
}
}

View File

@ -0,0 +1,11 @@
package com.example.demo.types.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.types.model.TypeEntity;
@Repository
public class TypeRepository extends MapRepository<TypeEntity> {
}

View File

@ -0,0 +1,42 @@
package com.example.demo.types.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.repository.TypeRepository;
@Service
public class TypeService {
private final TypeRepository repository;
public TypeService(TypeRepository repository){
this.repository = repository;
}
public List<TypeEntity> getAll(){
return repository.getAll();
}
public TypeEntity get(Long id){
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public TypeEntity create(TypeEntity entity){
return repository.create(entity);
}
public TypeEntity update(Long id, TypeEntity entity){
final TypeEntity existsEntity = get(id);
existsEntity.setName(entity.getName());
return repository.update(existsEntity);
}
public TypeEntity delete(Long id){
final TypeEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -0,0 +1,53 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.service.GameService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class GameServiceTests {
@Autowired
private GameService gameService;
@Test
void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
}
@Test
@Order(1)
void createTest(){
gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", null));
gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", null));
Assertions.assertEquals(2, gameService.getAll(0L,0L).size());
}
@Test
@Order(2)
void updateTest(){
final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", null);
final GameEntity updGame = gameService.update(2L, newGame);
Assertions.assertEquals(2, gameService.getAll(0L, 0L).size());
Assertions.assertEquals(updGame, gameService.get(2L));
Assertions.assertEquals(newGame.getName(), updGame.getName());
Assertions.assertEquals(newGame.getDescription(), updGame.getDescription());
Assertions.assertEquals(newGame.getPrice(), updGame.getPrice());
}
@Test
@Order(3)
void deleteTest(){
gameService.delete(2L);
Assertions.assertEquals(1L, gameService.getAll(0L,0L).size());
}
}

View File

@ -0,0 +1,58 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class TypeServiceTests {
@Autowired
private TypeService typeService;
@Test
void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
}
@Test
@Order(1)
void createTest(){
typeService.create(new TypeEntity(null, "Игра"));
typeService.create(new TypeEntity(null, "Программа"));
final TypeEntity last = typeService.create(new TypeEntity(null, "Игра2"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(last, typeService.get(3L));
}
@Test
@Order(2)
void updateTest(){
final String test = "TEST";
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
}
@Test
@Order(3)
void deleteTest(){
typeService.delete(3L);
Assertions.assertEquals(2, typeService.getAll().size());
final TypeEntity last = typeService.get(2L);
Assertions.assertEquals(2L, last.getId());
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игра"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
}