Merge branch '50-refactorForGrants' into 'dev'

Resolve "Тех. долг: отрефакторить контроллеры и модели"

Closes #50

See merge request romanov73/ng-tracker!32
This commit is contained in:
Anton Romanov 2019-03-18 11:08:15 +00:00
commit 9418a47a1d
11 changed files with 83 additions and 118 deletions

View File

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

View File

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

View File

@ -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<Deadline> saveOrCreate(List<DeadlineDto> deadlines) {
return deadlines.stream().map(deadlineDto -> {
return deadlineDto.getId() != null ? update(deadlineDto) : create(deadlineDto);
}).collect(Collectors.toList());
public List<Deadline> saveOrCreate(List<Deadline> 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;
}
}

View File

@ -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;
@ -20,6 +20,10 @@ import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty;
import static ru.ulstu.grant.controller.Navigation.GRANTS_PAGE;
import static ru.ulstu.grant.controller.Navigation.GRANT_PAGE;
import static ru.ulstu.grant.controller.Navigation.REDIRECT_TO;
import static ru.ulstu.grant.controller.Navigation.hasErrors;
@Controller()
@ -56,36 +60,30 @@ public class GrantController {
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
if (errors.hasErrors()) {
return "/grants/grant";
}
hasErrors(errors, GRANT_PAGE);
grantService.save(grantDto);
return "redirect:/grants/grants";
return String.format(REDIRECT_TO, GRANTS_PAGE);
}
@PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto);
if (errors.hasErrors()) {
return "/grants/grant";
}
grantDto.getDeadlines().add(new DeadlineDto());
return "/grants/grant";
hasErrors(errors, GRANT_PAGE);
grantDto.getDeadlines().add(new Deadline());
return GRANT_PAGE;
}
@PostMapping(value = "/grant", params = "createProject")
public String createProject(@Valid GrantDto grantDto, Errors errors) {
if (errors.hasErrors()) {
return "/grants/grant";
}
hasErrors(errors, GRANT_PAGE);
grantService.createProject(grantDto);
return "/grants/grant";
return GRANT_PAGE;
}
@GetMapping("/delete/{grant-id}")
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
grantService.delete(grantId);
return "redirect:/grants/grants";
return String.format(REDIRECT_TO, GRANTS_PAGE);
}
@ModelAttribute("allStatuses")

View File

@ -0,0 +1,16 @@
package ru.ulstu.grant.controller;
import org.springframework.validation.Errors;
public class Navigation {
public static final String REDIRECT_TO = "redirect:%s";
public static final String GRANTS_PAGE = "/grants/grants";
public static final String GRANT_PAGE = "/grants/grant";
public static String hasErrors(Errors errors, String page) {
if (errors.hasErrors()) {
return page;
}
return null;
}
}

View File

@ -30,7 +30,8 @@ public class Grant extends BaseEntity {
SUCCESSFUL_PASSAGE("Успешное прохождение"),
IN_WORK("В работе"),
COMPLETED("Завершен"),
FAILED("Провалены сроки");
FAILED("Провалены сроки"),
LOADED_FROM_KIAS("Загружен автоматически");
private String statusName;

View File

@ -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<DeadlineDto> deadlines = new ArrayList<>();
private List<Deadline> 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<DeadlineDto> deadlines,
@JsonProperty("deadlines") List<Deadline> 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<DeadlineDto> getDeadlines() {
public List<Deadline> getDeadlines() {
return deadlines;
}
public void setDeadlines(List<DeadlineDto> deadlines) {
public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = deadlines;
}

View File

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

View File

@ -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<DeadlineDto> deadlines = new ArrayList<>();
private List<Deadline> 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<DeadlineDto> deadlines,
@JsonProperty("deadlines") List<Deadline> 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<DeadlineDto> getDeadlines() {
public List<Deadline> getDeadlines() {
return deadlines;
}
public void setDeadlines(List<DeadlineDto> deadlines) {
public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = deadlines;
}

View File

@ -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<DeadlineDto> deadlines = new ArrayList<>();
private List<Deadline> 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<DeadlineDto> deadlines) {
@JsonProperty("deadlines") List<Deadline> 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<DeadlineDto> getDeadlines() {
public List<Deadline> getDeadlines() {
return deadlines;
}
public void setDeadlines(List<DeadlineDto> deadlines) {
public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = deadlines;
}
}

View File

@ -8,7 +8,7 @@
<meta name="description" content=""/>
<meta name="author" content=""/>
<title>NG-Tacker</title>
<title>NG-Tracker</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="/webjars/bootstrap/4.1.0/css/bootstrap.min.css"/>