добавил заказы
This commit is contained in:
parent
ff3038573e
commit
f0d47b6feb
@ -13,6 +13,8 @@ import com.example.demo.games.model.GameEntity;
|
|||||||
import com.example.demo.games.service.GameService;
|
import com.example.demo.games.service.GameService;
|
||||||
import com.example.demo.genres.model.GenreEntity;
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
import com.example.demo.genres.service.GenreService;
|
import com.example.demo.genres.service.GenreService;
|
||||||
|
import com.example.demo.order.model.OrderEntity;
|
||||||
|
import com.example.demo.order.service.OrderService;
|
||||||
import com.example.demo.types.model.TypeEntity;
|
import com.example.demo.types.model.TypeEntity;
|
||||||
import com.example.demo.types.service.TypeService;
|
import com.example.demo.types.service.TypeService;
|
||||||
|
|
||||||
@ -24,11 +26,13 @@ public class DemoApplication implements CommandLineRunner {
|
|||||||
private final TypeService typeService;
|
private final TypeService typeService;
|
||||||
private final GenreService genreService;
|
private final GenreService genreService;
|
||||||
private final GameService gameService;
|
private final GameService gameService;
|
||||||
|
private final OrderService orderService;
|
||||||
|
|
||||||
public DemoApplication(TypeService typeService, GenreService genreService, GameService gameService){
|
public DemoApplication(TypeService typeService, GenreService genreService, GameService gameService, OrderService orderService){
|
||||||
this.typeService = typeService;
|
this.typeService = typeService;
|
||||||
this.gameService = gameService;
|
this.gameService = gameService;
|
||||||
this.genreService = genreService;
|
this.genreService = genreService;
|
||||||
|
this.orderService = orderService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,8 +50,13 @@ public class DemoApplication implements CommandLineRunner {
|
|||||||
final List<GenreEntity> genres2 = new ArrayList<GenreEntity>();
|
final List<GenreEntity> genres2 = new ArrayList<GenreEntity>();
|
||||||
genres2.add(genre2);
|
genres2.add(genre2);
|
||||||
|
|
||||||
gameService.create(new GameEntity(null,type1,"Game1",2100.0,"good game", genres1));
|
final var game1 = gameService.create(new GameEntity(null,type1,"Game1",2100.0,"good game", genres1));
|
||||||
gameService.create(new GameEntity(null, type2, "Game2", 1200.0,"bad game", genres2));
|
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);
|
||||||
|
|
||||||
|
orderService.create(new OrderEntity(null,games));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,82 @@
|
|||||||
|
package com.example.demo.order.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.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.order.model.OrderEntity;
|
||||||
|
import com.example.demo.order.service.OrderService;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService){
|
||||||
|
this.gameService = gameService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
this.orderService = orderService;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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 = "games", defaultValue = "") List<Long> games){
|
||||||
|
return orderService.getAll(games).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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public OrderDto update(@PathVariable(name = "id") Long id, @RequestBody OrderDto dto){
|
||||||
|
return toDto(orderService.update(id, toEntity(dto)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public OrderDto delete(@PathVariable(name = "id") Long id){
|
||||||
|
return toDto(orderService.delete(id));
|
||||||
|
}
|
||||||
|
}
|
42
src/main/java/com/example/demo/order/api/OrderDto.java
Normal file
42
src/main/java/com/example/demo/order/api/OrderDto.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package com.example.demo.order.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
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
58
src/main/java/com/example/demo/order/model/OrderEntity.java
Normal file
58
src/main/java/com/example/demo/order/model/OrderEntity.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package com.example.demo.order.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 final List<GameEntity> games = new ArrayList<>();
|
||||||
|
|
||||||
|
public OrderEntity(){
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderEntity(Long id, List<GameEntity> games){
|
||||||
|
super(id);
|
||||||
|
this.games.clear();
|
||||||
|
this.games.addAll(games);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getSum(){
|
||||||
|
|
||||||
|
for(var game : games){
|
||||||
|
sum += game.getPrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.demo.order.repository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.example.demo.core.repository.MapRepository;
|
||||||
|
import com.example.demo.order.model.OrderEntity;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
|
||||||
|
public class OrderRepository extends MapRepository<OrderEntity>{
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.example.demo.order.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.games.service.GameService;
|
||||||
|
import com.example.demo.order.repository.OrderRepository;
|
||||||
|
import com.example.demo.order.model.OrderEntity;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class OrderService {
|
||||||
|
private final OrderRepository repository;
|
||||||
|
private final GameService gameService;
|
||||||
|
|
||||||
|
public OrderService(OrderRepository repository, GameService gameService){
|
||||||
|
this.repository = repository;
|
||||||
|
this.gameService = gameService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderEntity> getAll(List<Long> games){
|
||||||
|
var gamesEnt = new ArrayList<>();
|
||||||
|
for(var gameId : games){
|
||||||
|
gamesEnt.add(gameService.get(gameId));
|
||||||
|
}
|
||||||
|
if(!Objects.equals(gamesEnt.size(), 0)){
|
||||||
|
return repository.getAll().stream().filter(game -> game.getGames().containsAll(gamesEnt)).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 update(Long id, OrderEntity entity){
|
||||||
|
final OrderEntity existEntity = get(id);
|
||||||
|
var games = entity.getGames();
|
||||||
|
for(var game : games){
|
||||||
|
existEntity.setGames(game);
|
||||||
|
}
|
||||||
|
return repository.update(existEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderEntity delete(Long id){
|
||||||
|
final OrderEntity exisEntity = get(id);
|
||||||
|
return repository.delete(exisEntity);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user