написал контроллеры

This commit is contained in:
bekodeg 2024-06-02 21:56:28 +04:00
parent 3a149f97fa
commit e58d7117cf
10 changed files with 237 additions and 246 deletions

View File

@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping(Constants.API_URL + "/games") @RequestMapping(Constants.API_URL + "game")
public class GameController { public class GameController {
private final GameService gameService; private final GameService gameService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
@ -20,39 +20,42 @@ public class GameController {
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
} }
private GameDTO toDTO(GameEntity entity){ private GameDto toDto(GameEntity entity){
return modelMapper.map(entity, GameDTO.class); return modelMapper.map(entity, GameDto.class);
} }
private GameEntity toEntity(GameDTO dto){ private GameEntity toEntity(GameDto dto){
return modelMapper.map(dto, GameEntity.class); return modelMapper.map(dto, GameEntity.class);
} }
@GetMapping @GetMapping
public List<GameDTO> getAll() public List<GameDto> getAll() {
{ return gameService.getAll().stream().map(this::toDto).toList();
return gameService.getAll().stream().map(this::toDTO).toList(); }
@GetMapping("/user/{userId}")
public List<GameDto> getByUser(@PathVariable Long userId) {
return gameService.getByUser(userId).stream().map(this::toDto).toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public GameDTO get(@PathVariable(name = "id") long id) public GameDto get(@PathVariable(name = "id") long id) {
{ return toDto(gameService.get(id));
return toDTO(gameService.get(id));
} }
@PostMapping("/") @PostMapping
public GameDTO create(@RequestBody @Valid GameDTO dto) { public GameDto create(@RequestBody @Valid GameDto dto, @RequestParam Long userId) {
return toDTO(gameService.create(toEntity(dto))); return toDto(gameService.create(userId, toEntity(dto)));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public GameDTO update(@PathVariable(name = "id") long id, @RequestBody GameDTO dto) { public GameDto update(@PathVariable(name = "id") long id, @RequestBody GameDto dto) {
return toDTO(gameService.update(id, toEntity(dto))); return toDto(gameService.update(id, toEntity(dto)));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public GameDTO delete(@PathVariable(name = "id") long id) public GameDto delete(@PathVariable(name = "id") long id)
{ {
return toDTO(gameService.delete(id)); return toDto(gameService.delete(id));
} }
} }

View File

@ -1,25 +0,0 @@
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;
public class GameDTO {
private Long id;
@NotNull
@Min(2)
private String name;
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -0,0 +1,54 @@
package com.example.demo.games.api;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.util.List;
public class GameDto {
private Long id;
@NotNull
@Min(2)
private String name;
private String description;
private Long user;
private List<Long> plays;
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 Long getUser(){
return user;
}
public void setUser(Long user){
this.user = user;
}
public List<Long> getPlays(){
return plays;
}
public void setPlays(List<Long> plays){
this.plays = plays;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -0,0 +1,63 @@
package com.example.demo.plays.api;
import com.example.demo.core.config.Constants;
import com.example.demo.games.api.GameDto;
import com.example.demo.games.model.GameEntity;
import com.example.demo.plays.model.PlayEntity;
import com.example.demo.plays.service.PlayService;
import com.example.demo.users.api.UserDto;
import com.example.demo.users.model.UserEntity;
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 PlayController {
private final PlayService playService;
private final ModelMapper modelMapper;
public PlayController(PlayService playService, ModelMapper modelMapper){
this.playService = playService;
this.modelMapper = modelMapper;
}
private PlayDto toDto(PlayEntity entity) {
return modelMapper.map(entity, PlayDto.class);
}
private PlayEntity toEntity(PlayDto dto){
return modelMapper.map(dto, PlayEntity.class);
}
@GetMapping
public List<PlayDto> getAll()
{
return playService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public PlayDto get(@PathVariable(name = "id") long id)
{
return toDto(playService.get(id));
}
@PostMapping("/")
public PlayDto create(@RequestBody @Valid PlayDto dto) {
return toDto(playService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public PlayDto update(@PathVariable(name = "id") long id, @RequestBody PlayDto dto) {
return toDto(playService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public PlayDto delete(@PathVariable(name = "id") long id)
{
return toDto(playService.delete(id));
}
}

View File

@ -0,0 +1,55 @@
package com.example.demo.plays.api;
import jakarta.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
public class PlayDto {
private Long id;
private String description;
@NotNull
private Date date;
@NotNull
private Long game;
private List<Long> users;
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 Long getGame(){
return game;
}
public void setGame(Long game){
this.game = game;
}
public List<Long> getUsers(){
return users;
}
public void setUsers(List<Long> users){
this.users = users;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -1,74 +0,0 @@
package com.example.demo.plays.api;
import com.example.demo.core.config.Constants;
import com.example.demo.games.api.GameDTO;
import com.example.demo.games.model.GameEntity;
import com.example.demo.plays.model.PlayEntity;
import com.example.demo.plays.service.PlayService;
import com.example.demo.users.api.UserDTO;
import com.example.demo.users.model.UserEntity;
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 PlayService playService;
private final ModelMapper modelMapper;
public TableController(PlayService playService, ModelMapper modelMapper){
this.playService = playService;
this.modelMapper = modelMapper;
}
private TableDTO toDTO(PlayEntity entity) {
return new TableDTO(
entity.getId(),
entity.getDescription(),
entity.getDate(),
modelMapper.map(entity.getCreator(), UserDTO.class),
modelMapper.map(entity.getGame(), GameDTO.class),
entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList()
);
}
private PlayEntity toEntity(TableDTO dto){
PlayEntity entity = modelMapper.map(dto, PlayEntity.class);
entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class));
entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class));
entity.setGamers(dto.getGamers().stream()
.map(u -> modelMapper.map(u, UserEntity.class)).toList());
return entity;
}
@GetMapping
public List<TableDTO> getAll()
{
return playService.getAll().stream().map(this::toDTO).toList();
}
@GetMapping("/{id}")
public TableDTO get(@PathVariable(name = "id") long id)
{
return toDTO(playService.get(id));
}
@PostMapping("/")
public TableDTO create(@RequestBody @Valid TableDTO dto) {
return toDTO(playService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) {
return toDTO(playService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public TableDTO delete(@PathVariable(name = "id") long id)
{
return toDTO(playService.delete(id));
}
}

View File

@ -1,70 +0,0 @@
package com.example.demo.plays.api;
import com.example.demo.games.api.GameDTO;
import com.example.demo.users.api.UserDTO;
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 TableDTO(Long id, String description, Date date, UserDTO creator, GameDTO game, List<UserDTO> gamers){
this.id = id;
this.description = description;
this.date = date;
this.creator = creator;
this.game = game;
this.gamers = 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 getCreator(){
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;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -51,18 +51,18 @@ public class PlayService {
} }
@Transactional @Transactional
public PlayEntity create(long gameId, Set<Long> usersId, PlayEntity entity){ public PlayEntity create(PlayEntity entity){
if (entity == null) { if (entity == null) {
throw new IllegalArgumentException("Entity is null"); throw new IllegalArgumentException("Entity is null");
} }
final GameEntity existGame = gameService.get(gameId); final GameEntity existGame = gameService.get(entity.getGame().getId());
entity.setGame(existGame); entity.setGame(existGame);
Set<UserEntity> users = new HashSet<UserEntity>() { Set<UserEntity> users = new HashSet<UserEntity>() {
}; };
usersId.forEach(userId -> { entity.getUsers().forEach(user -> {
final UserEntity existUser = userService.get(userId); final UserEntity existUser = userService.get(user.getId());
users.add(existUser); users.add(existUser);
}); });

View File

@ -20,81 +20,42 @@ public class UserController {
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
} }
private UserDTO toDTO(UserEntity entity){ private UserDto toDto(UserEntity entity) {
UserDTO dto = new UserDTO(); return modelMapper.map(entity, UserDto.class);
dto.setId(entity.getId());
dto.setName(entity.getName());
dto.setLogin(entity.getLogin());
dto.setPassword(entity.getPassword());
return dto;
} }
private UserEntity toEntity(UserDTO dto){ private UserEntity toEntity(UserDto dto) {
return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword()); return modelMapper.map(dto, UserEntity.class);
} }
@GetMapping @GetMapping
public List<UserDTO> getAll() public List<UserDto> getAll()
{ {
return userService.getAll() return userService.getAll()
.stream() .stream()
.map(this::toDTO) .map(this::toDto)
.toList(); .toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public UserDTO get(@PathVariable(name = "id") long id) public UserDto get(@PathVariable(name = "id") long id) {
{ return toDto(userService.get(id));
return toDTO(userService.get(id));
} }
@PostMapping @PostMapping
public UserDTO create(@RequestBody @Valid UserDTO dto) { public UserDto create(@RequestBody @Valid UserDto dto) {
return toDTO(userService.create(toEntity(dto))); return toDto(userService.create(toEntity(dto)));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public UserDTO update( public UserDto update(
@PathVariable(name = "id") long id, @PathVariable(name = "id") long id,
@RequestBody UserDTO dto) { @RequestBody UserDto dto) {
return toDTO(userService.update(id, toEntity(dto))); return toDto(userService.update(id, toEntity(dto)));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public UserDTO delete(@PathVariable(name = "id") long id) public UserDto delete(@PathVariable(name = "id") long id) {
{ return toDto(userService.delete(id));
return toDTO(userService.delete(id));
}
@GetMapping("/{id}/subscription")
public List<SubscriptionDto> getUserSubscriptions(@PathVariable(name = "id") Long id) {
return userService.getUserSubscriptions(id).stream()
.map(this::toSubscriptionDto)
.toList();
}
@PostMapping("/{id}/subscription")
public List<SubscriptionDto> enableUserSubscriptions(
@PathVariable(name = "id") Long id,
@RequestParam(name = "subscriptions", defaultValue = "") List<Long> subscriptionsIds) {
return userService.enableUserSubscriptions(id, subscriptionsIds).stream()
.map(this::toSubscriptionDto)
.toList();
}
@DeleteMapping("/{id}/subscription")
public List<SubscriptionDto> disableUserSubscriptions(
@PathVariable(name = "id") Long id,
@RequestParam(name = "subscriptions", defaultValue = "") List<Long> subscriptionsIds) {
return userService.disableUserSubscriptions(id, subscriptionsIds).stream()
.map(this::toSubscriptionDto)
.toList();
}
@DeleteMapping("/{id}/subscription/all")
public List<SubscriptionDto> deleteAllUserSubscriptions(@PathVariable(name = "id") Long id) {
return userService.deleteAllUserSubscriptions(id).stream()
.map(this::toSubscriptionDto)
.toList();
} }
} }

View File

@ -1,22 +1,31 @@
package com.example.demo.users.api; package com.example.demo.users.api;
import jakarta.validation.constraints.*; import jakarta.validation.constraints.*;
public class UserDTO { import java.util.List;
public class UserDto {
private Long id; private Long id;
@NotNull @NotNull
@Min(2) @Min(2)
private String name; private String name;
@NotBlank @NotBlank
@Min(4) @Min(4)
private String login; private String login;
private List<Long> games;
private List<Long> plays;
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name){ public void setName(String name){
this.name = name; this.name = name;
} }
public String getLogin() public String getLogin()
{ {
return login; return login;
@ -24,6 +33,21 @@ public class UserDTO {
public void setLogin(String login){ public void setLogin(String login){
this.login = login; this.login = login;
} }
public List<Long> getGames() {
return games;
}
public void setGames(List<Long> games){
this.games = games;
}
public List<Long> getPlays(){
return plays;
}
public void setPlays(List<Long> plays){
this.plays = plays;
}
public Long getId(){ public Long getId(){
return id; return id;
} }