добавил игру

This commit is contained in:
bekodeg 2024-04-30 14:20:26 +04:00
parent d3779e0ee2
commit 9ecf0abda5
15 changed files with 338 additions and 69 deletions

View File

@ -1,19 +1,19 @@
package com.example.demo.core.model;
public abstract class BaseEntity {
protected long id;
protected Long id;
protected BaseEntity(){
}
protected BaseEntity(long id){
protected BaseEntity(Long id){
this.id = id;
}
public long getId(){
public Long getId(){
return id;
}
public void setId(long id){
public void setId(Long id){
this.id = id;
}
}

View File

@ -8,7 +8,7 @@ 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;
private Long lastId = 0L;
protected MapRepository(){
}

View File

@ -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));
}
}

View File

@ -0,0 +1,26 @@
package com.example.demo.games.api;
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;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}

View File

@ -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;
}
}

View File

@ -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> {
}

View File

@ -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());
return repository.update(existEntity);
}
public GameEntity delete(Long id){
return repository.delete(repository.get(id));
}
}

View File

@ -1,46 +0,0 @@
package com.example.demo.news.model;
import com.example.demo.core.model.BaseEntity;
import java.util.Date;
public class NewsEntity extends BaseEntity {
private String title;
private String text;
private Date dateCreate;
public NewsEntity() {
super();
}
public NewsEntity(Long id, String name, String password, Date dateCreate){
super(id);
this.title = name;
this.text = password;
this.dateCreate = dateCreate;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getText(){
return text;
}
public void setText(String text){
this.text = text;
}
public Date getDateCreate(){
return dateCreate;
}
public void setDateCreate(Date dateCreate){
this.dateCreate = dateCreate;
}
}

View File

@ -1,6 +1,5 @@
package com.example.demo.users.api;
import java.util.ArrayList;
import java.util.List;
import com.example.demo.core.config.Constants;
@ -29,11 +28,16 @@ public class UserController {
}
private UserDTO toDTO(UserEntity entity){
return modelMapper.map(entity, UserDTO.class);
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(-1L, dto.getName(), dto.getPassword());
return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword());
}
@GetMapping

View File

@ -10,6 +10,9 @@ public class UserDTO {
private String name;
@NotNull
@Min(4)
private String login;
@NotNull
@Min(4)
private String password;
public String getName() {
@ -18,6 +21,13 @@ public class UserDTO {
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;
}
@ -25,10 +35,10 @@ public class UserDTO {
this.password = password;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public long getId(){
public Long getId(){
return id;
}
public void setId(long id){
public void setId(Long id){
this.id = id;
}
}

View File

@ -1,7 +0,0 @@
package com.example.demo.users.api;
import org.modelmapper.ModelMapper;
public class UserMapper extends ModelMapper {
}

View File

@ -6,6 +6,7 @@ import java.util.Date;
public class UserEntity extends BaseEntity {
private String name;
private String login;
private String password;
private Date dateCreate;
@ -13,25 +14,39 @@ public class UserEntity extends BaseEntity {
super();
}
public UserEntity(Long id, String name, String password) {
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;
}
@ -39,7 +54,6 @@ public class UserEntity extends BaseEntity {
public Date getDateCreate(){
return dateCreate;
}
public void setDateCreate(Date dateCreate){
this.dateCreate = dateCreate;
}

View File

@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import java.awt.event.ItemEvent;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
@ -25,7 +26,6 @@ public class UserService {
.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
}
public UserEntity create(UserEntity entity){
return repository.create(entity);
}
@ -33,7 +33,10 @@ public class UserService {
public UserEntity update(long id, UserEntity entity){
final UserEntity existEntity = get(id);
existEntity.setName(entity.getName());
existEntity.setPassword(entity.getPassword());
if (!Objects.equals(entity.getLogin(), "") && !Objects.equals(entity.getPassword(), "")){
existEntity.setLogin(entity.getLogin());
existEntity.setPassword(entity.getPassword());
}
return repository.update(existEntity);
}

View File

@ -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());
}
}

View File

@ -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());
}
}