добавил пользователей
This commit is contained in:
parent
f0d47b6feb
commit
4d5ff0399a
@ -13,10 +13,12 @@ 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.order.model.OrderEntity;
|
||||
import com.example.demo.order.service.OrderService;
|
||||
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
|
||||
@ -27,12 +29,14 @@ public class DemoApplication implements CommandLineRunner {
|
||||
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){
|
||||
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
|
||||
@ -56,7 +60,9 @@ public class DemoApplication implements CommandLineRunner {
|
||||
games.add(game1);
|
||||
games.add(game2);
|
||||
|
||||
orderService.create(new OrderEntity(null,games));
|
||||
final var order = orderService.create(new OrderEntity(null,games));
|
||||
|
||||
userService.create(new UserEntity(null, order, "login1", "email@mail.com", "qwerty123"));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.order.api;
|
||||
package com.example.demo.orders.api;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@ -16,8 +16,8 @@ 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 com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.service.OrderService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.order.api;
|
||||
package com.example.demo.orders.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.order.model;
|
||||
package com.example.demo.orders.model;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.example.demo.order.repository;
|
||||
package com.example.demo.orders.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.order.model.OrderEntity;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
|
||||
@Repository
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo.order.service;
|
||||
package com.example.demo.orders.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -9,8 +9,8 @@ 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;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.repository.OrderRepository;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
75
src/main/java/com/example/demo/users/api/UserController.java
Normal file
75
src/main/java/com/example/demo/users/api/UserController.java
Normal file
@ -0,0 +1,75 @@
|
||||
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 OrderService orderService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){
|
||||
this.modelMapper = modelMapper;
|
||||
this.orderService = orderService;
|
||||
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());
|
||||
dto.setOrderId(entity.getOrder().getId());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDto dto){
|
||||
final UserEntity entity = modelMapper.map(dto, UserEntity.class);
|
||||
entity.setOrder(orderService.get(dto.getOrderId()));
|
||||
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));
|
||||
}
|
||||
}
|
56
src/main/java/com/example/demo/users/api/UserDto.java
Normal file
56
src/main/java/com/example/demo/users/api/UserDto.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String login;
|
||||
@NotBlank
|
||||
private String email;
|
||||
@NotBlank
|
||||
private String password;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long orderId;
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
public Long getOrderId(){
|
||||
return orderId;
|
||||
}
|
||||
public void setOrderId(Long orderId){
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
51
src/main/java/com/example/demo/users/model/UserEntity.java
Normal file
51
src/main/java/com/example/demo/users/model/UserEntity.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.example.demo.users.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
|
||||
public class UserEntity extends BaseEntity{
|
||||
private String login;
|
||||
private String email;
|
||||
private String password;
|
||||
private OrderEntity order;
|
||||
|
||||
public UserEntity(){
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, OrderEntity order, String login, String email, String password){
|
||||
super(id);
|
||||
this.login = login;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public OrderEntity getOrder(){
|
||||
return order;
|
||||
}
|
||||
public void setOrder(OrderEntity order){
|
||||
this.order = order;
|
||||
}
|
||||
}
|
@ -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> {
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
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());
|
||||
existEntity.setOrder(entity.getOrder());
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public UserEntity delete(Long id){
|
||||
final UserEntity existEntity = get(id);
|
||||
return repository.delete(existEntity);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user