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; 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 ru.ulstu.core.model.BaseEntity;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -13,6 +16,7 @@ public class Deadline extends BaseEntity {
private String description; private String description;
@Temporal(value = TemporalType.TIMESTAMP) @Temporal(value = TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date; private Date date;
public Deadline() { public Deadline() {
@ -23,6 +27,15 @@ public class Deadline extends BaseEntity {
this.description = description; 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() { public String getDescription() {
return description; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.deadline.repository.DeadlineRepository; import ru.ulstu.deadline.repository.DeadlineRepository;
import java.util.List; import java.util.List;
@ -17,29 +16,29 @@ public class DeadlineService {
this.deadlineRepository = deadlineRepository; this.deadlineRepository = deadlineRepository;
} }
public List<Deadline> saveOrCreate(List<DeadlineDto> deadlines) { public List<Deadline> saveOrCreate(List<Deadline> deadlines) {
return deadlines.stream().map(deadlineDto -> { return deadlines
return deadlineDto.getId() != null ? update(deadlineDto) : create(deadlineDto); .stream()
.map(deadline -> {
return deadline.getId() != null ? update(deadline) : create(deadline);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
@Transactional @Transactional
public Deadline update(DeadlineDto deadlineDto) { public Deadline update(Deadline deadline) {
Deadline deadline = deadlineRepository.findOne(deadlineDto.getId()); Deadline updateDeadline = deadlineRepository.findOne(deadline.getId());
deadlineRepository.save(copyFromDto(deadline, deadlineDto)); updateDeadline.setDate(deadline.getDate());
return deadline; updateDeadline.setDescription(deadline.getDescription());
deadlineRepository.save(updateDeadline);
return updateDeadline;
} }
@Transactional @Transactional
public Deadline create(DeadlineDto deadlineDto) { public Deadline create(Deadline deadline) {
Deadline newDeadline = copyFromDto(new Deadline(), deadlineDto); Deadline newDeadline = new Deadline();
newDeadline.setDate(deadline.getDate());
newDeadline.setDescription(deadline.getDescription());
newDeadline = deadlineRepository.save(newDeadline); newDeadline = deadlineRepository.save(newDeadline);
return 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; 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.Grant;
import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.service.GrantService; import ru.ulstu.grant.service.GrantService;
@ -20,6 +20,10 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty; 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() @Controller()
@ -56,36 +60,30 @@ public class GrantController {
if (grantDto.getDeadlines().isEmpty()) { if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
} }
if (errors.hasErrors()) { hasErrors(errors, GRANT_PAGE);
return "/grants/grant";
}
grantService.save(grantDto); grantService.save(grantDto);
return "redirect:/grants/grants"; return String.format(REDIRECT_TO, GRANTS_PAGE);
} }
@PostMapping(value = "/grant", params = "addDeadline") @PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) { public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto); filterEmptyDeadlines(grantDto);
if (errors.hasErrors()) { hasErrors(errors, GRANT_PAGE);
return "/grants/grant"; grantDto.getDeadlines().add(new Deadline());
} return GRANT_PAGE;
grantDto.getDeadlines().add(new DeadlineDto());
return "/grants/grant";
} }
@PostMapping(value = "/grant", params = "createProject") @PostMapping(value = "/grant", params = "createProject")
public String createProject(@Valid GrantDto grantDto, Errors errors) { public String createProject(@Valid GrantDto grantDto, Errors errors) {
if (errors.hasErrors()) { hasErrors(errors, GRANT_PAGE);
return "/grants/grant";
}
grantService.createProject(grantDto); grantService.createProject(grantDto);
return "/grants/grant"; return GRANT_PAGE;
} }
@GetMapping("/delete/{grant-id}") @GetMapping("/delete/{grant-id}")
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
grantService.delete(grantId); grantService.delete(grantId);
return "redirect:/grants/grants"; return String.format(REDIRECT_TO, GRANTS_PAGE);
} }
@ModelAttribute("allStatuses") @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("Успешное прохождение"), SUCCESSFUL_PASSAGE("Успешное прохождение"),
IN_WORK("В работе"), IN_WORK("В работе"),
COMPLETED("Завершен"), COMPLETED("Завершен"),
FAILED("Провалены сроки"); FAILED("Провалены сроки"),
LOADED_FROM_KIAS("Загружен автоматически");
private String statusName; 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.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty; 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 ru.ulstu.project.model.ProjectDto;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class GrantDto { public class GrantDto {
private Integer id; private Integer id;
@NotEmpty @NotEmpty
private String title; private String title;
private Grant.GrantStatus status; private Grant.GrantStatus status;
private List<DeadlineDto> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();
private String comment; private String comment;
private String applicationFileName; private String applicationFileName;
private ProjectDto project; private ProjectDto project;
public GrantDto() { public GrantDto() {
deadlines.add(new DeadlineDto()); deadlines.add(new Deadline());
} }
@JsonCreator @JsonCreator
public GrantDto(@JsonProperty("id") Integer id, public GrantDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title, @JsonProperty("title") String title,
@JsonProperty("status") Grant.GrantStatus status, @JsonProperty("status") Grant.GrantStatus status,
@JsonProperty("deadlines") List<DeadlineDto> deadlines, @JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("comment") String comment, @JsonProperty("comment") String comment,
@JsonProperty("project") ProjectDto project) { @JsonProperty("project") ProjectDto project) {
this.id = id; this.id = id;
@ -45,7 +43,7 @@ public class GrantDto {
this.id = grant.getId(); this.id = grant.getId();
this.title = grant.getTitle(); this.title = grant.getTitle();
this.status = grant.getStatus(); this.status = grant.getStatus();
this.deadlines = convert(grant.getDeadlines(), DeadlineDto::new); this.deadlines = grant.getDeadlines();
this.comment = grant.getComment(); this.comment = grant.getComment();
this.project = grant.getProject() == null ? null : new ProjectDto(grant.getProject()); this.project = grant.getProject() == null ? null : new ProjectDto(grant.getProject());
this.applicationFileName = grant.getApplication() == null ? null : grant.getApplication().getName(); this.applicationFileName = grant.getApplication() == null ? null : grant.getApplication().getName();
@ -75,11 +73,11 @@ public class GrantDto {
this.status = status; this.status = status;
} }
public List<DeadlineDto> getDeadlines() { public List<Deadline> getDeadlines() {
return deadlines; return deadlines;
} }
public void setDeadlines(List<DeadlineDto> deadlines) { public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; 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.Paper;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto; import ru.ulstu.paper.model.PaperFilterDto;
@ -80,7 +80,7 @@ public class PaperController {
if (errors.hasErrors()) { if (errors.hasErrors()) {
return "/papers/paper"; return "/papers/paper";
} }
paperDto.getDeadlines().add(new DeadlineDto()); paperDto.getDeadlines().add(new Deadline());
return "/papers/paper"; return "/papers/paper";
} }

View File

@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty; 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 ru.ulstu.user.model.UserDto;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -27,7 +27,7 @@ public class PaperDto {
private Date createDate; private Date createDate;
private Date updateDate; private Date updateDate;
@NotEmpty @NotEmpty
private List<DeadlineDto> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();
private String comment; private String comment;
private Boolean locked; private Boolean locked;
private String tmpFileName; private String tmpFileName;
@ -39,7 +39,7 @@ public class PaperDto {
private Integer filterAuthorId; private Integer filterAuthorId;
public PaperDto() { public PaperDto() {
deadlines.add(new DeadlineDto()); deadlines.add(new Deadline());
} }
@JsonCreator @JsonCreator
@ -48,7 +48,7 @@ public class PaperDto {
@JsonProperty("status") Paper.PaperStatus status, @JsonProperty("status") Paper.PaperStatus status,
@JsonProperty("createDate") Date createDate, @JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate, @JsonProperty("updateDate") Date updateDate,
@JsonProperty("deadlines") List<DeadlineDto> deadlines, @JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("comment") String comment, @JsonProperty("comment") String comment,
@JsonProperty("locked") Boolean locked, @JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName, @JsonProperty("tmpFileName") String tmpFileName,
@ -75,7 +75,7 @@ public class PaperDto {
this.status = paper.getStatus(); this.status = paper.getStatus();
this.createDate = paper.getCreateDate(); this.createDate = paper.getCreateDate();
this.updateDate = paper.getUpdateDate(); this.updateDate = paper.getUpdateDate();
this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new); this.deadlines = paper.getDeadlines();
this.comment = paper.getComment(); this.comment = paper.getComment();
this.locked = paper.getLocked(); this.locked = paper.getLocked();
this.tmpFileName = null; this.tmpFileName = null;
@ -126,11 +126,11 @@ public class PaperDto {
this.updateDate = updateDate; this.updateDate = updateDate;
} }
public List<DeadlineDto> getDeadlines() { public List<Deadline> getDeadlines() {
return deadlines; return deadlines;
} }
public void setDeadlines(List<DeadlineDto> deadlines) { public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = 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.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty; 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.ArrayList;
import java.util.List; import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class ProjectDto { public class ProjectDto {
private Integer id; private Integer id;
@NotEmpty @NotEmpty
private String title; private String title;
private List<DeadlineDto> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();
public ProjectDto() { public ProjectDto() {
} }
@ -28,7 +26,7 @@ public class ProjectDto {
@JsonCreator @JsonCreator
public ProjectDto(@JsonProperty("id") Integer id, public ProjectDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title, @JsonProperty("title") String title,
@JsonProperty("deadlines") List<DeadlineDto> deadlines) { @JsonProperty("deadlines") List<Deadline> deadlines) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.deadlines = deadlines; this.deadlines = deadlines;
@ -38,7 +36,7 @@ public class ProjectDto {
public ProjectDto(Project project) { public ProjectDto(Project project) {
this.id = project.getId(); this.id = project.getId();
this.title = project.getTitle(); this.title = project.getTitle();
this.deadlines = convert(project.getDeadlines(), DeadlineDto::new); this.deadlines = project.getDeadlines();
} }
public Integer getId() { public Integer getId() {
@ -57,11 +55,11 @@ public class ProjectDto {
this.title = title; this.title = title;
} }
public List<DeadlineDto> getDeadlines() { public List<Deadline> getDeadlines() {
return deadlines; return deadlines;
} }
public void setDeadlines(List<DeadlineDto> deadlines) { public void setDeadlines(List<Deadline> deadlines) {
this.deadlines = deadlines; this.deadlines = deadlines;
} }
} }

View File

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