From b96398d9181f569b8c1140610e4da8517d7ac476 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 9 Apr 2024 14:45:23 +0400 Subject: [PATCH] lab2 --- demo/build.gradle | 1 + .../demo/Controllers/ApiController.java | 59 ----------------- .../java/com/example/demo/Dto/UserDTO.java | 41 ------------ .../example/demo/core/config/Constants.java | 5 ++ .../demo/core/config/MapperConfig.java | 13 ++++ .../demo/{ => core/config}/WebConfig.java | 4 +- .../demo/core/error/NotFoundException.java | 7 ++ .../example/demo/core/model/BaseEntity.java | 19 ++++++ .../core/repository/CommonRepository.java | 12 ++++ .../demo/core/repository/MapRepository.java | 54 +++++++++++++++ .../example/demo/news/model/NewsEntity.java | 46 +++++++++++++ .../demo/users/api/UserController.java | 66 +++++++++++++++++++ .../com/example/demo/users/api/UserDTO.java | 41 ++++++++++++ .../example/demo/users/model/UserEntity.java | 46 +++++++++++++ .../demo/users/repository/UserRepository.java | 10 +++ .../demo/users/service/UserService.java | 43 ++++++++++++ 16 files changed, 365 insertions(+), 102 deletions(-) delete mode 100644 demo/src/main/java/com/example/demo/Controllers/ApiController.java delete mode 100644 demo/src/main/java/com/example/demo/Dto/UserDTO.java create mode 100644 demo/src/main/java/com/example/demo/core/config/Constants.java create mode 100644 demo/src/main/java/com/example/demo/core/config/MapperConfig.java rename demo/src/main/java/com/example/demo/{ => core/config}/WebConfig.java (85%) create mode 100644 demo/src/main/java/com/example/demo/core/error/NotFoundException.java create mode 100644 demo/src/main/java/com/example/demo/core/model/BaseEntity.java create mode 100644 demo/src/main/java/com/example/demo/core/repository/CommonRepository.java create mode 100644 demo/src/main/java/com/example/demo/core/repository/MapRepository.java create mode 100644 demo/src/main/java/com/example/demo/news/model/NewsEntity.java create mode 100644 demo/src/main/java/com/example/demo/users/api/UserController.java create mode 100644 demo/src/main/java/com/example/demo/users/api/UserDTO.java create mode 100644 demo/src/main/java/com/example/demo/users/model/UserEntity.java create mode 100644 demo/src/main/java/com/example/demo/users/repository/UserRepository.java create mode 100644 demo/src/main/java/com/example/demo/users/service/UserService.java diff --git a/demo/build.gradle b/demo/build.gradle index 42a4e60..edd4116 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -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' } diff --git a/demo/src/main/java/com/example/demo/Controllers/ApiController.java b/demo/src/main/java/com/example/demo/Controllers/ApiController.java deleted file mode 100644 index 05a8971..0000000 --- a/demo/src/main/java/com/example/demo/Controllers/ApiController.java +++ /dev/null @@ -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 map = new LinkedHashMap<>(); - int id = 0; - - @GetMapping("/") - public List 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); - } -} diff --git a/demo/src/main/java/com/example/demo/Dto/UserDTO.java b/demo/src/main/java/com/example/demo/Dto/UserDTO.java deleted file mode 100644 index 83a00ab..0000000 --- a/demo/src/main/java/com/example/demo/Dto/UserDTO.java +++ /dev/null @@ -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; - } -} 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 new file mode 100644 index 0000000..ece8436 --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/config/Constants.java @@ -0,0 +1,5 @@ +package com.example.demo.core.config; + +public class Constants { + public static final String API_URL = "/api"; +} diff --git a/demo/src/main/java/com/example/demo/core/config/MapperConfig.java b/demo/src/main/java/com/example/demo/core/config/MapperConfig.java new file mode 100644 index 0000000..3fc9bc9 --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/config/MapperConfig.java @@ -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(); + } +} diff --git a/demo/src/main/java/com/example/demo/WebConfig.java b/demo/src/main/java/com/example/demo/core/config/WebConfig.java similarity index 85% rename from demo/src/main/java/com/example/demo/WebConfig.java rename to demo/src/main/java/com/example/demo/core/config/WebConfig.java index d5585a1..87a157a 100644 --- a/demo/src/main/java/com/example/demo/WebConfig.java +++ b/demo/src/main/java/com/example/demo/core/config/WebConfig.java @@ -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; @@ -9,7 +9,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(@NonNull CorsRegistry registry) { - registry.addMapping("/**") + registry.addMapping("lab2/**") .allowedMethods("GET", "POST", "PUT", "DELETE"); } } diff --git a/demo/src/main/java/com/example/demo/core/error/NotFoundException.java b/demo/src/main/java/com/example/demo/core/error/NotFoundException.java new file mode 100644 index 0000000..64cb700 --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/error/NotFoundException.java @@ -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)); + } +} diff --git a/demo/src/main/java/com/example/demo/core/model/BaseEntity.java b/demo/src/main/java/com/example/demo/core/model/BaseEntity.java new file mode 100644 index 0000000..70e533f --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/model/BaseEntity.java @@ -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; + } +} 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 new file mode 100644 index 0000000..7ce1d96 --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/repository/CommonRepository.java @@ -0,0 +1,12 @@ +package com.example.demo.core.repository; + +import java.util.List; + +public interface CommonRepository { + List getAll(); + E get(T id); + E create(E entity); + E update(E entity); + E delete(E entity); + long 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 new file mode 100644 index 0000000..6866e5f --- /dev/null +++ b/demo/src/main/java/com/example/demo/core/repository/MapRepository.java @@ -0,0 +1,54 @@ +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 implements CommonRepository { + private final Map entities = new TreeMap<>(); + private long lastId = 0L; + protected MapRepository(){ + + } + @Override + public List 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); + return entities.put(lastId, entity); + } + + @Override + public E update(E entity){ + if (get(entity.getId()) == null){ + return null; + } + return entities.put(entity.getId(), entity); + } + + @Override + public E delete(E entity){ + if (get(entity.getId()) == null){ + return null; + } + return entities.remove(entity.getId()); + } + + @Override + public long deleteAll(){ + long count = lastId; + lastId = 0L; + entities.clear(); + return count; + } +} diff --git a/demo/src/main/java/com/example/demo/news/model/NewsEntity.java b/demo/src/main/java/com/example/demo/news/model/NewsEntity.java new file mode 100644 index 0000000..c9865e5 --- /dev/null +++ b/demo/src/main/java/com/example/demo/news/model/NewsEntity.java @@ -0,0 +1,46 @@ +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; + } +} 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 new file mode 100644 index 0000000..1ca5a0f --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/api/UserController.java @@ -0,0 +1,66 @@ +package com.example.demo.users.api; + +import java.util.ArrayList; +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){ + return modelMapper.map(entity, UserDTO.class); + } + + private UserEntity toEntity(UserDTO dto){ + return modelMapper.map(dto, UserEntity.class); + } + + @GetMapping + public List 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)); + } +} 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 new file mode 100644 index 0000000..aa0fb12 --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/api/UserDTO.java @@ -0,0 +1,41 @@ +package com.example.demo.users.api; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Past; + +import java.util.Date; + +public class UserDTO { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + private Long id; + @NotNull + @Min(2) + private String name; + @NotNull + @Min(4) + private String password; + @NotNull + @Past + private Date dateCreate; + + public String getName() { + return name; + } + public void setName(String name){ + this.name = name; + } + + public Date getDateCreate() { + return dateCreate; + } + + public String getPassword() { + return password; + } + public void setPassword(String password){ + this.password = password; + } +} 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 new file mode 100644 index 0000000..0890d87 --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/model/UserEntity.java @@ -0,0 +1,46 @@ +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 password; + private Date dateCreate; + + public UserEntity() { + super(); + } + + public UserEntity(Long id, String name, String password, Date dateCreate){ + super(id); + this.name = name; + this.password = password; + this.dateCreate = dateCreate; + } + + public String getName(){ + return name; + } + + public void setName(String name){ + this.name = name; + } + + 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; + } +} diff --git a/demo/src/main/java/com/example/demo/users/repository/UserRepository.java b/demo/src/main/java/com/example/demo/users/repository/UserRepository.java new file mode 100644 index 0000000..ac2ef22 --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/repository/UserRepository.java @@ -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 { + +} 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 new file mode 100644 index 0000000..98b9c11 --- /dev/null +++ b/demo/src/main/java/com/example/demo/users/service/UserService.java @@ -0,0 +1,43 @@ +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.Optional; + +@Service +public class UserService { + private final UserRepository repository; + public UserService(UserRepository repository){ + this.repository = repository; + } + + public List 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()); + existEntity.setPassword(entity.getPassword()); + return repository.update(entity); + } + + public UserEntity delete(Long id){ + return repository.delete(repository.get(id)); + } +}