From 787a5430988367a0dc00ae49ed3f6c783af6673c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sun, 17 Mar 2024 16:58:24 +0400 Subject: [PATCH] lab2 --- .../java/com/example/demo/ApiController.java | 46 ---------- src/main/java/com/example/demo/GenreDto.java | 26 ------ src/main/java/com/example/demo/WebConfig.java | 15 ---- .../demo/games/api/GameController.java | 74 ++++++++++++++++ .../com/example/demo/games/api/GameDto.java | 73 +++++++++++++++ .../example/demo/games/model/GameEntity.java | 88 +++++++++++++++++++ .../demo/games/repository/GameRepository.java | 11 +++ .../demo/games/service/GameService.java | 56 ++++++++++++ .../demo/genres/api/GenreController.java | 64 ++++++++++++++ .../com/example/demo/genres/api/GenreDto.java | 29 ++++++ .../demo/genres/model/GenreEntity.java | 41 +++++++++ .../genres/repository/GenreRepository.java | 10 +++ .../demo/genres/service/GenreService.java | 42 +++++++++ .../demo/types/api/TypeController.java | 64 ++++++++++++++ .../com/example/demo/types/api/TypeDto.java | 28 ++++++ .../example/demo/types/model/TypeEntity.java | 41 +++++++++ .../demo/types/repository/TypeRepository.java | 11 +++ .../demo/types/service/TypeService.java | 42 +++++++++ .../com/example/demo/GameServiceTests.java | 53 +++++++++++ .../com/example/demo/TypeServiceTests.java | 58 ++++++++++++ 20 files changed, 785 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/com/example/demo/ApiController.java delete mode 100644 src/main/java/com/example/demo/GenreDto.java delete mode 100644 src/main/java/com/example/demo/WebConfig.java create mode 100644 src/main/java/com/example/demo/games/api/GameController.java create mode 100644 src/main/java/com/example/demo/games/api/GameDto.java create mode 100644 src/main/java/com/example/demo/games/model/GameEntity.java create mode 100644 src/main/java/com/example/demo/games/repository/GameRepository.java create mode 100644 src/main/java/com/example/demo/games/service/GameService.java create mode 100644 src/main/java/com/example/demo/genres/api/GenreController.java create mode 100644 src/main/java/com/example/demo/genres/api/GenreDto.java create mode 100644 src/main/java/com/example/demo/genres/model/GenreEntity.java create mode 100644 src/main/java/com/example/demo/genres/repository/GenreRepository.java create mode 100644 src/main/java/com/example/demo/genres/service/GenreService.java create mode 100644 src/main/java/com/example/demo/types/api/TypeController.java create mode 100644 src/main/java/com/example/demo/types/api/TypeDto.java create mode 100644 src/main/java/com/example/demo/types/model/TypeEntity.java create mode 100644 src/main/java/com/example/demo/types/repository/TypeRepository.java create mode 100644 src/main/java/com/example/demo/types/service/TypeService.java create mode 100644 src/test/java/com/example/demo/GameServiceTests.java create mode 100644 src/test/java/com/example/demo/TypeServiceTests.java diff --git a/src/main/java/com/example/demo/ApiController.java b/src/main/java/com/example/demo/ApiController.java deleted file mode 100644 index 90db1df..0000000 --- a/src/main/java/com/example/demo/ApiController.java +++ /dev/null @@ -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 genres = new ArrayList<>(); - - @PostMapping - public boolean createGenre(@RequestBody GenreDto value){ - - return genres.add(value); - } - - @GetMapping - public List 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); - } -} \ No newline at end of file diff --git a/src/main/java/com/example/demo/GenreDto.java b/src/main/java/com/example/demo/GenreDto.java deleted file mode 100644 index 93ce8fc..0000000 --- a/src/main/java/com/example/demo/GenreDto.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/com/example/demo/WebConfig.java b/src/main/java/com/example/demo/WebConfig.java deleted file mode 100644 index d5585a1..0000000 --- a/src/main/java/com/example/demo/WebConfig.java +++ /dev/null @@ -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"); - } -} diff --git a/src/main/java/com/example/demo/games/api/GameController.java b/src/main/java/com/example/demo/games/api/GameController.java new file mode 100644 index 0000000..55984ed --- /dev/null +++ b/src/main/java/com/example/demo/games/api/GameController.java @@ -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 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)); + } +} diff --git a/src/main/java/com/example/demo/games/api/GameDto.java b/src/main/java/com/example/demo/games/api/GameDto.java new file mode 100644 index 0000000..456b2eb --- /dev/null +++ b/src/main/java/com/example/demo/games/api/GameDto.java @@ -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; + } + +} diff --git a/src/main/java/com/example/demo/games/model/GameEntity.java b/src/main/java/com/example/demo/games/model/GameEntity.java new file mode 100644 index 0000000..1020d69 --- /dev/null +++ b/src/main/java/com/example/demo/games/model/GameEntity.java @@ -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); + } +} diff --git a/src/main/java/com/example/demo/games/repository/GameRepository.java b/src/main/java/com/example/demo/games/repository/GameRepository.java new file mode 100644 index 0000000..961dad3 --- /dev/null +++ b/src/main/java/com/example/demo/games/repository/GameRepository.java @@ -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{ + +} diff --git a/src/main/java/com/example/demo/games/service/GameService.java b/src/main/java/com/example/demo/games/service/GameService.java new file mode 100644 index 0000000..0564772 --- /dev/null +++ b/src/main/java/com/example/demo/games/service/GameService.java @@ -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 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); + } +} diff --git a/src/main/java/com/example/demo/genres/api/GenreController.java b/src/main/java/com/example/demo/genres/api/GenreController.java new file mode 100644 index 0000000..852f406 --- /dev/null +++ b/src/main/java/com/example/demo/genres/api/GenreController.java @@ -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 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)); + } +} diff --git a/src/main/java/com/example/demo/genres/api/GenreDto.java b/src/main/java/com/example/demo/genres/api/GenreDto.java new file mode 100644 index 0000000..97a9778 --- /dev/null +++ b/src/main/java/com/example/demo/genres/api/GenreDto.java @@ -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; + } +} diff --git a/src/main/java/com/example/demo/genres/model/GenreEntity.java b/src/main/java/com/example/demo/genres/model/GenreEntity.java new file mode 100644 index 0000000..7213eab --- /dev/null +++ b/src/main/java/com/example/demo/genres/model/GenreEntity.java @@ -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); + } +} diff --git a/src/main/java/com/example/demo/genres/repository/GenreRepository.java b/src/main/java/com/example/demo/genres/repository/GenreRepository.java new file mode 100644 index 0000000..bd86082 --- /dev/null +++ b/src/main/java/com/example/demo/genres/repository/GenreRepository.java @@ -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 { +} diff --git a/src/main/java/com/example/demo/genres/service/GenreService.java b/src/main/java/com/example/demo/genres/service/GenreService.java new file mode 100644 index 0000000..08e6b46 --- /dev/null +++ b/src/main/java/com/example/demo/genres/service/GenreService.java @@ -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 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); + } +} diff --git a/src/main/java/com/example/demo/types/api/TypeController.java b/src/main/java/com/example/demo/types/api/TypeController.java new file mode 100644 index 0000000..2938544 --- /dev/null +++ b/src/main/java/com/example/demo/types/api/TypeController.java @@ -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 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)); + } +} diff --git a/src/main/java/com/example/demo/types/api/TypeDto.java b/src/main/java/com/example/demo/types/api/TypeDto.java new file mode 100644 index 0000000..f0ae6ec --- /dev/null +++ b/src/main/java/com/example/demo/types/api/TypeDto.java @@ -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; + } +} diff --git a/src/main/java/com/example/demo/types/model/TypeEntity.java b/src/main/java/com/example/demo/types/model/TypeEntity.java new file mode 100644 index 0000000..c60fcef --- /dev/null +++ b/src/main/java/com/example/demo/types/model/TypeEntity.java @@ -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); + } +} diff --git a/src/main/java/com/example/demo/types/repository/TypeRepository.java b/src/main/java/com/example/demo/types/repository/TypeRepository.java new file mode 100644 index 0000000..4474581 --- /dev/null +++ b/src/main/java/com/example/demo/types/repository/TypeRepository.java @@ -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 { + +} diff --git a/src/main/java/com/example/demo/types/service/TypeService.java b/src/main/java/com/example/demo/types/service/TypeService.java new file mode 100644 index 0000000..095ba44 --- /dev/null +++ b/src/main/java/com/example/demo/types/service/TypeService.java @@ -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 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); + } +} diff --git a/src/test/java/com/example/demo/GameServiceTests.java b/src/test/java/com/example/demo/GameServiceTests.java new file mode 100644 index 0000000..f15e220 --- /dev/null +++ b/src/test/java/com/example/demo/GameServiceTests.java @@ -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()); + } +} diff --git a/src/test/java/com/example/demo/TypeServiceTests.java b/src/test/java/com/example/demo/TypeServiceTests.java new file mode 100644 index 0000000..38014c2 --- /dev/null +++ b/src/test/java/com/example/demo/TypeServiceTests.java @@ -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()); + } +}