#74 fixing adding and db everything added except tags

This commit is contained in:
ASH 2019-04-10 23:57:55 +04:00
parent 9b697c6f3f
commit f019806a8e
14 changed files with 118 additions and 74 deletions

View File

@ -8,6 +8,7 @@ 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 ru.ulstu.tags.model.TagDto;
import springfox.documentation.annotations.ApiIgnore;
import ru.ulstu.students.service.TaskService;
import javax.validation.Valid;
@ -25,8 +26,8 @@ public class TaskController {
private final TaskService taskService;
public TaskController(TaskService grantService) {
this.taskService = grantService;
public TaskController(TaskService taskService) {
this.taskService = taskService;
}
@GetMapping("/tasks")
@ -54,15 +55,17 @@ public class TaskController {
if (taskDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
hasErrors(errors, TASK_PAGE);
if(errors.hasErrors())
return TASK_PAGE;
taskService.save(taskDto);
return String.format(REDIRECT_TO, TASK_PAGE);
return String.format(REDIRECT_TO, TASKS_PAGE);
}
@PostMapping(value = "/task", params = "addDeadline")
public String addDeadline(@Valid TaskDto taskDto, Errors errors) {
filterEmptyDeadlines(taskDto);
hasErrors(errors, TASK_PAGE);
if(errors.hasErrors())
return TASK_PAGE;
taskDto.getDeadlines().add(new Deadline());
return TASK_PAGE;
}
@ -72,10 +75,19 @@ public class TaskController {
return taskService.getTaskStatuses();
}
@ModelAttribute("allTags")
public List<Tag> getAllTags() {
return taskService.getTaskTags();
}
// @ModelAttribute("allTags")
// public List<Tag> getAllTags() {
// return taskService.getTaskTags();
// }
// @PostMapping(value = "/task", params = "addTag")
// public String addTag(@Valid TaskDto taskDto, Errors errors) {
// filterEmptyDeadlines(taskDto);
// if(errors.hasErrors())
// return TASK_PAGE;
// taskDto.getTags().add(new TagDto());
// return TASK_PAGE;
// }
private void filterEmptyDeadlines(TaskDto taskDto) {
taskDto.setDeadlines(taskDto.getDeadlines().stream()

View File

@ -6,6 +6,7 @@ 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 ru.ulstu.tags.model.TagDto;
import javax.persistence.*;
import java.util.*;
@ -53,7 +54,10 @@ public class Task extends BaseEntity {
private Date updateDate = new Date();
@ManyToMany(fetch = FetchType.EAGER)
private Set<Tag> tags = new HashSet<>();
@JoinTable(name = "task_tags",
joinColumns = {@JoinColumn(name = "task_id")},
inverseJoinColumns = {@JoinColumn(name = "tag_id")})
private Set<TagDto> tags = new HashSet<>();
public String getTitle() {
return title;
@ -103,11 +107,11 @@ public class Task extends BaseEntity {
this.updateDate = updateDate;
}
public Set<Tag> getTags() {
public Set<TagDto> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
public void setTags(Set<TagDto> tags) {
this.tags = tags;
}
}

View File

@ -43,7 +43,7 @@ public class TaskDto {
@JsonProperty("status") Task.TaskStatus status,
@JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("tagIds") Set<Integer> tagIds,
@JsonProperty("tagIds") Set<TagDto> tags){
@JsonProperty("tags") Set<TagDto> tags){
this.id = id;
this.title = title;
this.status = status;
@ -62,8 +62,8 @@ public class TaskDto {
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);
// this.tagIds = convert(task.getTags(), tag -> tag.getId());
this.tags = task.getTags();
}
public Integer getId() {

View File

@ -8,6 +8,7 @@ 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.model.TagDto;
import ru.ulstu.tags.service.TagService;
import java.io.IOException;
@ -62,9 +63,10 @@ public class TaskService {
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)));
}
task.setTags(tagService.saveOrCreate(taskDto.getTags()));
// if (taskDto.getTagIds() != null && !taskDto.getTagIds().isEmpty()) {
// taskDto.getTagIds().forEach(tagIds -> task.getTags().add(tagService.findById(tagIds)));
// }
return task;
}
@ -92,7 +94,8 @@ public class TaskService {
public List<Task.TaskStatus> getTaskStatuses() {
return Arrays.asList(Task.TaskStatus.values());
}
public List<Tag> getTaskTags() {
return tagService.findAll();
}
// public List<TagDto> getTaskTags() {
// return tagService.;
// }
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.tags.model;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@ -11,6 +12,7 @@ import javax.persistence.Table;
public class Tag extends BaseEntity {
@NotBlank
@Column(name = "tag_name")
private String tagName;
public String getTagName() {

View File

@ -3,29 +3,35 @@ package ru.ulstu.tags.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.core.model.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Size;
public class TagDto {
@Entity
@Table(name = "tag")
public class TagDto extends BaseEntity {
private Integer id;
@NotEmpty
@Size(max = 50)
@Column(name = "tag_name")
private String tagName;
public TagDto() {
}
@JsonCreator
public TagDto(@JsonProperty("id") Integer id,
@JsonProperty("tagName") String tagName) {
this.id = id;
@JsonProperty("tag_name") String tagName) {
this.setId(id);
this.tagName = tagName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
public TagDto(String name) {
this.tagName = name;
}
public String getTagName() {
@ -35,9 +41,4 @@ public class TagDto {
public void setTagName(String tagName) {
this.tagName = tagName;
}
public TagDto(Tag tag) {
this.id = tag.getId();
this.tagName = tag.getTagName();
}
}

View File

@ -1,8 +1,8 @@
package ru.ulstu.tags.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.tags.model.Tag;
import ru.ulstu.tags.model.TagDto;
public interface TagRepository extends JpaRepository<Tag, Integer> {
public interface TagRepository extends JpaRepository<TagDto, Integer>{
}

View File

@ -2,11 +2,12 @@ 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 javax.transaction.Transactional;
import java.util.Set;
import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -21,26 +22,25 @@ public class TagService {
this.tagRepository = tagRepository;
}
public List<Tag> findAll() {
return tagRepository.findAll();
public Set<TagDto> saveOrCreate(Set<TagDto> tagDtos) {
return tagDtos
.stream()
.map(tagDto -> {
return tagRepository.exists(tagDto.getId()) ? takeExist(tagDto) : create(tagDto);
}).collect(Collectors.toSet());
}
// 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);
@Transactional
public TagDto takeExist(TagDto tagDto) {
return tagRepository.findOne(tagDto.getId());
}
public Tag findById(Integer id) {
return tagRepository.findOne(id);
@Transactional
public TagDto create(TagDto tagDto) {
TagDto newTag = new TagDto();
newTag.setTagName(tagDto.getTagName());
newTag = tagRepository.save(newTag);
return newTag;
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="nastya" id="20190410_000000-1">
<addColumn tableName="tag">
<column name="version" type="integer"/>
</addColumn>
<renameColumn tableName="tag" oldColumnName="tagname" newColumnName="tag_name"/>
</changeSet>
<changeSet author="nastya" id="20190410_000000-2">
<addColumn tableName="task">
<column name="version" type="integer"/>
</addColumn>
</changeSet>
</databaseChangeLog>

View File

@ -23,5 +23,6 @@
<include file="db/changelog-20190327_000000-schema.xml"/>
<include file="db/changelog-20190331_000000-schema.xml"/>
<include file="db/changelog-20190331_000010-schema.xml"/>
<include file="db/changelog-20190410_000000-schema.xml"/>
<include file="db/common/changelog-20190312_130000-schema.xml"/>
</databaseChangeLog>

View File

@ -4,12 +4,12 @@
<meta charset="UTF-8"/>
</head>
<body>
<div th:fragment="taskLine (task)" class class="row text-left task-row" style="background-color: white;">
<div th:fragment="taskLine (task)" class="row text-left task-row" style="background-color: white;">
<div class="col">
<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}" />
<span class="text-muted" th:text="${task.tagsString}" />
</a>
<input class="id-class" type="hidden" th:value="${task.id}" />
<a class="remove-task pull-right d-none" th:href="@{'/students/delete/'+${task.id}}"

View File

@ -15,7 +15,7 @@
</div>
<div class="col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3">
<a href="./task" class="btn btn-light toolbar-button"><i class="fa fa-plus-circle"
<a href="./task?id=0" class="btn btn-light toolbar-button"><i class="fa fa-plus-circle"
aria-hidden="true"></i>
Добавить задачу</a>

View File

@ -11,6 +11,7 @@
<div class="container" layout:fragment="content">
<section id="paper">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
@ -21,7 +22,7 @@
<hr/>
<div class="row">
<div class="col-lg-12">
<form id="task-form" method="post" th:action="@{'/tasks/task?id='+ *{id == null ? '' : id} + ''}"
<form id="task-form" method="post" th:action="@{'/students/task?id='+ *{id == null ? '' : id} + ''}"
th:object="${taskDto}">
<div class="row">
<div class="col-md-7 col-sm-12">
@ -45,12 +46,14 @@
</div>
<div class="form-group">
<label for="comment">Описание задачи:</label>
<textarea class="form-control" rows="3" id="comment" th:field="*{description}"></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"/>
<input class="form-control" placeholder="Теги задачи" data-role="tagsinput"
id="tags" name="tags"/>
</div>
<div class="form-group">
<label>Дедлайны задачи:</label>
@ -77,7 +80,8 @@
class="alert alert-danger">Incorrect title</p>
</div>
<div class="form-group">
<input type="submit" id="addDeadline" name="addDeadline" class="btn btn-primary" value="Добавить
<input type="submit" id="addDeadline" name="addDeadline" class="btn btn-primary"
value="Добавить
дедлайн"/>
</div>
<div class="clearfix"></div>
@ -86,9 +90,9 @@
type="submit">
Сохранить
</button>
<button id="cancelButton" class="btn btn-default text-uppercase" href="/students/tasks">
<a id="cancelButton" class="btn btn-default text-uppercase" href="/students/tasks">
Отмена
</button>
</a>
</div>
</div>
<div class="col-md-4 offset-md-1 col-sm-12 offset-sm-0">
@ -124,6 +128,7 @@
</div>
</div>
</div>
<script src="/js/tasks.js"></script>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
</div>

View File

@ -20,7 +20,7 @@
<hr/>
<div class="row">
<div class="col-md-9 col-sm-12">
<th:block th:each="task : ${tasks.tasks}">
<th:block th:each="task : ${tasks}">
<div th:replace="students/fragments/taskLineFragment :: taskLine(task=${task})"/>
</th:block>
</div>
@ -41,7 +41,6 @@
<!--th:text="${year}">year-->
<!--</option>-->
</select>
</div>
</div>
</div>