From 29df7120bf820ae855f25c81bea4d50e83c013dd Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 18 Mar 2019 14:27:54 +0400 Subject: [PATCH] Delete DeadlineDTO and update usages --- .../ru/ulstu/deadline/model/Deadline.java | 13 +++++ .../ru/ulstu/deadline/model/DeadlineDto.java | 58 ------------------- .../deadline/service/DeadlineService.java | 33 +++++------ .../grant/controller/GrantController.java | 4 +- .../java/ru/ulstu/grant/model/GrantDto.java | 16 +++-- .../paper/controller/PaperController.java | 4 +- .../java/ru/ulstu/paper/model/PaperDto.java | 14 ++--- .../ru/ulstu/project/model/ProjectDto.java | 14 ++--- 8 files changed, 53 insertions(+), 103 deletions(-) delete mode 100644 src/main/java/ru/ulstu/deadline/model/DeadlineDto.java diff --git a/src/main/java/ru/ulstu/deadline/model/Deadline.java b/src/main/java/ru/ulstu/deadline/model/Deadline.java index 1a73e59..404e5c8 100644 --- a/src/main/java/ru/ulstu/deadline/model/Deadline.java +++ b/src/main/java/ru/ulstu/deadline/model/Deadline.java @@ -1,5 +1,8 @@ package ru.ulstu.deadline.model; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.springframework.format.annotation.DateTimeFormat; import ru.ulstu.core.model.BaseEntity; import javax.persistence.Entity; @@ -13,6 +16,7 @@ public class Deadline extends BaseEntity { private String description; @Temporal(value = TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") private Date date; public Deadline() { @@ -23,6 +27,15 @@ public class Deadline extends BaseEntity { this.description = description; } + @JsonCreator + public Deadline(@JsonProperty("id") Integer id, + @JsonProperty("description") String description, + @JsonProperty("date") Date date) { + this.setId(id); + this.description = description; + this.date = date; + } + public String getDescription() { return description; } diff --git a/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java b/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java deleted file mode 100644 index 7f724aa..0000000 --- a/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java +++ /dev/null @@ -1,58 +0,0 @@ -package ru.ulstu.deadline.model; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.format.annotation.DateTimeFormat; - -import java.util.Date; - -public class DeadlineDto { - private Integer id; - - private String description; - - @DateTimeFormat(pattern = "yyyy-MM-dd") - private Date date; - - public DeadlineDto() { - } - - @JsonCreator - public DeadlineDto(@JsonProperty("id") Integer id, - @JsonProperty("description") String description, - @JsonProperty("date") Date date) { - this.id = id; - this.description = description; - this.date = date; - } - - public DeadlineDto(Deadline deadline) { - this.id = deadline.getId(); - this.description = deadline.getDescription(); - this.date = deadline.getDate(); - } - - public Integer getId() { - return id; - } - - public String getDescription() { - return description; - } - - public Date getDate() { - return date; - } - - public void setId(Integer id) { - this.id = id; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setDate(Date date) { - this.date = date; - } -} diff --git a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java index c371d87..2180025 100644 --- a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java +++ b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java @@ -3,7 +3,6 @@ package ru.ulstu.deadline.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.deadline.repository.DeadlineRepository; import java.util.List; @@ -17,29 +16,29 @@ public class DeadlineService { this.deadlineRepository = deadlineRepository; } - public List saveOrCreate(List deadlines) { - return deadlines.stream().map(deadlineDto -> { - return deadlineDto.getId() != null ? update(deadlineDto) : create(deadlineDto); - }).collect(Collectors.toList()); + public List saveOrCreate(List deadlines) { + return deadlines + .stream() + .map(deadline -> { + return deadline.getId() != null ? update(deadline) : create(deadline); + }).collect(Collectors.toList()); } @Transactional - public Deadline update(DeadlineDto deadlineDto) { - Deadline deadline = deadlineRepository.findOne(deadlineDto.getId()); - deadlineRepository.save(copyFromDto(deadline, deadlineDto)); - return deadline; + public Deadline update(Deadline deadline) { + Deadline updateDeadline = deadlineRepository.findOne(deadline.getId()); + updateDeadline.setDate(deadline.getDate()); + updateDeadline.setDescription(deadline.getDescription()); + deadlineRepository.save(updateDeadline); + return updateDeadline; } @Transactional - public Deadline create(DeadlineDto deadlineDto) { - Deadline newDeadline = copyFromDto(new Deadline(), deadlineDto); + public Deadline create(Deadline deadline) { + Deadline newDeadline = new Deadline(); + newDeadline.setDate(deadline.getDate()); + newDeadline.setDescription(deadline.getDescription()); newDeadline = deadlineRepository.save(newDeadline); return newDeadline; } - - private Deadline copyFromDto(Deadline deadline, DeadlineDto deadlineDto) { - deadline.setDate(deadlineDto.getDate()); - deadline.setDescription(deadlineDto.getDescription()); - return deadline; - } } diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 8310f84..c786ffa 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.service.GrantService; @@ -69,7 +69,7 @@ public class GrantController { public String addDeadline(@Valid GrantDto grantDto, Errors errors) { filterEmptyDeadlines(grantDto); hasErrors(errors, GRANT_PAGE); - grantDto.getDeadlines().add(new DeadlineDto()); + grantDto.getDeadlines().add(new Deadline()); return GRANT_PAGE; } diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index e56896a..fb29164 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -3,33 +3,31 @@ package ru.ulstu.grant.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.project.model.ProjectDto; import java.util.ArrayList; import java.util.List; -import static ru.ulstu.core.util.StreamApiUtils.convert; - public class GrantDto { private Integer id; @NotEmpty private String title; private Grant.GrantStatus status; - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); private String comment; private String applicationFileName; private ProjectDto project; public GrantDto() { - deadlines.add(new DeadlineDto()); + deadlines.add(new Deadline()); } @JsonCreator public GrantDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, @JsonProperty("status") Grant.GrantStatus status, - @JsonProperty("deadlines") List deadlines, + @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, @JsonProperty("project") ProjectDto project) { this.id = id; @@ -45,7 +43,7 @@ public class GrantDto { this.id = grant.getId(); this.title = grant.getTitle(); this.status = grant.getStatus(); - this.deadlines = convert(grant.getDeadlines(), DeadlineDto::new); + this.deadlines = grant.getDeadlines(); this.comment = grant.getComment(); this.project = grant.getProject() == null ? null : new ProjectDto(grant.getProject()); this.applicationFileName = grant.getApplication() == null ? null : grant.getApplication().getName(); @@ -75,11 +73,11 @@ public class GrantDto { this.status = status; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index cb34a8a..31df0e3 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperFilterDto; @@ -80,7 +80,7 @@ public class PaperController { if (errors.hasErrors()) { return "/papers/paper"; } - paperDto.getDeadlines().add(new DeadlineDto()); + paperDto.getDeadlines().add(new Deadline()); return "/papers/paper"; } diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 545b432..3405768 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.user.model.UserDto; import javax.validation.constraints.Size; @@ -27,7 +27,7 @@ public class PaperDto { private Date createDate; private Date updateDate; @NotEmpty - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); private String comment; private Boolean locked; private String tmpFileName; @@ -39,7 +39,7 @@ public class PaperDto { private Integer filterAuthorId; public PaperDto() { - deadlines.add(new DeadlineDto()); + deadlines.add(new Deadline()); } @JsonCreator @@ -48,7 +48,7 @@ public class PaperDto { @JsonProperty("status") Paper.PaperStatus status, @JsonProperty("createDate") Date createDate, @JsonProperty("updateDate") Date updateDate, - @JsonProperty("deadlines") List deadlines, + @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, @JsonProperty("locked") Boolean locked, @JsonProperty("tmpFileName") String tmpFileName, @@ -75,7 +75,7 @@ public class PaperDto { this.status = paper.getStatus(); this.createDate = paper.getCreateDate(); this.updateDate = paper.getUpdateDate(); - this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new); + this.deadlines = paper.getDeadlines(); this.comment = paper.getComment(); this.locked = paper.getLocked(); this.tmpFileName = null; @@ -126,11 +126,11 @@ public class PaperDto { this.updateDate = updateDate; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index fdce26b..db7fc75 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -3,20 +3,18 @@ package ru.ulstu.project.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import java.util.ArrayList; import java.util.List; -import static ru.ulstu.core.util.StreamApiUtils.convert; - public class ProjectDto { private Integer id; @NotEmpty private String title; - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); public ProjectDto() { } @@ -28,7 +26,7 @@ public class ProjectDto { @JsonCreator public ProjectDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, - @JsonProperty("deadlines") List deadlines) { + @JsonProperty("deadlines") List deadlines) { this.id = id; this.title = title; this.deadlines = deadlines; @@ -38,7 +36,7 @@ public class ProjectDto { public ProjectDto(Project project) { this.id = project.getId(); this.title = project.getTitle(); - this.deadlines = convert(project.getDeadlines(), DeadlineDto::new); + this.deadlines = project.getDeadlines(); } public Integer getId() { @@ -57,11 +55,11 @@ public class ProjectDto { this.title = title; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } }