#74 adding models, controllers and services

This commit is contained in:
ASH 2019-04-09 23:18:39 +04:00
parent b9af7c20c7
commit 9b697c6f3f
15 changed files with 636 additions and 57 deletions

View File

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

View File

@ -0,0 +1,85 @@
package ru.ulstu.students.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.students.model.Task;
import ru.ulstu.students.model.TaskDto;
import ru.ulstu.tags.model.Tag;
import springfox.documentation.annotations.ApiIgnore;
import ru.ulstu.students.service.TaskService;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty;
import static ru.ulstu.students.controller.Navigation.*;
@Controller()
@RequestMapping(value = "/students")
@ApiIgnore
public class TaskController {
private final TaskService taskService;
public TaskController(TaskService grantService) {
this.taskService = grantService;
}
@GetMapping("/tasks")
public void getTasks(ModelMap modelMap) {
modelMap.put("tasks", taskService.findAllDto());
}
@GetMapping("/dashboard")
public void getDashboard(ModelMap modelMap) {
modelMap.put("tasks", taskService.findAllDto());
}
@GetMapping("/task")
public void getTask(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) {
modelMap.put("taskDto", taskService.findOneDto(id));
} else {
modelMap.put("taskDto", new TaskDto());
}
}
@PostMapping(value = "/task", params = "save")
public String save(@Valid TaskDto taskDto, Errors errors) throws IOException {
filterEmptyDeadlines(taskDto);
if (taskDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
hasErrors(errors, TASK_PAGE);
taskService.save(taskDto);
return String.format(REDIRECT_TO, TASK_PAGE);
}
@PostMapping(value = "/task", params = "addDeadline")
public String addDeadline(@Valid TaskDto taskDto, Errors errors) {
filterEmptyDeadlines(taskDto);
hasErrors(errors, TASK_PAGE);
taskDto.getDeadlines().add(new Deadline());
return TASK_PAGE;
}
@ModelAttribute("allStatuses")
public List<Task.TaskStatus> getTaskStatuses() {
return taskService.getTaskStatuses();
}
@ModelAttribute("allTags")
public List<Tag> getAllTags() {
return taskService.getTaskTags();
}
private void filterEmptyDeadlines(TaskDto taskDto) {
taskDto.setDeadlines(taskDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
}

View File

@ -0,0 +1,113 @@
package ru.ulstu.students.model;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.tags.model.Tag;
import javax.persistence.*;
import java.util.*;
@Entity
public class Task extends BaseEntity {
public enum TaskStatus {
IN_WORK("В работе"),
COMPLETED("Завершен"),
FAILED("Провалены сроки"),
LOADED_FROM_KIAS("Загружен автоматически");
private String statusName;
TaskStatus(String name) {
this.statusName = name;
}
public String getStatusName() {
return statusName;
}
}
@NotBlank
private String title;
private String description;
@Enumerated(value = EnumType.STRING)
private ru.ulstu.students.model.Task.TaskStatus status = TaskStatus.IN_WORK;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "task_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
@OrderBy("date")
private List<Deadline> deadlines = new ArrayList<>();
@Column(name = "create_date")
@Temporal(TemporalType.TIMESTAMP)
private Date createDate = new Date();
@Column(name = "update_date")
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate = new Date();
@ManyToMany(fetch = FetchType.EAGER)
private Set<Tag> tags = new HashSet<>();
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public TaskStatus getStatus() {
return status;
}
public void setStatus(TaskStatus status) {
this.status = status;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
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<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,14 +7,14 @@
<div th:fragment="taskDashboard (task)" class="col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3 dashboard-card">
<div class="row">
<div class="col-2">
<!--<span th:replace="students/fragments/taskStatusFragment :: taskStatus(taskStatus=${task.status})"/>-->
<span th:replace="students/fragments/taskStatusFragment"/>
<span th:replace="students/fragments/taskStatusFragment :: taskStatus(taskStatus=${task.status})"/>
<!--<span th:replace="students/fragments/taskStatusFragment"/>-->
</div>
<div class="col col-10 text-right">
<!--<h7 class="service-heading" th:text="${task.title}"> title</h7>-->
<!--<p class="text-muted" th:text="${task.typeString}">authors</p>-->
<h7 class="service-heading" th:text="Title"> title</h7>
<p class="text-muted" th:text="Type">type</p>
<h7 class="service-heading" th:text="${task.title}"> title</h7>
<p class="text-muted" th:text="${task.status.statusName}"> status</p>
<!--<h7 class="service-heading" th:text="Title"> title</h7>-->
<!--<p class="text-muted" th:text="Type">type</p>-->
</div>
</div>
</div>

View File

@ -4,15 +4,15 @@
<meta charset="UTF-8"/>
</head>
<body>
<div class="row text-left task-row" style="background-color: white;">
<div th:fragment="taskLine (task)" class class="row text-left task-row" style="background-color: white;">
<div class="col">
<span th:replace="students/fragments/taskStatusFragment"/>
<a th:href="@{'task?id='+ 1}">
<span class="h6" >Первая хадач</span>
<span class="text-muted">Курсовая работа</span>
<span th:replace="students/fragments/taskStatusFragment :: taskStatus(taskStatus=${task.status})" />
<a th:href="@{'task?id='+${task.id}}">
<span class="h6" th:text="${task.title}"></span>
<span class="text-muted" th:text="${paper.tagsString}" />
</a>
<input class="id-class" type="hidden" th:value="1" />
<a class="remove-task pull-right d-none" th:href="@{'/students/delete/'+1}"
<input class="id-class" type="hidden" th:value="${task.id}" />
<a class="remove-task pull-right d-none" th:href="@{'/students/delete/'+${task.id}}"
data-confirm="Удалить задачу?">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>

View File

@ -5,18 +5,14 @@
</head>
<body>
<span th:fragment="taskStatus (taskStatus)" class="fa-stack fa-1x">
<!--<th:block th:switch="${taskStatus.name()}">-->
<th:block th:switch="'ON_PREPARATION'">
<div th:case="'ATTENTION'">
<i class="fa fa-circle fa-stack-2x text-warning"></i>
</div>
<div th:case="'DRAFT'">
<i class="fa fa-circle fa-stack-2x text-draft"></i>
</div>
<div th:case="'ON_PREPARATION'">
<th:block th:switch="${taskStatus.name()}">
<!--<div th:case="'ATTENTION'">-->
<!--<i class="fa fa-circle fa-stack-2x text-warning"></i>-->
<!--</div>-->
<div th:case="'IN_WORK'">
<i class="fa fa-circle fa-stack-2x text-primary"></i>
</div>
<div th:case="'ON_REVIEW'">
<div th:case="'LOADED_FROM_KIAS'">
<i class="fa fa-circle fa-stack-2x text-review"></i>
</div>
<div th:case="'COMPLETED'">
@ -25,12 +21,6 @@
<div th:case="'FAILED'">
<i class="fa fa-circle fa-stack-2x text-failed"></i>
</div>
<div th:case="'ACCEPTED'">
<i class="fa fa-circle fa-stack-2x text-accepted"></i>
</div>
<div th:case="'NOT_ACCEPTED'">
<i class="fa fa-circle fa-stack-2x text-not-accepted"></i>
</div>
</th:block>
<i class="fa fa-check fa-stack-1x fa-inverse"></i>
</span>

View File

@ -21,47 +21,60 @@
<hr/>
<div class="row">
<div class="col-lg-12">
<form id="task-form" method="post">
<form id="task-form" method="post" th:action="@{'/tasks/task?id='+ *{id == null ? '' : id} + ''}"
th:object="${taskDto}">
<div class="row">
<div class="col-md-7 col-sm-12">
<input type="hidden" name="id"/>
<input type="hidden" name="id" th:field="*{id}"/>
<div class="form-group">
<label for="title">Название:</label>
<input class="form-control" id="title" type="text" placeholder="Название задачи"/>
<input class="form-control" id="title" type="text" placeholder="Название задачи"
th:field="*{title}"/>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}"
class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="status">Статус:</label>
<select class="form-control" id="status">
<option>Status</option>
<select class="form-control" id="status" th:field="*{status}">
<option th:each="status : ${allStatuses}" th:value="${status}"
th:text="${status.statusName}">Status
</option>
</select>
</div>
<div class="form-group">
<label for="comment">Описание задачи:</label>
<textarea class="form-control" rows="3" id="comment"></textarea>
<textarea class="form-control" rows="3" id="comment" th:field="*{description}"></textarea>
</div>
<div class="form-group">
<label for="tags">Теги:</label>
<input class="form-control" data-role="tagsinput" placeholder="Теги задачи" id="tags"/>
</div>
<div class="form-group">
<div class="form-group" >
<label>Дедлайны задачи:</label>
<div class="row">
<input type="hidden" />
<div class="row" th:each="deadline, rowStat : *{deadlines}">
<input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}" />
<div class="col-6">
<input type="date" class="form-control" name="deadline"/>
<input type="date" class="form-control" name="deadline"
th:field="*{deadlines[__${rowStat.index}__].date}"/>
</div>
<div class="col-4">
<input class="form-control" type="text" placeholder="Описание"/>
<input class="form-control" type="text" placeholder="Описание"
th:field="*{deadlines[__${rowStat.index}__].description}"/>
</div>
<div class="col-2">
<a class="btn btn-danger float-right"><span
<a class="btn btn-danger float-right"
th:onclick="|$('#deadlines${rowStat.index}\\.description').val('');
$('#deadlines${rowStat.index}\\.date').val('');
$('#addDeadline').click();|"><span
aria-hidden="true"><i class="fa fa-times"/></span>
</a>
</div>
</div>
<p th:if="${#fields.hasErrors('deadlines')}" th:errors="*{deadlines}"
class="alert alert-danger">Incorrect title</p>
</div>
<div class="form-group">
<input type="submit" id="addDeadline" name="addDeadline" class="btn btn-primary" value="Добавить
@ -85,8 +98,8 @@
<h6 class="my-0">Дата создания:</h6>
</div>
<div class="col">
<small class="text-muted">
<!--th:text="${taskDto.createDate == null ? '' : #dates.format(taskDto.createDate, 'dd.MM.yyyy HH:mm')}">-->
<small class="text-muted"
th:text="${taskDto.createDate == null ? '' : #dates.format(taskDto.createDate, 'dd.MM.yyyy HH:mm')}">
text
</small>
</div>
@ -98,8 +111,8 @@
<h6 class="my-0">Дата изменения:</h6>
</div>
<div class="col">
<small class="text-muted">
<!--th:text="${paperDto.updateDate == null ? '' : #dates.format(paperDto.updateDate, 'dd.MM.yyyy HH:mm')}">-->
<small class="text-muted"
th:text="${taskDto.updateDate == null ? '' : #dates.format(taskDto.updateDate, 'dd.MM.yyyy HH:mm')}">
text
</small>
</div>

View File

@ -20,8 +20,8 @@
<hr/>
<div class="row">
<div class="col-md-9 col-sm-12">
<th:block>
<div th:replace="students/fragments/taskLineFragment"/>
<th:block th:each="task : ${tasks.tasks}">
<div th:replace="students/fragments/taskLineFragment :: taskLine(task=${task})"/>
</th:block>
</div>
<div class="col-md-3 col-sm-12">
@ -37,16 +37,6 @@
<select class="form-control" id="tags"
onchange="this.form.submit();">
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<option value="">Все типы</option>
<!--<option th:each="year: ${allYears}" th:value="${year}"-->
<!--th:text="${year}">year-->
<!--</option>-->