создал певые модели

This commit is contained in:
bekodeg 2024-05-30 17:43:57 +04:00
parent 3aed9456e6
commit 7b44e6f770
16 changed files with 169 additions and 176 deletions

View File

@ -1,5 +1,6 @@
package com.example.demo.core.config; package com.example.demo.core.config;
public class Constants { public class Constants {
public static final String SEQUENCE_NAME = "hibernate_sequence";
public static final String API_URL = "/api_3"; public static final String API_URL = "/api_3";
} }

View File

@ -1,7 +1,7 @@
package com.example.demo.core.error; package com.example.demo.core.error;
public class NotFoundException extends RuntimeException { public class NotFoundException extends RuntimeException {
public NotFoundException(long id) { public <T> NotFoundException(Class<T> clazz, Long id) {
super(String.format("Entity with id {%s} is not found or not exists", id)); super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id));
} }
} }

View File

@ -1,18 +1,27 @@
package com.example.demo.core.model; package com.example.demo.core.model;
import com.example.demo.core.config.Constants;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.SequenceGenerator;
@MappedSuperclass
public abstract class BaseEntity { public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
protected Long id; protected Long id;
protected BaseEntity() { protected BaseEntity() {
}
protected BaseEntity(Long id){
this.id = id;
} }
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }

View File

@ -1,12 +0,0 @@
package com.example.demo.core.repository;
import java.util.List;
public interface CommonRepository<E, T> {
List<E> getAll();
E get(T id);
E create(E entity);
E update(E entity);
E delete(E entity);
void deleteAll();
}

View File

@ -1,56 +0,0 @@
package com.example.demo.core.repository;
import com.example.demo.core.model.BaseEntity;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
private final Map<Long, E> entities = new TreeMap<>();
private Long lastId = 0L;
protected MapRepository(){
}
@Override
public List<E> getAll(){
return entities.values().stream().toList();
}
@Override
public E get(Long id) {
return entities.get(id);
}
@Override
public E create(E entity){
entity.setId(++lastId);
entities.put(lastId, entity);
return entity;
}
@Override
public E update(E entity){
if (get(entity.getId()) == null){
return null;
}
entities.put(entity.getId(), entity);
return entity;
}
@Override
public E delete(E entity){
if (get(entity.getId()) == null){
return null;
}
entities.remove(entity.getId());
return entity;
}
@Override
public void deleteAll(){
long count = lastId;
lastId = 0L;
entities.clear();
}
}

View File

@ -1,18 +1,29 @@
package com.example.demo.games.model; package com.example.demo.games.model;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.tables.model.BookingEntity;
import com.example.demo.users.model.UserEntity;
import jakarta.persistence.*;
import java.util.Date; import java.util.List;
@Entity
@Table(name = "game")
public class GameEntity extends BaseEntity { public class GameEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 100)
private String name; private String name;
@ManyToMany(mappedBy = "user_game")
private List<UserEntity> users;
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("date desc")
List<BookingEntity> bookings;
public GameEntity() { public GameEntity() {
super();
} }
public GameEntity(Long id, String name) { public GameEntity(String name) {
super(id);
this.name = name; this.name = name;
} }
@ -22,4 +33,12 @@ public class GameEntity extends BaseEntity {
public void setName(String name){ public void setName(String name){
this.name = name; this.name = name;
} }
public List<UserEntity> getUsers(){
return users;
}
public List<BookingEntity> getBookings(){
return bookings;
}
} }

View File

