From d3779e0ee2729304a2797a3d67990456e0a68aef Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 16 Apr 2024 13:35:25 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/demo/core/config/Constants.java | 2 +- .../demo/core/repository/CommonRepository.java | 2 +- .../example/demo/core/repository/MapRepository.java | 12 +++++++----- .../com/example/demo/users/api/UserController.java | 2 +- .../java/com/example/demo/users/api/UserDTO.java | 11 +++++++---- .../java/com/example/demo/users/api/UserMapper.java | 7 +++++++ .../com/example/demo/users/model/UserEntity.java | 10 +++------- .../com/example/demo/users/service/UserService.java | 2 +- 8 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 demo/src/main/java/com/example/demo/users/api/UserMapper.java diff --git a/demo/src/main/java/com/example/demo/core/config/Constants.java b/demo/src/main/java/com/example/demo/core/config/Constants.java index 1a0ab0e..6efe2d1 100644 --- a/demo/src/main/java/com/example/demo/core/config/Constants.java +++ b/demo/src/main/java/com/example/demo/core/config/Constants.java @@ -1,5 +1,5 @@ package com.example.demo.core.config; public class Constants { - public static final String API_URL = "/api/2"; + public static final String API_URL = "/api_2"; } diff --git a/demo/src/main/java/com/example/demo/core/repository/CommonRepository.java b/demo/src/main/java/com/example/demo/core/repository/CommonRepository.java index 7ce1d96..b5f11f9 100644 --- a/demo/src/main/java/com/example/demo/core/repository/CommonRepository.java +++ b/demo/src/main/java/com/example/demo/core/repository/CommonRepository.java @@ -8,5 +8,5 @@ public interface CommonRepository { E create(E entity); E update(E entity); E delete(E entity); - long deleteAll(); + void deleteAll(); } diff --git a/demo/src/main/java/com/example/demo/core/repository/MapRepository.java b/demo/src/main/java/com/example/demo/core/repository/MapRepository.java index 6866e5f..78dcd83 100644 --- a/demo/src/main/java/com/example/demo/core/repository/MapRepository.java +++ b/demo/src/main/java/com/example/demo/core/repository/MapRepository.java @@ -25,7 +25,8 @@ public abstract class MapRepository implements CommonRepos @Override public E create(E entity){ entity.setId(++lastId); - return entities.put(lastId, entity); + entities.put(lastId, entity); + return entity; } @Override @@ -33,7 +34,8 @@ public abstract class MapRepository implements CommonRepos if (get(entity.getId()) == null){ return null; } - return entities.put(entity.getId(), entity); + entities.put(entity.getId(), entity); + return entity; } @Override @@ -41,14 +43,14 @@ public abstract class MapRepository implements CommonRepos if (get(entity.getId()) == null){ return null; } - return entities.remove(entity.getId()); + entities.remove(entity.getId()); + return entity; } @Override - public long deleteAll(){ + public void deleteAll(){ long count = lastId; lastId = 0L; entities.clear(); - return count; } } diff --git a/demo/src/main/java/com/example/demo/users/api/UserController.java b/demo/src/main/java/com/example/demo/users/api/UserController.java index 1ca5a0f..b376338 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserController.java +++ b/demo/src/main/java/com/example/demo/users/api/UserController.java @@ -33,7 +33,7 @@ public class UserController { } private UserEntity toEntity(UserDTO dto){ - return modelMapper.map(dto, UserEntity.class); + return new UserEntity(-1L, dto.getName(), dto.getPassword()); } @GetMapping diff --git a/demo/src/main/java/com/example/demo/users/api/UserDTO.java b/demo/src/main/java/com/example/demo/users/api/UserDTO.java index ca6f652..d68d13a 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserDTO.java +++ b/demo/src/main/java/com/example/demo/users/api/UserDTO.java @@ -1,13 +1,9 @@ package com.example.demo.users.api; -import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.*; -import java.util.Date; - public class UserDTO { - @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotNull @Min(2) @@ -28,4 +24,11 @@ public class UserDTO { public void setPassword(String password){ this.password = password; } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public long getId(){ + return id; + } + public void setId(long id){ + this.id = id; + } } diff --git a/demo/src/main/java/com/example/demo/users/api/UserMapper.java b/demo/src/main/java/com/example/demo/users/api/UserMapper.java new file mode 100644 index 0000000..548789f --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/api/UserMapper.java @@ -0,0 +1,7 @@ +package com.example.demo.users.api; + +import org.modelmapper.ModelMapper; + +public class UserMapper extends ModelMapper { + +} diff --git a/demo/src/main/java/com/example/demo/users/model/UserEntity.java b/demo/src/main/java/com/example/demo/users/model/UserEntity.java index 5fa0017..fc6c644 100644 --- a/demo/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/demo/src/main/java/com/example/demo/users/model/UserEntity.java @@ -2,8 +2,6 @@ package com.example.demo.users.model; import com.example.demo.core.model.BaseEntity; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Date; public class UserEntity extends BaseEntity { @@ -15,9 +13,7 @@ public class UserEntity extends BaseEntity { super(); } - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy"); - - public UserEntity(Long id, String name, String password, String dateCreate) throws ParseException { + public UserEntity(Long id, String name, String password) { super(id); this.name = name; this.password = password; @@ -40,8 +36,8 @@ public class UserEntity extends BaseEntity { this.password = password; } - public String getDateCreate(){ - return formatter.format(dateCreate); + public Date getDateCreate(){ + return dateCreate; } public void setDateCreate(Date dateCreate){ diff --git a/demo/src/main/java/com/example/demo/users/service/UserService.java b/demo/src/main/java/com/example/demo/users/service/UserService.java index 98b9c11..0477580 100644 --- a/demo/src/main/java/com/example/demo/users/service/UserService.java +++ b/demo/src/main/java/com/example/demo/users/service/UserService.java @@ -34,7 +34,7 @@ public class UserService { final UserEntity existEntity = get(id); existEntity.setName(entity.getName()); existEntity.setPassword(entity.getPassword()); - return repository.update(entity); + return repository.update(existEntity); } public UserEntity delete(Long id){