Compare commits

...

5 Commits
main ... lab2

33 changed files with 1444 additions and 88 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,15 +1,71 @@
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.service.GameService;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.genres.service.GenreService;
import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.service.OrderService;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService;
@SpringBootApplication
@RestController
@RequestMapping("/api")
public class DemoApplication {
public class DemoApplication implements CommandLineRunner {
private final TypeService typeService;
private final GenreService genreService;
private final GameService gameService;
private final OrderService orderService;
private final UserService userService;
public DemoApplication(TypeService typeService, GenreService genreService, GameService gameService, OrderService orderService, UserService userService){
this.typeService = typeService;
this.gameService = gameService;
this.genreService = genreService;
this.orderService = orderService;
this.userService = userService;
}
@Override
public void run(String... args) throws Exception{
final var type1 = typeService.create(new TypeEntity(null,"ААА"));
final var type2 = typeService.create(new TypeEntity(null,"АА"));
final var genre1 = genreService.create(new GenreEntity(null, "Приключения"));
final var genre2 = genreService.create(new GenreEntity(null, "Симулятор"));
final List<GenreEntity> genres1 = new ArrayList<GenreEntity>();
genres1.add(genre1);
genres1.add(genre2);
final List<GenreEntity> genres2 = new ArrayList<GenreEntity>();
genres2.add(genre2);
final var game1 = gameService.create(new GameEntity(null,type1,"Game1",2100.0,"good game", genres1));
final var game2 = gameService.create(new GameEntity(null, type2, "Game2", 1200.0,"bad game", genres2));
final List<GameEntity> games = new ArrayList<GameEntity>();
games.add(game1);
games.add(game2);
userService.create(new UserEntity(null, "login1", "email@mail.com", "qwerty123"));
userService.create(new UserEntity(null, "login2", "email@gmail.com", "qwerty1234"));
orderService.create(new OrderEntity(null, 1L,games));
orderService.create(new OrderEntity(null, 2L,games));
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);

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,86 @@
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.model.GenreEntity;
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);
var dto = new GameDto();
dto.setId(entity.getId());
dto.setGenres(entity.getGenres().stream().map(GenreEntity::getId).toList());
dto.setDescription(entity.getDescription());
dto.setName(entity.getName());
dto.setPrice(entity.getPrice());
dto.setTypeId(entity.getType().getId());
return dto;
}
private GameEntity toEntity(GameDto dto){
final GameEntity entity = modelMapper.map(dto, GameEntity.class);
entity.setType(typeService.get(dto.getTypeId()));
var genres = dto.getGenres();
for(var genre : genres){
entity.setGenres(genreService.get(genre));
}
return entity;
}
@GetMapping
public List<GameDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId, @RequestParam(name = "genres", defaultValue = "") List<Long> genres){
return gameService.getAll(typeId, genres).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,77 @@
package com.example.demo.games.api;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class GameDto {
private Long id;
@NotNull
@Min(1)
private Long typeId;
@NotNull
private final List<Long> genres = new ArrayList<>();
@NotNull
@Min(1)
private Double price;
@NotBlank
private String name;
@NotBlank
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 List<Long> getGenres(){
return genres;
}
public void setGenres(List<Long> genres){
this.genres.clear();
this.genres.addAll(genres);
}
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,91 @@
package com.example.demo.games.model;
import java.util.ArrayList;
import java.util.List;
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 final List<GenreEntity> genres = new ArrayList<>();
private TypeEntity type;
public GameEntity(){
super();
}
public GameEntity(Long id, TypeEntity type, String name, Double price, String description, List<GenreEntity> genres){
super(id);
this.type = type;
this.name = name;
this.price = price;
this.description = description;
this.genres.clear();
this.genres.addAll(genres);
}
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 List<GenreEntity> getGenres(){
return genres;
}
public void setGenres(GenreEntity genre){
this.genres.add(genre);
}
@Override
public int hashCode() {
return Objects.hash(id, type, price, genres, 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.getGenres(), genres)
&& 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,69 @@
package com.example.demo.games.service;
import java.util.ArrayList;
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;
import com.example.demo.genres.service.GenreService;
@Service
public class GameService {
private final GameRepository repository;
private final GenreService genreService;
public GameService(GameRepository repository, GenreService genreService){
this.repository = repository;
this.genreService = genreService;
}
public List<GameEntity> getAll(Long typeId, List<Long> genres){
var genresEnt = new ArrayList<>();
for(var genreid : genres){
genresEnt.add(genreService.get(genreid));
}
if(!Objects.equals(typeId, 0L) && !Objects.equals(genresEnt.size(), 0)){
return repository.getAll().stream().filter(game -> game.getType().getId().equals(typeId) && game.getGenres().containsAll(genresEnt)).toList();
}
if(Objects.equals(typeId, 0L) && !Objects.equals(genresEnt.size(), 0)){
return repository.getAll().stream().filter(game -> game.getGenres().containsAll(genresEnt)).toList();
}
if(!Objects.equals(typeId, 0L) && Objects.equals(genresEnt.size(), 0)){
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());
var genres = entity.getGenres();
for(var genre : genres){
existEntity.setGenres(genre);
}
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,80 @@
package com.example.demo.orders.api;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
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.orders.model.OrderEntity;
import com.example.demo.orders.service.OrderService;
import com.example.demo.users.service.UserService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/order")
public class OrderController {
private final GameService gameService;
private final ModelMapper modelMapper;
private final OrderService orderService;
//private final UserService userService;
public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService, UserService userService){
this.gameService = gameService;
this.modelMapper = modelMapper;
this.orderService = orderService;
//this.userService = userService;
}
private OrderDto toDto(OrderEntity entity){
var dto = new OrderDto();
dto.setId(entity.getId());
double sum = 0;
for(var game : entity.getGames()){
sum += game.getPrice();
}
dto.setUserId(entity.getUserId());
dto.setSum(sum);
dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList());
return dto;
}
private OrderEntity toEntity(OrderDto dto){
final OrderEntity entity = modelMapper.map(dto, OrderEntity.class);
var games = dto.getGames();
for(var game : games){
entity.setGames(gameService.get(game));
}
return entity;
}
@GetMapping
public List<OrderDto> getAll(@RequestParam(name = "userId", defaultValue = "") Long userId){
return orderService.getAll(userId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public OrderDto get(@PathVariable(name = "id")Long id){
return toDto(orderService.get(id));
}
@PostMapping
public OrderDto create(@RequestBody @Valid OrderDto dto){
return toDto(orderService.create(toEntity(dto)));
}
@DeleteMapping("/{id}")
public OrderDto delete(@PathVariable(name = "id") Long id){
return toDto(orderService.delete(id));
}
}

View File

@ -0,0 +1,53 @@
package com.example.demo.orders.api;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class OrderDto {
private Long id;
@NotNull
@Min(1)
private double sum;
@NotNull
@Min(1)
private Long userId;
@NotNull
private final List<Long> games = new ArrayList<>();
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public double getSum(){
return sum;
}
public void setSum(double sum){
this.sum = sum;
}
public List<Long> getGames(){
return games;
}
public void setGames(List<Long> games){
this.games.clear();
this.games.addAll(games);
}
public Long getUserId(){
return userId;
}
public void setUserId(Long userId){
this.userId = userId;
}
}

View File

@ -0,0 +1,66 @@
package com.example.demo.orders.model;
import java.util.List;
import java.util.Objects;
import java.util.ArrayList;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.games.model.GameEntity;
public class OrderEntity extends BaseEntity{
private double sum;
private Long userId;
private final List<GameEntity> games = new ArrayList<>();
public OrderEntity(){
super();
}
public OrderEntity(Long id, Long userId, List<GameEntity> games){
super(id);
this.userId = userId;
this.games.clear();
this.games.addAll(games);
}
public double getSum(){
for(var game : games){
sum += game.getPrice();
}
return sum;
}
public Long getUserId(){
return userId;
}
public void setUserId(Long userId){
this.userId = userId;
}
public List<GameEntity> getGames(){
return games;
}
public void setGames(GameEntity game){
this.games.add(game);
}
@Override
public int hashCode(){
return Objects.hash(id, sum, games);
}
@Override
public boolean equals(Object obj){
if(this == obj) return true;
if(obj == null || getClass() != obj.getClass()) return false;
final OrderEntity other = (OrderEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getSum(), sum)
&& Objects.equals(other.getGames(), games);
}
}

View File

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

View File

@ -0,0 +1,40 @@
package com.example.demo.orders.service;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.repository.OrderRepository;
@Service
public class OrderService {
private final OrderRepository repository;
public OrderService(OrderRepository repository){
this.repository = repository;
}
public List<OrderEntity> getAll(Long userId){
if(!Objects.equals(userId, null)){
return repository.getAll().stream().filter(user -> user.getId().equals(userId)).toList();
}
return repository.getAll();
}
public OrderEntity get(Long id){
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public OrderEntity create(OrderEntity entity){
return repository.create(entity);
}
public OrderEntity delete(Long id){
final OrderEntity exisEntity = get(id);
return repository.delete(exisEntity);
}
}

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,71 @@
package com.example.demo.users.api;
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 java.util.List;
import com.example.demo.core.configuration.Constants;
import com.example.demo.orders.service.OrderService;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL+"/user")
public class UserController {
private final ModelMapper modelMapper;
private final UserService userService;
public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){
this.modelMapper = modelMapper;
this.userService = userService;
}
private UserDto toDto(UserEntity entity){
var dto = new UserDto();
dto.setId(entity.getId());
dto.setLogin(entity.getLogin());
dto.setEmail(entity.getEmail());
dto.setPassword(entity.getPassword());
return dto;
}
private UserEntity toEntity(UserDto dto){
final UserEntity entity = modelMapper.map(dto, UserEntity.class);
return entity;
}
@GetMapping
public List<UserDto> getAll(){
return userService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public UserDto get(@PathVariable(name = "id") Long id){
return toDto(userService.get(id));
}
@PostMapping
public UserDto create(@RequestBody @Valid UserDto dto){
return toDto(userService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto){
return toDto(userService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public UserDto delete(@PathVariable(name = "id")Long id){
return toDto(userService.delete(id));
}
}

View File

@ -0,0 +1,44 @@
package com.example.demo.users.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
public class UserDto {
private Long id;
@NotBlank
private String login;
@NotBlank
private String email;
@NotBlank
private String password;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getLogin(){
return login;
}
public void setLogin(String login){
this.login = login;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}

View File

@ -0,0 +1,41 @@
package com.example.demo.users.model;
import com.example.demo.core.model.BaseEntity;
public class UserEntity extends BaseEntity{
private String login;
private String email;
private String password;
public UserEntity(){
super();
}
public UserEntity(Long id, String login, String email, String password){
super(id);
this.login = login;
this.email = email;
this.password = password;
}
public String getLogin(){
return login;
}
public void setLogin(String login){
this.login = login;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}

View File

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

View File

@ -0,0 +1,44 @@
package com.example.demo.users.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.users.model.UserEntity;
import com.example.demo.users.repository.UserRepository;
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository){
this.repository = repository;
}
public List<UserEntity> getAll(){
return repository.getAll().stream().toList();
}
public UserEntity get(Long id){
return Optional.ofNullable(repository.get(id)).orElseThrow(()-> new NotFoundException(id));
}
public UserEntity create(UserEntity entity){
return repository.create(entity);
}
public UserEntity update(Long id, UserEntity entity){
final UserEntity existEntity = get(id);
existEntity.setLogin(entity.getLogin());
existEntity.setEmail(entity.getEmail());
existEntity.setPassword(entity.getPassword());
return repository.update(existEntity);
}
public UserEntity delete(Long id){
final UserEntity existEntity = get(id);
return repository.delete(existEntity);
}
}

View File

@ -0,0 +1,62 @@
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
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;
import com.example.demo.genres.model.GenreEntity;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class GameServiceTests {
@Autowired
private GameService gameService;
List<GenreEntity> list = new ArrayList<GenreEntity>();
List<Long> list2 = new ArrayList<Long>();
@Test
void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
}
@Test
@Order(1)
void createTest(){
list.add(new GenreEntity(null, "приключения"));
list.add(new GenreEntity(null, "Симулятор"));
gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", list));
gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", list));
Assertions.assertEquals(4, gameService.getAll(0L,list2).size());
}
@Test
@Order(2)
void updateTest(){
final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", list);
final GameEntity updGame = gameService.update(2L, newGame);
Assertions.assertEquals(4, gameService.getAll(0L, list2).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(3L, gameService.getAll(0L,list2).size());
}
}

View File

@ -0,0 +1,52 @@
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
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.games.model.GameEntity;
import com.example.demo.games.service.GameService;
import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.service.OrderService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Autowired
private GameService gameService;
@Test
void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L));
}
@Test
@Order(1)
void createTest(){
final List<GameEntity> games = new ArrayList<GameEntity>();
games.add(gameService.get(1L));
orderService.create(new OrderEntity(null, 1L,games));
final var last = orderService.create(new OrderEntity(null, 2L,games));
Assertions.assertEquals(4, orderService.getAll(null).size());
Assertions.assertEquals(last, orderService.get(4L));
}
@Test
@Order(2)
void deleteTest(){
orderService.delete(2L);
Assertions.assertEquals(3, orderService.getAll(null).size());
final OrderEntity last = orderService.get(1L);
Assertions.assertEquals(1L, last.getId());
}
}

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(5, typeService.getAll().size());
Assertions.assertEquals(last, typeService.get(5L));
}
@Test
@Order(2)
void updateTest(){
final String test = "TEST";
final TypeEntity newEntity = typeService.update(5L, new TypeEntity(1L, test));
Assertions.assertEquals(5, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(5L));
Assertions.assertEquals(test, newEntity.getName());
}
@Test
@Order(3)
void deleteTest(){
typeService.delete(5L);
Assertions.assertEquals(4, typeService.getAll().size());
final TypeEntity last = typeService.get(4L);
Assertions.assertEquals(4L, last.getId());
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игра"));
Assertions.assertEquals(5, typeService.getAll().size());
Assertions.assertEquals(6L, newEntity.getId());
}
}

View File

@ -0,0 +1,48 @@
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.users.model.UserEntity;
import com.example.demo.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class UserServiceTest {
@Autowired
UserService userService;
@Test
void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
}
@Test
@Order(1)
void createTest(){
userService.create(new UserEntity(null, "login5", "gmail", "qwert123"));
final UserEntity last = userService.create(new UserEntity(null, "login6", "mail", "qwer123"));
Assertions.assertEquals(4, userService.getAll().size());
Assertions.assertEquals(last, userService.get(4L));
}
@Test
@Order(2)
void updateTest(){
userService.update(4L, new UserEntity(null,"login","email", "qwe123"));
Assertions.assertEquals(4, userService.getAll().size());
}
@Test
@Order(3)
void deleteTest(){
userService.delete(2L);
Assertions.assertEquals(3, userService.getAll().size());
}
}