@ -19,7 +19,7 @@ public class GameService {
} }
public List<GameEntity> getAll(){ public List<GameEntity> getAll(){
return repository.getAll(); return repository.findAll();
} }
public GameEntity get(long id){ public GameEntity get(long id){

View File

@ -3,7 +3,7 @@ package com.example.demo.tables.api;
import com.example.demo.core.config.Constants; import com.example.demo.core.config.Constants;
import com.example.demo.games.api.GameDTO; import com.example.demo.games.api.GameDTO;
import com.example.demo.games.model.GameEntity; import com.example.demo.games.model.GameEntity;
import com.example.demo.tables.model.TableEntity; import com.example.demo.tables.model.BookingEntity;
import com.example.demo.tables.service.TableService; import com.example.demo.tables.service.TableService;
import com.example.demo.users.api.UserDTO; import com.example.demo.users.api.UserDTO;
import com.example.demo.users.model.UserEntity; import com.example.demo.users.model.UserEntity;
@ -25,7 +25,7 @@ public class TableController {
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
} }
private TableDTO toDTO(TableEntity entity) { private TableDTO toDTO(BookingEntity entity) {
return new TableDTO( return new TableDTO(
entity.getId(), entity.getId(),
entity.getDescription(), entity.getDescription(),
@ -35,8 +35,8 @@ public class TableController {
entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList() entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList()
); );
} }
private TableEntity toEntity(TableDTO dto){ private BookingEntity toEntity(TableDTO dto){
TableEntity entity = modelMapper.map(dto, TableEntity.class); BookingEntity entity = modelMapper.map(dto, BookingEntity.class);
entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class)); entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class));
entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class)); entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class));
entity.setGamers(dto.getGamers().stream() entity.setGamers(dto.getGamers().stream()

View File

@ -3,22 +3,36 @@ package com.example.demo.tables.model;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.games.model.GameEntity; import com.example.demo.games.model.GameEntity;
import com.example.demo.users.model.UserEntity; import com.example.demo.users.model.UserEntity;
import jakarta.persistence.*;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class TableEntity extends BaseEntity { @Entity
@Table(name = "booking")
public class BookingEntity extends BaseEntity {
@Column(nullable = false, length = 500)
private String description; private String description;
private Date date;
private UserEntity creator;
private GameEntity game;
private List<UserEntity> gamers;
public TableEntity() { @Column(nullable = false)
super(); private Date date;
@ManyToOne
@JoinColumn(name = "creator_id")
private UserEntity creator;
@ManyToOne
@JoinColumn(name = "game_id")
private GameEntity game;
@ManyToMany(mappedBy = "book_user")
private List<UserEntity> users;
public BookingEntity() {
} }
public TableEntity(String description, Date date, public BookingEntity(String description,
UserEntity creator, GameEntity game, List<UserEntity> users){ Date date){
this.description = description; this.description = description;
this.date = date; this.date = date;
} }
@ -52,9 +66,9 @@ public class TableEntity extends BaseEntity {
} }
public List<UserEntity> getGamers(){ public List<UserEntity> getGamers(){
return gamers; return users;
} }
public void setGamers(List<UserEntity> gamers){ public void setGamers(List<UserEntity> gamers){
this.gamers = gamers; this.users = gamers;
} }
} }

View File

@ -1,8 +1,8 @@
package com.example.demo.tables.repository; package com.example.demo.tables.repository;
import com.example.demo.tables.model.TableEntity; import com.example.demo.tables.model.BookingEntity;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
public interface TableRepository extends CrudRepository<TableEntity, Long> { public interface TableRepository extends CrudRepository<BookingEntity, Long> {
} }

View File

@ -2,14 +2,11 @@ package com.example.demo.tables.service;
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.games.service.GameService;
import com.example.demo.tables.model.TableEntity; import com.example.demo.tables.model.BookingEntity;
import com.example.demo.tables.repository.TableRepository; import com.example.demo.tables.repository.TableRepository;
import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService; import com.example.demo.users.service.UserService;
import jakarta.validation.OverridesAttribute;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -25,16 +22,16 @@ public class TableService {
this.gameService = gameService; this.gameService = gameService;
} }
public List<TableEntity> getAll(){ public List<BookingEntity> getAll(){
return repository.getAll(); return repository.getAll();
} }
public TableEntity get(Long id){ public BookingEntity get(Long id){
return Optional return Optional
.ofNullable(repository.get(id)) .ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id)); .orElseThrow(() -> new NotFoundException(id));
} }
public TableEntity create(TableEntity entity){ public BookingEntity create(BookingEntity entity){
entity.setCreator( entity.setCreator(
entity.getCreator() == null ? entity.getCreator() == null ?
entity.getCreator() : entity.getCreator() :
@ -55,8 +52,8 @@ public class TableService {
return repository.create(entity); return repository.create(entity);
} }
public TableEntity update(Long id, TableEntity entity){ public BookingEntity update(Long id, BookingEntity entity){
final TableEntity existEntity = get(id); final BookingEntity existEntity = get(id);
existEntity.setDate(entity.getDate() == null ? existEntity.getDate() : entity.getDate()); existEntity.setDate(entity.getDate() == null ? existEntity.getDate() : entity.getDate());
existEntity.setDescription( existEntity.setDescription(
@ -84,7 +81,7 @@ public class TableService {
return repository.update(existEntity); return repository.update(existEntity);
} }
public TableEntity delete(Long id){ public BookingEntity delete(Long id){
return repository.delete(repository.get(id)); return repository.delete(repository.get(id));
} }
} }

View File

@ -7,14 +7,7 @@ import com.example.demo.users.model.UserEntity;
import com.example.demo.users.service.UserService; import com.example.demo.users.service.UserService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.*;
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;
@RestController @RestController
@RequestMapping(Constants.API_URL + "/users") @RequestMapping(Constants.API_URL + "/users")
@ -43,7 +36,10 @@ public class UserController {
@GetMapping @GetMapping
public List<UserDTO> getAll() public List<UserDTO> getAll()
{ {
return userService.getAll().stream().map(this::toDTO).toList(); return userService.getAll()
.stream()
.map(this::toDTO)
.toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -52,13 +48,15 @@ public class UserController {
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(@PathVariable(name = "id") long id, @RequestBody UserDTO dto) { public UserDTO update(
@PathVariable(name = "id") long id,
@RequestBody UserDTO dto) {
return toDTO(userService.update(id, toEntity(dto))); return toDTO(userService.update(id, toEntity(dto)));
} }
@ -67,4 +65,36 @@ public class UserController {
{ {
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,6 +1,5 @@
package com.example.demo.users.api; package com.example.demo.users.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.*; import jakarta.validation.constraints.*;
public class UserDTO { public class UserDTO {
@ -8,12 +7,9 @@ public class UserDTO {
@NotNull @NotNull
@Min(2) @Min(2)
private String name; private String name;
@NotNull @NotBlank
@Min(4) @Min(4)
private String login; private String login;
@NotNull
@Min(4)
private String password;
public String getName() { public String getName() {
return name; return name;
@ -28,12 +24,6 @@ public class UserDTO {
public void setLogin(String login){ public void setLogin(String login){
this.login = login; this.login = login;
} }
public String getPassword() {
return password;
}
public void setPassword(String password){
this.password = password;
}
public Long getId(){ public Long getId(){
return id; return id;
} }

View File

@ -1,33 +1,26 @@
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 jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import java.util.Date; import java.util.Date;
@Entity
@Table(name = "users")
public class UserEntity extends BaseEntity { public class UserEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 100)
private String name; private String name;
@Column(nullable = false, unique = true, length = 50)
private String login; private String login;
private String password;
private Date dateCreate;
public UserEntity() { public UserEntity() {
super();
} }
public UserEntity(Long id, String name, String login, String password) { public UserEntity(String login, String name) {
super(id);
this.name = name;
this.login = login; this.login = login;
this.password = password;
this.dateCreate = new Date();
}
public UserEntity(Long id, String name) {
super(id);
this.name = name; this.name = name;
this.dateCreate = new Date();
this.login = "";
this.password = "";
} }
public String getName(){ public String getName(){
@ -44,17 +37,4 @@ public class UserEntity extends BaseEntity {
this.login = login; this.login = login;
} }
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public Date getDateCreate(){
return dateCreate;
}
public void setDateCreate(Date dateCreate){
this.dateCreate = dateCreate;
}
} }

View File

@ -2,6 +2,9 @@ package com.example.demo.users.repository;
import com.example.demo.users.model.UserEntity; import com.example.demo.users.model.UserEntity;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<UserEntity, Long> {
import java.util.Optional;
public interface UserRepository extends CrudRepository<UserEntity, Long> {
Optional<UserEntity> findByLoginIgnoreCase(String login);
} }

View File

@ -4,11 +4,13 @@ import com.example.demo.core.error.NotFoundException;
import com.example.demo.users.model.UserEntity; import com.example.demo.users.model.UserEntity;
import com.example.demo.users.repository.UserRepository; import com.example.demo.users.repository.UserRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.StreamSupport;
@Service @Service
public class UserService { public class UserService {
@ -17,8 +19,16 @@ public class UserService {
this.repository = repository; this.repository = repository;
} }
private void checkLogin(String login) {
if (repository.findByLoginIgnoreCase(login).isPresent()) {
throw new IllegalArgumentException(
String.format("User with login %s is already exists", login));
}
}
@Transactional(readOnly = true)
public List<UserEntity> getAll() { public List<UserEntity> getAll() {
return repository.getAll(); return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
} }
public UserEntity get(Long id){ public UserEntity get(Long id){
@ -26,21 +36,29 @@ public class UserService {
.ofNullable(repository.get(id)) .ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id)); .orElseThrow(() -> new NotFoundException(id));
} }
@Transactional
public UserEntity create(UserEntity entity) { public UserEntity create(UserEntity entity) {
return repository.create(entity); if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
checkLogin(entity.getLogin());
repository.save(entity);
return repository.save(entity);
} }
public UserEntity update(Long id, UserEntity entity){ @Transactional
final UserEntity existEntity = get(id); public UserEntity update(long id, UserEntity entity) {
final UserEntity existsEntity = get(id);
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName()); checkLogin(entity.getLogin());
existEntity.setLogin(entity.getLogin().isEmpty() ? existEntity.getLogin() : entity.getLogin()); existsEntity.setLogin(entity.getLogin());
existEntity.setPassword(entity.getPassword().isEmpty() ? existEntity.getPassword() : entity.getPassword()); repository.save(existsEntity);
return existsEntity;
return repository.update(existEntity);
} }
public UserEntity delete(Long id){ @Transactional
return repository.delete(repository.get(id)); public UserEntity delete(long id) {
final UserEntity existsEntity = get(id);
repository.delete(existsEntity);
return existsEntity;
} }
} }