Готовая 2 лаба

This commit is contained in:
Максим Яковлев 2024-04-13 14:01:48 +04:00
parent 4d5ff0399a
commit 3c6cc35264
13 changed files with 181 additions and 97 deletions

View File

@ -60,9 +60,11 @@ public class DemoApplication implements CommandLineRunner {
games.add(game1); games.add(game1);
games.add(game2); games.add(game2);
final var order = orderService.create(new OrderEntity(null,games)); userService.create(new UserEntity(null, "login1", "email@mail.com", "qwerty123"));
userService.create(new UserEntity(null, "login2", "email@gmail.com", "qwerty1234"));
userService.create(new UserEntity(null, order, "login1", "email@mail.com", "qwerty123")); orderService.create(new OrderEntity(null, 1L,games));
orderService.create(new OrderEntity(null, 2L,games));
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -18,6 +17,7 @@ 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.orders.model.OrderEntity; import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.service.OrderService; import com.example.demo.orders.service.OrderService;
import com.example.demo.users.service.UserService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -27,11 +27,13 @@ public class OrderController {
private final GameService gameService; private final GameService gameService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
private final OrderService orderService; private final OrderService orderService;
//private final UserService userService;
public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService){ public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService, UserService userService){
this.gameService = gameService; this.gameService = gameService;
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.orderService = orderService; this.orderService = orderService;
//this.userService = userService;
} }
private OrderDto toDto(OrderEntity entity){ private OrderDto toDto(OrderEntity entity){
@ -41,6 +43,7 @@ public class OrderController {
for(var game : entity.getGames()){ for(var game : entity.getGames()){
sum += game.getPrice(); sum += game.getPrice();
} }
dto.setUserId(entity.getUserId());
dto.setSum(sum); dto.setSum(sum);
dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList()); dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList());
return dto; return dto;
@ -56,8 +59,8 @@ public class OrderController {
} }
@GetMapping @GetMapping
public List<OrderDto> getAll(@RequestParam(name = "games", defaultValue = "") List<Long> games){ public List<OrderDto> getAll(@RequestParam(name = "userId", defaultValue = "") Long userId){
return orderService.getAll(games).stream().map(this::toDto).toList(); return orderService.getAll(userId).stream().map(this::toDto).toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -70,11 +73,6 @@ public class OrderController {
return toDto(orderService.create(toEntity(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}") @DeleteMapping("/{id}")
public OrderDto delete(@PathVariable(name = "id") Long id){ public OrderDto delete(@PathVariable(name = "id") Long id){
return toDto(orderService.delete(id)); return toDto(orderService.delete(id));

View File

@ -14,6 +14,9 @@ public class OrderDto {
@Min(1) @Min(1)
private double sum; private double sum;
@NotNull @NotNull
@Min(1)
private Long userId;
@NotNull
private final List<Long> games = new ArrayList<>(); private final List<Long> games = new ArrayList<>();
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
@ -39,4 +42,12 @@ public class OrderDto {
this.games.clear(); this.games.clear();
this.games.addAll(games); this.games.addAll(games);
} }
public Long getUserId(){
return userId;
}
public void setUserId(Long userId){
this.userId = userId;
}
} }

View File

@ -11,14 +11,16 @@ import com.example.demo.games.model.GameEntity;
public class OrderEntity extends BaseEntity{ public class OrderEntity extends BaseEntity{
private double sum; private double sum;
private Long userId;
private final List<GameEntity> games = new ArrayList<>(); private final List<GameEntity> games = new ArrayList<>();
public OrderEntity(){ public OrderEntity(){
super(); super();
} }
public OrderEntity(Long id, List<GameEntity> games){ public OrderEntity(Long id, Long userId, List<GameEntity> games){
super(id); super(id);
this.userId = userId;
this.games.clear(); this.games.clear();
this.games.addAll(games); this.games.addAll(games);
} }
@ -32,10 +34,16 @@ public class OrderEntity extends BaseEntity{
return sum; return sum;
} }
public Long getUserId(){
return userId;
}
public void setUserId(Long userId){
this.userId = userId;
}
public List<GameEntity> getGames(){ public List<GameEntity> getGames(){
return games; return games;
} }
public void setGames(GameEntity game){ public void setGames(GameEntity game){
this.games.add(game); this.games.add(game);
} }

View File

@ -3,32 +3,24 @@ package com.example.demo.orders.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.service.GameService;
import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.repository.OrderRepository; import com.example.demo.orders.repository.OrderRepository;
@Service @Service
public class OrderService { public class OrderService {
private final OrderRepository repository; private final OrderRepository repository;
private final GameService gameService;
public OrderService(OrderRepository repository, GameService gameService){ public OrderService(OrderRepository repository){
this.repository = repository; this.repository = repository;
this.gameService = gameService;
} }
public List<OrderEntity> getAll(List<Long> games){ public List<OrderEntity> getAll(Long userId){
var gamesEnt = new ArrayList<>(); if(!Objects.equals(userId, null)){
for(var gameId : games){ return repository.getAll().stream().filter(user -> user.getId().equals(userId)).toList();
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(); return repository.getAll();
} }
@ -41,15 +33,6 @@ public class OrderService {
return repository.create(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){ public OrderEntity delete(Long id){
final OrderEntity exisEntity = get(id); final OrderEntity exisEntity = get(id);
return repository.delete(exisEntity); return repository.delete(exisEntity);

View File

@ -22,13 +22,11 @@ import jakarta.validation.Valid;
@RestController @RestController
@RequestMapping(Constants.API_URL+"/user") @RequestMapping(Constants.API_URL+"/user")
public class UserController { public class UserController {
private final OrderService orderService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
private final UserService userService; private final UserService userService;
public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){ public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.orderService = orderService;
this.userService = userService; this.userService = userService;
} }
@ -38,13 +36,11 @@ public class UserController {
dto.setLogin(entity.getLogin()); dto.setLogin(entity.getLogin());
dto.setEmail(entity.getEmail()); dto.setEmail(entity.getEmail());
dto.setPassword(entity.getPassword()); dto.setPassword(entity.getPassword());
dto.setOrderId(entity.getOrder().getId());
return dto; return dto;
} }
private UserEntity toEntity(UserDto dto){ private UserEntity toEntity(UserDto dto){
final UserEntity entity = modelMapper.map(dto, UserEntity.class); final UserEntity entity = modelMapper.map(dto, UserEntity.class);
entity.setOrder(orderService.get(dto.getOrderId()));
return entity; return entity;
} }

View File

@ -2,9 +2,7 @@ package com.example.demo.users.api;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class UserDto { public class UserDto {
private Long id; private Long id;
@ -14,9 +12,6 @@ public class UserDto {
private String email; private String email;
@NotBlank @NotBlank
private String password; private String password;
@NotNull
@Min(1)
private Long orderId;
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){ public Long getId(){
@ -46,11 +41,4 @@ public class UserDto {
public void setPassword(String password){ public void setPassword(String password){
this.password = password; this.password = password;
} }
public Long getOrderId(){
return orderId;
}
public void setOrderId(Long orderId){
this.orderId = orderId;
}
} }

View File

@ -1,24 +1,21 @@
package com.example.demo.users.model; package com.example.demo.users.model;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.orders.model.OrderEntity;
public class UserEntity extends BaseEntity{ public class UserEntity extends BaseEntity{
private String login; private String login;
private String email; private String email;
private String password; private String password;
private OrderEntity order;
public UserEntity(){ public UserEntity(){
super(); super();
} }
public UserEntity(Long id, OrderEntity order, String login, String email, String password){ public UserEntity(Long id, String login, String email, String password){
super(id); super(id);
this.login = login; this.login = login;
this.email = email; this.email = email;
this.password = password; this.password = password;
this.order = order;
} }
public String getLogin(){ public String getLogin(){
@ -41,11 +38,4 @@ public class UserEntity extends BaseEntity{
public void setPassword(String password){ public void setPassword(String password){
this.password = password; this.password = password;
} }
public OrderEntity getOrder(){
return order;
}
public void setOrder(OrderEntity order){
this.order = order;
}
} }

View File

@ -34,7 +34,6 @@ public class UserService {
existEntity.setLogin(entity.getLogin()); existEntity.setLogin(entity.getLogin());
existEntity.setEmail(entity.getEmail()); existEntity.setEmail(entity.getEmail());
existEntity.setPassword(entity.getPassword()); existEntity.setPassword(entity.getPassword());
existEntity.setOrder(entity.getOrder());
return repository.update(existEntity); return repository.update(existEntity);
} }

View File

@ -1,5 +1,8 @@
package com.example.demo; package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@ -11,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.model.GameEntity; 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;
@SpringBootTest @SpringBootTest
@ -18,36 +22,41 @@ import com.example.demo.games.service.GameService;
class GameServiceTests { class GameServiceTests {
@Autowired @Autowired
private GameService gameService; private GameService gameService;
List<GenreEntity> list = new ArrayList<GenreEntity>();
List<Long> list2 = new ArrayList<Long>();
@Test @Test
void getTest(){ void getTest(){
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L)); Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
} }
// @Test @Test
// @Order(1) @Order(1)
// void createTest(){ 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)); list.add(new GenreEntity(null, "приключения"));
// Assertions.assertEquals(2, gameService.getAll(0L,0L).size()); 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 @Test
// @Order(2) @Order(2)
// void updateTest(){ void updateTest(){
// final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", null); final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", list);
// final GameEntity updGame = gameService.update(2L, newGame); final GameEntity updGame = gameService.update(2L, newGame);
// Assertions.assertEquals(2, gameService.getAll(0L, 0L).size()); Assertions.assertEquals(4, gameService.getAll(0L, list2).size());
// Assertions.assertEquals(updGame, gameService.get(2L)); Assertions.assertEquals(updGame, gameService.get(2L));
// Assertions.assertEquals(newGame.getName(), updGame.getName()); Assertions.assertEquals(newGame.getName(), updGame.getName());
// Assertions.assertEquals(newGame.getDescription(), updGame.getDescription()); Assertions.assertEquals(newGame.getDescription(), updGame.getDescription());
// Assertions.assertEquals(newGame.getPrice(), updGame.getPrice()); Assertions.assertEquals(newGame.getPrice(), updGame.getPrice());
// } }
// @Test @Test
// @Order(3) @Order(3)
// void deleteTest(){ void deleteTest(){
// gameService.delete(2L); gameService.delete(2L);
// Assertions.assertEquals(1L, gameService.getAll(0L,0L).size()); 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

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