This commit is contained in:
bekodeg 2024-05-21 11:24:10 +04:00
parent 9ecf0abda5
commit 0d207ca4ac
8 changed files with 273 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package com.example.demo.games.api;
import com.example.demo.users.api.UserDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
@ -9,7 +10,6 @@ public class GameDTO {
@NotNull
@Min(2)
private String name;
public String getName() {
return name;
}

View File

@ -33,7 +33,7 @@ public class GameService {
public GameEntity update(long id, GameEntity entity){
final GameEntity existEntity = get(id);
existEntity.setName(entity.getName());
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName());
return repository.update(existEntity);
}

View File

@ -0,0 +1,58 @@
package com.example.demo.tables.api;
import com.example.demo.core.config.Constants;
import com.example.demo.tables.model.TableEntity;
import com.example.demo.tables.service.TableService;
import jakarta.validation.Valid;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(Constants.API_URL + "/tables")
public class TableController {
private final TableService tableService;
private final ModelMapper modelMapper;
public TableController(TableService tableService, ModelMapper modelMapper){
this.tableService = tableService;
this.modelMapper = modelMapper;
}
private TableDTO toDTO(TableEntity entity) {
return modelMapper.map(entity, TableDTO.class);
}
private TableEntity toEntity(TableDTO dto){
return modelMapper.map(dto, TableEntity.class);
}
@GetMapping
public List<TableDTO> getAll()
{
return tableService.getAll().stream().map(this::toDTO).toList();
}
@GetMapping("/{id}")
public TableDTO get(@PathVariable(name = "id") long id)
{
return toDTO(tableService.get(id));
}
@PostMapping("/")
public TableDTO create(@RequestBody @Valid TableDTO dto) {
return toDTO(tableService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) {
return toDTO(tableService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public TableDTO delete(@PathVariable(name = "id") long id)
{
return toDTO(tableService.delete(id));
}
}

View File

@ -0,0 +1,67 @@
package com.example.demo.tables.api;
import com.example.demo.games.api.GameDTO;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.repository.GameRepository;
import com.example.demo.users.api.UserDTO;
import com.example.demo.users.model.UserEntity;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
public class TableDTO {
private Long id;
private String description;
@NotNull
private Date date;
@NotNull
private UserDTO creator;
@NotNull
private GameDTO game;
private List<UserDTO> gamers;
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public Date getDate(){
return date;
}
public void setDate(Date date){
this.date = date;
}
public UserDTO geyCreator(){
return creator;
}
public void setCreator(UserDTO creator){
this.creator = creator;
}
public GameDTO getGame(){
return game;
}
public void setGame(GameDTO game){
this.game = game;
}
public List<UserDTO> getGamers(){
return gamers;
}
public void setGamers(List<UserDTO> gamers){
this.gamers = gamers;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -0,0 +1,61 @@
package com.example.demo.tables.model;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.games.model.GameEntity;
import com.example.demo.users.model.UserEntity;
import java.time.Period;
import java.util.Date;
import java.util.List;
public class TableEntity extends BaseEntity {
private String description;
private Date date;
private UserEntity creator;
private GameEntity game;
private List<UserEntity> gamers;
public TableEntity() {
super();
}
public TableEntity(String description, Date date,
UserEntity creator, GameEntity game, List<UserEntity> users){
this.description = description;
this.date = date;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public Date getDate(){
return date;
}
public void setDate(Date date){
this.date = date;
}
public UserEntity geyCreator(){
return creator;
}
public void setCreator(UserEntity creator){
this.creator = creator;
}
public GameEntity getGame(){
return game;
}
public void setGame(GameEntity game){
this.game = game;
}
public List<UserEntity> getGamers(){
return gamers;
}
public void setGamers(List<UserEntity> gamers){
this.gamers = gamers;
}
}

View File

@ -0,0 +1,10 @@
package com.example.demo.tables.repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.tables.model.TableEntity;
import org.springframework.stereotype.Repository;
@Repository
public class TableRepository extends MapRepository<TableEntity> {
}

View File

@ -0,0 +1,68 @@
package com.example.demo.tables.service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.service.GameService;
import com.example.demo.tables.model.TableEntity;
import com.example.demo.tables.repository.TableRepository;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService;
import jakarta.validation.OverridesAttribute;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@Service
public class TableService {
private final TableRepository repository;
private final UserService userService;
private final GameService gameService;
public TableService(TableRepository repository, UserService userService, GameService gameService)
{
this.repository = repository;
this.userService = userService;
this.gameService = gameService;
}
public List<TableEntity> getAll(){
return repository.getAll();
}
public TableEntity get(Long id){
return Optional
.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
}
public TableEntity create(TableEntity entity){
return repository.create(entity);
}
public TableEntity update(Long id, TableEntity entity){
final TableEntity existEntity = get(id);
existEntity.setDate(entity.getDate() == null ? existEntity.getDate() : entity.getDate());
existEntity.setDescription(
entity.getDescription().isEmpty() ? existEntity.getDescription() : entity.getDescription()
);
existEntity.setGamers(
entity.getGamers().isEmpty() ?
existEntity.getGamers() :
entity.getGamers()
.stream()
.map(g -> userService.get(g.getId()))
.toList()
);
existEntity.setGame(
entity.getGame().getId() == null ?
existEntity.getGame() :
(gameService.get(entity.getGame().getId())));
return repository.update(existEntity);
}
public TableEntity delete(Long id){
return repository.delete(repository.get(id));
}
}

View File

@ -21,7 +21,7 @@ public class UserService {
return repository.getAll();
}
public UserEntity get(long id){
public UserEntity get(Long id){
return Optional
.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
@ -30,13 +30,13 @@ public class UserService {
return repository.create(entity);
}
public UserEntity update(long id, UserEntity entity){
public UserEntity update(Long id, UserEntity entity){
final UserEntity existEntity = get(id);
existEntity.setName(entity.getName());
if (!Objects.equals(entity.getLogin(), "") && !Objects.equals(entity.getPassword(), "")){
existEntity.setLogin(entity.getLogin());
existEntity.setPassword(entity.getPassword());
}
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName());
existEntity.setLogin(entity.getLogin().isEmpty() ? existEntity.getLogin() : entity.getLogin());
existEntity.setPassword(entity.getPassword().isEmpty() ? existEntity.getPassword() : entity.getPassword());
return repository.update(existEntity);
}