#74 adding models, controllers and services
parent
b9af7c20c7
commit
9b697c6f3f
@ -0,0 +1,16 @@
|
||||
package ru.ulstu.students.controller;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
public class Navigation {
|
||||
public static final String REDIRECT_TO = "redirect:%s";
|
||||
public static final String TASKS_PAGE = "/students/tasks";
|
||||
public static final String TASK_PAGE = "/students/task";
|
||||
|
||||
public static String hasErrors(Errors errors, String page) {
|
||||
if (errors.hasErrors()) {
|
||||
return page;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
package ru.ulstu.students.model;
|
||||
|
||||
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.Deadline;
|
||||
import ru.ulstu.tags.model.TagDto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
|
||||
public class TaskDto {
|
||||
|
||||
private final static int MAX_TAGS_LENGTH = 50;
|
||||
|
||||
private Integer id;
|
||||
@NotEmpty
|
||||
private String title;
|
||||
private String description;
|
||||
private Task.TaskStatus status;
|
||||
private List<Deadline> deadlines = new ArrayList<>();
|
||||
private Date createDate;
|
||||
private Date updateDate;
|
||||
private Set<Integer> tagIds;
|
||||
private Set<TagDto> tags;
|
||||
|
||||
public TaskDto() {
|
||||
deadlines.add(new Deadline());
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public TaskDto(@JsonProperty("id") Integer id,
|
||||
@JsonProperty("title") String title,
|
||||
@JsonProperty("description") String description,
|
||||
@JsonProperty("createDate") Date createDate,
|
||||
@JsonProperty("updateDate") Date updateDate,
|
||||
@JsonProperty("status") Task.TaskStatus status,
|
||||
@JsonProperty("deadlines") List<Deadline> deadlines,
|
||||
@JsonProperty("tagIds") Set<Integer> tagIds,
|
||||
@JsonProperty("tagIds") Set<TagDto> tags){
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.status = status;
|
||||
this.deadlines = deadlines;
|
||||
this.createDate = createDate;
|
||||
this.updateDate = updateDate;
|
||||
this.description = description;
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public TaskDto(Task task) {
|
||||
this.id = task.getId();
|
||||
this.title = task.getTitle();
|
||||
this.status = task.getStatus();
|
||||
this.deadlines = task.getDeadlines();
|
||||
this.createDate = task.getCreateDate();
|
||||
this.updateDate = task.getUpdateDate();
|
||||
this.description = task.getDescription();
|
||||
this.tagIds = convert(task.getTags(), tag -> tag.getId());
|
||||
this.tags = convert(task.getTags(), TagDto::new);
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Task.TaskStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Task.TaskStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public List<Deadline> getDeadlines() {
|
||||
return deadlines;
|
||||
}
|
||||
|
||||
public void setDeadlines(List<Deadline> deadlines) {
|
||||
this.deadlines = deadlines;
|
||||
}
|
||||
|
||||
public Date getCreateDate() {
|
||||
return createDate;
|
||||
}
|
||||
|
||||
public void setCreateDate(Date createDate) {
|
||||
this.createDate = createDate;
|
||||
}
|
||||
|
||||
public Date getUpdateDate() {
|
||||
return updateDate;
|
||||
}
|
||||
|
||||
public void setUpdateDate(Date updateDate) {
|
||||
this.updateDate = updateDate;
|
||||
}
|
||||
|
||||
public Set<Integer> getTagIds() {
|
||||
return tagIds;
|
||||
}
|
||||
|
||||
public void setTagIds(Set<Integer> tagIds) {
|
||||
this.tagIds = tagIds;
|
||||
}
|
||||
|
||||
public Set<TagDto> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Set<TagDto> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public String getTagsString() {
|
||||
return StringUtils.abbreviate(tags
|
||||
.stream()
|
||||
.map(tag -> tag.getTagName())
|
||||
.collect(Collectors.joining(", ")), MAX_TAGS_LENGTH );
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package ru.ulstu.students.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ulstu.students.model.Task;
|
||||
|
||||
public interface TaskRepository extends JpaRepository<Task, Integer> {
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package ru.ulstu.students.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.deadline.service.DeadlineService;
|
||||
import ru.ulstu.students.model.Task;
|
||||
import ru.ulstu.students.model.TaskDto;
|
||||
import ru.ulstu.students.repository.TaskRepository;
|
||||
import ru.ulstu.tags.model.Tag;
|
||||
import ru.ulstu.tags.service.TagService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
import static ru.ulstu.students.model.Task.TaskStatus.IN_WORK;
|
||||
|
||||
@Service
|
||||
public class TaskService {
|
||||
|
||||
private final static int MAX_DISPLAY_SIZE = 40;
|
||||
|
||||
private final TaskRepository taskRepository;
|
||||
private final DeadlineService deadlineService;
|
||||
private final TagService tagService;
|
||||
|
||||
public TaskService(TaskRepository grantRepository,
|
||||
DeadlineService deadlineService, TagService tagService){
|
||||
this.taskRepository = grantRepository;
|
||||
this.deadlineService = deadlineService;
|
||||
this.tagService = tagService;
|
||||
}
|
||||
|
||||
public List<Task> findAll() {
|
||||
return taskRepository.findAll();
|
||||
}
|
||||
|
||||
public List<TaskDto> findAllDto() {
|
||||
List<TaskDto> tasks = convert(findAll(), TaskDto::new);
|
||||
tasks.forEach(taskDto -> taskDto.setTitle(StringUtils.abbreviate(taskDto.getTitle(), MAX_DISPLAY_SIZE)));
|
||||
return tasks;
|
||||
}
|
||||
|
||||
public TaskDto findOneDto(Integer id) {
|
||||
return new TaskDto(taskRepository.findOne(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Integer create(TaskDto taskDto) throws IOException {
|
||||
Task newTask = copyFromDto(new Task(), taskDto);
|
||||
newTask = taskRepository.save(newTask);
|
||||
return newTask.getId();
|
||||
}
|
||||
|
||||
private Task copyFromDto(Task task, TaskDto taskDto) throws IOException {
|
||||
task.setTitle(taskDto.getTitle());
|
||||
task.setDescription(taskDto.getDescription());
|
||||
task.setStatus(taskDto.getStatus() == null ? IN_WORK : taskDto.getStatus());
|
||||
task.setDeadlines(deadlineService.saveOrCreate(taskDto.getDeadlines()));
|
||||
task.setCreateDate(task.getCreateDate() == null ? new Date() : task.getCreateDate());
|
||||
task.setUpdateDate(new Date());
|
||||
task.getTags().clear();
|
||||
if (taskDto.getTagIds() != null && !taskDto.getTagIds().isEmpty()) {
|
||||
taskDto.getTagIds().forEach(tagIds -> task.getTags().add(tagService.findById(tagIds)));
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Integer update(TaskDto taskDto) throws IOException {
|
||||
Task task = taskRepository.findOne(taskDto.getId());
|
||||
taskRepository.save(copyFromDto(task, taskDto));
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Integer taskId) throws IOException {
|
||||
Task task = taskRepository.findOne(taskId);
|
||||
taskRepository.delete(task);
|
||||
}
|
||||
|
||||
public void save(TaskDto taskDto) throws IOException {
|
||||
if (isEmpty(taskDto.getId())) {
|
||||
create(taskDto);
|
||||
} else {
|
||||
update(taskDto);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Task.TaskStatus> getTaskStatuses() {
|
||||
return Arrays.asList(Task.TaskStatus.values());
|
||||
}
|
||||
public List<Tag> getTaskTags() {
|
||||
return tagService.findAll();
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ru.ulstu.tags.model;
|
||||
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import ru.ulstu.core.model.BaseEntity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tag")
|
||||
public class Tag extends BaseEntity {
|
||||
|
||||
@NotBlank
|
||||
private String tagName;
|
||||
|
||||
public String getTagName() {
|
||||
return tagName;
|
||||
}
|
||||
|
||||
public void setTagName(String tagName) {
|
||||
this.tagName = tagName;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package ru.ulstu.tags.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class TagDto {
|
||||
|
||||
private Integer id;
|
||||
@NotEmpty
|
||||
@Size(max = 50)
|
||||
private String tagName;
|
||||
|
||||
@JsonCreator
|
||||
public TagDto(@JsonProperty("id") Integer id,
|
||||
@JsonProperty("tagName") String tagName) {
|
||||
this.id = id;
|
||||
this.tagName = tagName;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTagName() {
|
||||
return tagName;
|
||||
}
|
||||
|
||||
public void setTagName(String tagName) {
|
||||
this.tagName = tagName;
|
||||
}
|
||||
|
||||
public TagDto(Tag tag) {
|
||||
this.id = tag.getId();
|
||||
this.tagName = tag.getTagName();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ru.ulstu.tags.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ulstu.tags.model.Tag;
|
||||
|
||||
public interface TagRepository extends JpaRepository<Tag, Integer> {
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package ru.ulstu.tags.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.tags.model.Tag;
|
||||
import ru.ulstu.tags.model.TagDto;
|
||||
import ru.ulstu.tags.repository.TagRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
|
||||
@Service
|
||||
public class TagService {
|
||||
|
||||
private final TagRepository tagRepository;
|
||||
|
||||
|
||||
public TagService(TagRepository tagRepository) {
|
||||
|
||||
this.tagRepository = tagRepository;
|
||||
}
|
||||
|
||||
public List<Tag> findAll() {
|
||||
return tagRepository.findAll();
|
||||
}
|
||||
|
||||
// public List<TagDto> findAllDto() {
|
||||
// List<TagDto> tag = convert(findAll(), TagDto::new);
|
||||
// tags.forEach(tagDto -> tagDto.setTitle(StringUtils.abbreviate(tagDto.getTitle(), MAX_DISPLAY_SIZE)));
|
||||
// return tags;
|
||||
// }
|
||||
//
|
||||
// public TagDto findOneDto(Integer id) {
|
||||
// return new TagDto(tagRepository.findOne(id));
|
||||
// }
|
||||
|
||||
|
||||
public List<Tag> findByIds(List<Integer> ids) {
|
||||
return tagRepository.findAll(ids);
|
||||
}
|
||||
public Tag findById(Integer id) {
|
||||
return tagRepository.findOne(id);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue