Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
fe2692765f | ||
|
1f34a355bc | ||
|
ddd0f77d9a | ||
|
0d207ca4ac | ||
|
9ecf0abda5 | ||
|
d3779e0ee2 | ||
|
4cc4300a14 | ||
|
57a412b0d7 | ||
|
b96398d918 |
37
demo/.gitignore
vendored
Normal file
37
demo/.gitignore
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
HELP.md
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
@ -23,6 +23,7 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.modulith:spring-modulith-starter-core'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.4.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.1.1'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.springframework.modulith:spring-modulith-starter-test'
|
||||
}
|
||||
|
BIN
demo/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
demo/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
@ -1,59 +0,0 @@
|
||||
package com.example.demo.Controllers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.example.demo.Dto.UserDTO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class ApiController {
|
||||
private final Logger log = LoggerFactory.getLogger(ApiController.class);
|
||||
private LinkedHashMap<Integer, UserDTO> map = new LinkedHashMap<>();
|
||||
int id = 0;
|
||||
|
||||
@GetMapping("/")
|
||||
public List<UserDTO> getAll()
|
||||
{
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UserDTO get(@PathVariable(name = "id") int id)
|
||||
{
|
||||
return map.get(id);
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public UserDTO create(@RequestBody UserDTO data) {
|
||||
map.put(id, data);
|
||||
++id;
|
||||
return data;
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserDTO update(@PathVariable(name = "id") int id, @RequestBody UserDTO data) {
|
||||
log.info("The body value is {}", data);
|
||||
if (map.containsKey(id)) {
|
||||
map.put(id, data);
|
||||
}
|
||||
return get(id);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public UserDTO delete(@PathVariable(name = "id") int id)
|
||||
{
|
||||
return map.remove(id);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package com.example.demo.Dto;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class UserDTO {
|
||||
private String name;
|
||||
private String password;
|
||||
private String dateCreate;
|
||||
|
||||
public UserDTO() {
|
||||
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public UserDTO(
|
||||
@JsonProperty(value = "name") String name,
|
||||
@JsonProperty(value = "dateCreate") String dateCreate,
|
||||
@JsonProperty(value = "password") String password
|
||||
)
|
||||
{
|
||||
this.name = name;
|
||||
this.dateCreate = dateCreate;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDateCreate() {
|
||||
return dateCreate;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.demo.core.config;
|
||||
|
||||
public class Constants {
|
||||
public static final String API_URL = "/api_2";
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.demo.core.config;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MapperConfig {
|
||||
@Bean
|
||||
ModelMapper modelMapper(){
|
||||
return new ModelMapper();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo;
|
||||
package com.example.demo.core.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.lang.NonNull;
|
@ -0,0 +1,7 @@
|
||||
package com.example.demo.core.error;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(long id) {
|
||||
super(String.format("Entity with id {%s} is not found or not exists", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
public abstract class BaseEntity {
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity(){
|
||||
|
||||
}
|
||||
protected BaseEntity(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
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();
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.example.demo.games.api;
|
||||
|
||||
import com.example.demo.core.config.Constants;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import jakarta.validation.Valid;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/games")
|
||||
public class GameController {
|
||||
private final GameService gameService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public GameController(GameService gameService, ModelMapper modelMapper){
|
||||
this.gameService = gameService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private GameDTO toDTO(GameEntity entity){
|
||||
return modelMapper.map(entity, GameDTO.class);
|
||||
}
|
||||
|
||||
private GameEntity toEntity(GameDTO dto){
|
||||
return modelMapper.map(dto, GameEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GameDTO> getAll()
|
||||
{
|
||||
return gameService.getAll().stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{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)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
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)
|
||||
{
|
||||
return toDTO(gameService.delete(id));
|
||||
}
|
||||
}
|
25
demo/src/main/java/com/example/demo/games/api/GameDTO.java
Normal file
25
demo/src/main/java/com/example/demo/games/api/GameDTO.java
Normal file
@ -0,0 +1,25 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.example.demo.games.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class GameEntity extends BaseEntity {
|
||||
private String name;
|
||||
|
||||
public GameEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GameEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.games.repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class GameRepository extends MapRepository<GameEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.games.service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.repository.GameRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class GameService {
|
||||
private final GameRepository repository;
|
||||
public GameService(GameRepository repository){
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<GameEntity> getAll(){
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public GameEntity get(long id){
|
||||
return Optional
|
||||
.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
public GameEntity create(GameEntity entity){
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public GameEntity update(long id, GameEntity entity){
|
||||
final GameEntity existEntity = get(id);
|
||||
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName());
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public GameEntity delete(Long id){
|
||||
return repository.delete(repository.get(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.example.demo.tables.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.tables.model.TableEntity;
|
||||
import com.example.demo.tables.service.TableService;
|
||||
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 TableService tableService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public TableController(TableService tableService, ModelMapper modelMapper){
|
||||
|
||||
this.tableService = tableService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private TableDTO toDTO(TableEntity 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 TableEntity toEntity(TableDTO dto){
|
||||
TableEntity entity = modelMapper.map(dto, TableEntity.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 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));
|
||||
}
|
||||
}
|
75
demo/src/main/java/com/example/demo/tables/api/TableDTO.java
Normal file
75
demo/src/main/java/com/example/demo/tables/api/TableDTO.java
Normal file
@ -0,0 +1,75 @@
|
||||
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 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
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.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 getCreator(){
|
||||
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;
|
||||
}
|
||||
}
|
@ -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> {
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
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){
|
||||
entity.setCreator(
|
||||
entity.getCreator() == null ?
|
||||
entity.getCreator() :
|
||||
userService.get(entity.getCreator().getId()));
|
||||
|
||||
entity.setGamers(
|
||||
entity.getGamers().isEmpty() ?
|
||||
entity.getGamers() :
|
||||
entity.getGamers()
|
||||
.stream()
|
||||
.map(g -> userService.get(g.getId()))
|
||||
.toList()
|
||||
);
|
||||
entity.setGame(
|
||||
entity.getGame().getId() == null ?
|
||||
entity.getGame() :
|
||||
(gameService.get(entity.getGame().getId())));
|
||||
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.setCreator(
|
||||
entity.getCreator() == null ?
|
||||
existEntity.getCreator() :
|
||||
userService.get(entity.getCreator().getId()));
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.example.demo.core.config.Constants;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/users")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public UserController(UserService userService, ModelMapper modelMapper){
|
||||
this.userService = userService;
|
||||
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 UserEntity toEntity(UserDTO dto){
|
||||
return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword());
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
43
demo/src/main/java/com/example/demo/users/api/UserDTO.java
Normal file
43
demo/src/main/java/com/example/demo/users/api/UserDTO.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.*;
|
||||
|
||||
public class UserDTO {
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(2)
|
||||
private String name;
|
||||
@NotNull
|
||||
@Min(4)
|
||||
private String login;
|
||||
@NotNull
|
||||
@Min(4)
|
||||
private String password;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
public String getLogin()
|
||||
{
|
||||
return login;
|
||||
}
|
||||
public void setLogin(String login){
|
||||
this.login = login;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.example.demo.users.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class UserEntity extends BaseEntity {
|
||||
private String name;
|
||||
private String login;
|
||||
private String password;
|
||||
private Date dateCreate;
|
||||
|
||||
public UserEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name, String login, String password) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.dateCreate = new Date();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.dateCreate = new Date();
|
||||
this.login = "";
|
||||
this.password = "";
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
public void setLogin(String 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.users.repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class UserRepository extends MapRepository<UserEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.example.demo.users.service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository repository;
|
||||
public UserService(UserRepository repository){
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<UserEntity> getAll(){
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
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.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);
|
||||
}
|
||||
|
||||
public UserEntity delete(Long id){
|
||||
return repository.delete(repository.get(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.example.demo.DemoApplicationTests;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class GameServiceTest {
|
||||
@Autowired
|
||||
private GameService gameService;
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
Assertions.assertEquals(0, gameService.getAll().size());
|
||||
gameService.create(new GameEntity(null, "Древний ужас"));
|
||||
Assertions.assertEquals(1, gameService.getAll().size());
|
||||
gameService.create(new GameEntity(null, "Покорение марса"));
|
||||
Assertions.assertEquals(2, gameService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void UpdateTest(){
|
||||
var newEntity = new GameEntity(1L, "Аркхем");
|
||||
Assertions.assertNotEquals(newEntity, gameService.get(1L));
|
||||
final GameEntity newEntityCopy = gameService.update(1L, newEntity);
|
||||
|
||||
AssertionsEqualEntity(newEntityCopy, newEntity);
|
||||
|
||||
Assertions.assertEquals(2, gameService.getAll().size());
|
||||
|
||||
AssertionsEqualEntity(newEntity, gameService.get(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void DeleteTest(){
|
||||
Assertions.assertThrows(NullPointerException.class, () -> gameService.delete(3L));
|
||||
Assertions.assertEquals(2L, gameService.getAll().size());
|
||||
gameService.delete(1L);
|
||||
Assertions.assertEquals(1L, gameService.getAll().size());
|
||||
final GameEntity last = gameService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final GameEntity newEntity = gameService.create(new GameEntity(null, "Крылья"));
|
||||
Assertions.assertEquals(2L, gameService.getAll().size());
|
||||
Assertions.assertEquals(3L, newEntity.getId());
|
||||
}
|
||||
|
||||
private void AssertionsEqualEntity(GameEntity l, GameEntity r){
|
||||
Assertions.assertEquals(l.getName(), r.getName());
|
||||
Assertions.assertEquals(l.getId(), r.getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.example.demo.DemoApplicationTests;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class UserServiceTest {
|
||||
private boolean equalEntity(UserEntity l, UserEntity r){
|
||||
return l.getId() == r.getId() &&
|
||||
Objects.equals(l.getName(), r.getName()) &&
|
||||
Objects.equals(l.getLogin(), r.getLogin()) &&
|
||||
Objects.equals(l.getPassword(), r.getPassword());
|
||||
}
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
Assertions.assertEquals(0, userService.getAll().size());
|
||||
userService.create(new UserEntity(null, "cawa", "cacawa", "1111"));
|
||||
Assertions.assertEquals(1, userService.getAll().size());
|
||||
userService.create(new UserEntity(null, "lambada", "lam", "0000"));
|
||||
Assertions.assertEquals(2, userService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void UpdateTest(){
|
||||
var newEntity = new UserEntity(1L, "name", "login", "password");
|
||||
Assertions.assertNotEquals(newEntity, userService.get(1L));
|
||||
final UserEntity newEntityCopy = userService.update(
|
||||
1L, newEntity);
|
||||
Assertions.assertTrue(equalEntity(newEntityCopy, newEntity));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertTrue(equalEntity(newEntity, userService.get(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void DeleteTest(){
|
||||
Assertions.assertThrows(NullPointerException.class, () -> userService.delete(3L));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
userService.delete(1L);
|
||||
Assertions.assertEquals(1L, userService.getAll().size());
|
||||
final UserEntity last = userService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final UserEntity newEntity = userService.create(new UserEntity(null, "1", "1", "1"));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertEquals(3L, newEntity.getId());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user