написал контроллеры
This commit is contained in:
parent
3a149f97fa
commit
e58d7117cf
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/games")
|
||||
@RequestMapping(Constants.API_URL + "game")
|
||||
public class GameController {
|
||||
private final GameService gameService;
|
||||
private final ModelMapper modelMapper;
|
||||
@ -20,39 +20,42 @@ public class GameController {
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private GameDTO toDTO(GameEntity entity){
|
||||
return modelMapper.map(entity, GameDTO.class);
|
||||
private GameDto toDto(GameEntity entity){
|
||||
return modelMapper.map(entity, GameDto.class);
|
||||
}
|
||||
|
||||
private GameEntity toEntity(GameDTO dto){
|
||||
private GameEntity toEntity(GameDto dto){
|
||||
return modelMapper.map(dto, GameEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GameDTO> getAll()
|
||||
{
|
||||
return gameService.getAll().stream().map(this::toDTO).toList();
|
||||
public List<GameDto> getAll() {
|
||||
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}")
|
||||
public GameDTO get(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(gameService.get(id));
|
||||
public GameDto get(@PathVariable(name = "id") long id) {
|
||||
return toDto(gameService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public GameDTO create(@RequestBody @Valid GameDTO dto) {
|
||||
return toDTO(gameService.create(toEntity(dto)));
|
||||
@PostMapping
|
||||
public GameDto create(@RequestBody @Valid GameDto dto, @RequestParam Long userId) {
|
||||
return toDto(gameService.create(userId, toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public GameDTO update(@PathVariable(name = "id") long id, @RequestBody GameDTO dto) {
|
||||
return toDTO(gameService.update(id, toEntity(dto)));
|
||||
public GameDto update(@PathVariable(name = "id") long id, @RequestBody GameDto dto) {
|
||||
return toDto(gameService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
54
demo/src/main/java/com/example/demo/games/api/GameDto.java
Normal file
54
demo/src/main/java/com/example/demo/games/api/GameDto.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
55
demo/src/main/java/com/example/demo/plays/api/PlayDto.java
Normal file
55
demo/src/main/java/com/example/demo/plays/api/PlayDto.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -51,18 +51,18 @@ public class PlayService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public PlayEntity create(long gameId, Set<Long> usersId, PlayEntity entity){
|
||||
public PlayEntity create(PlayEntity entity){
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
|
||||
final GameEntity existGame = gameService.get(gameId);
|
||||
final GameEntity existGame = gameService.get(entity.getGame().getId());
|
||||
entity.setGame(existGame);
|
||||
|
||||
Set<UserEntity> users = new HashSet<UserEntity>() {
|
||||
};
|
||||
usersId.forEach(userId -> {
|
||||
final UserEntity existUser = userService.get(userId);
|
||||
entity.getUsers().forEach(user -> {
|
||||
final UserEntity existUser = userService.get(user.getId());
|
||||
users.add(existUser);
|
||||
});
|
||||
|
||||
|
@ -20,81 +20,42 @@ public class UserController {
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private UserDTO toDTO(UserEntity entity){
|
||||
UserDTO dto = new UserDTO();
|
||||
dto.setId(entity.getId());
|
||||
dto.setName(entity.getName());
|
||||
dto.setLogin(entity.getLogin());
|
||||
dto.setPassword(entity.getPassword());
|
||||
return dto;
|
||||
private UserDto toDto(UserEntity entity) {
|
||||
return modelMapper.map(entity, UserDto.class);
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDTO dto){
|
||||
return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword());
|
||||
private UserEntity toEntity(UserDto dto) {
|
||||
return modelMapper.map(dto, UserEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDTO> getAll()
|
||||
public List<UserDto> getAll()
|
||||
{
|
||||
return userService.getAll()
|
||||
.stream()
|
||||
.map(this::toDTO)
|
||||
.map(this::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UserDTO get(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(userService.get(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)));
|
||||
public UserDto create(@RequestBody @Valid UserDto dto) {
|
||||
return toDto(userService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserDTO update(
|
||||
public UserDto update(
|
||||
@PathVariable(name = "id") long id,
|
||||
@RequestBody UserDTO dto) {
|
||||
return toDTO(userService.update(id, toEntity(dto)));
|
||||
@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));
|
||||
}
|
||||
|
||||
@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();
|
||||
public UserDto delete(@PathVariable(name = "id") long id) {
|
||||
return toDto(userService.delete(id));
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,31 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import jakarta.validation.constraints.*;
|
||||
|
||||
public class UserDTO {
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@Min(2)
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Min(4)
|
||||
private String login;
|
||||
|
||||
private List<Long> games;
|
||||
|
||||
private List<Long> plays;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLogin()
|
||||
{
|
||||
return login;
|
||||
@ -24,6 +33,21 @@ public class UserDTO {
|
||||
public void setLogin(String 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(){
|
||||
return id;
|
||||
}
|
Loading…
Reference in New Issue
Block a user