Compare commits

..

No commits in common. "57a412b0d713e0c712816b8f4a5ead570f90def5" and "09227a19c94f4a6f63856bf329427d08cdf3629f" have entirely different histories.

17 changed files with 102 additions and 402 deletions

37
demo/.gitignore vendored
View File

@ -1,37 +0,0 @@
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/

View File

@ -23,7 +23,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.modulith:spring-modulith-starter-core' implementation 'org.springframework.modulith:spring-modulith-starter-core'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.4.0' 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.boot:spring-boot-starter-test'
testImplementation 'org.springframework.modulith:spring-modulith-starter-test' testImplementation 'org.springframework.modulith:spring-modulith-starter-test'
} }

View File

@ -0,0 +1,59 @@
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);
}
}

View File

@ -0,0 +1,41 @@
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;
}
}

View File

@ -1,4 +1,4 @@
package com.example.demo.core.config; package com.example.demo;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -9,7 +9,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class WebConfig implements WebMvcConfigurer { public class WebConfig implements WebMvcConfigurer {
@Override @Override
public void addCorsMappings(@NonNull CorsRegistry registry) { public void addCorsMappings(@NonNull CorsRegistry registry) {
registry.addMapping("lab2/**") registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE"); .allowedMethods("GET", "POST", "PUT", "DELETE");
} }
} }

View File

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

View File

@ -1,13 +0,0 @@
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();
}
}

View File

@ -1,7 +0,0 @@
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));
}
}

View File

@ -1,19 +0,0 @@
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;
}
}

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

View File

@ -1,54 +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);
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;
}
}

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,66 +0,0 @@
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<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));
}
}

View File

@ -1,41 +0,0 @@
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;
}
}

View File

@ -1,46 +0,0 @@
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;
}
}

View File

@ -1,10 +0,0 @@
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> {
}

View File

@ -1,43 +0,0 @@
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<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());
existEntity.setPassword(entity.getPassword());
return repository.update(entity);
}
public UserEntity delete(Long id){
return repository.delete(repository.get(id));
}
}