diff --git a/src/main/java/ru/ulstu/students/controller/TaskController.java b/src/main/java/ru/ulstu/students/controller/TaskController.java index 76b2f2b..52c9fc7 100644 --- a/src/main/java/ru/ulstu/students/controller/TaskController.java +++ b/src/main/java/ru/ulstu/students/controller/TaskController.java @@ -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 getAllTags() { - return taskService.getTaskTags(); - } +// @ModelAttribute("allTags") +// public List 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() diff --git a/src/main/java/ru/ulstu/students/model/Task.java b/src/main/java/ru/ulstu/students/model/Task.java index 296ddcb..1ffa95c 100644 --- a/src/main/java/ru/ulstu/students/model/Task.java +++ b/src/main/java/ru/ulstu/students/model/Task.java @@ -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 tags = new HashSet<>(); + @JoinTable(name = "task_tags", + joinColumns = {@JoinColumn(name = "task_id")}, + inverseJoinColumns = {@JoinColumn(name = "tag_id")}) + private Set tags = new HashSet<>(); public String getTitle() { return title; @@ -103,11 +107,11 @@ public class Task extends BaseEntity { this.updateDate = updateDate; } - public Set getTags() { + public Set getTags() { return tags; } - public void setTags(Set tags) { + public void setTags(Set tags) { this.tags = tags; } } diff --git a/src/main/java/ru/ulstu/students/model/TaskDto.java b/src/main/java/ru/ulstu/students/model/TaskDto.java index 62e73b4..896d9ea 100644 --- a/src/main/java/ru/ulstu/students/model/TaskDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskDto.java @@ -43,7 +43,7 @@ public class TaskDto { @JsonProperty("status") Task.TaskStatus status, @JsonProperty("deadlines") List deadlines, @JsonProperty("tagIds") Set tagIds, - @JsonProperty("tagIds") Set tags){ + @JsonProperty("tags") Set 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() { diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index c3ac558..22190ee 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -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 getTaskStatuses() { return Arrays.asList(Task.TaskStatus.values()); } - public List getTaskTags() { - return tagService.findAll(); - } + +// public List getTaskTags() { +// return tagService.; +// } } diff --git a/src/main/java/ru/ulstu/tags/model/Tag.java b/src/main/java/ru/ulstu/tags/model/Tag.java index 78a6274..d299b22 100644 --- a/src/main/java/ru/ulstu/tags/model/Tag.java +++ b/src/main/java/ru/ulstu/tags/model/Tag.java @@ -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() { diff --git a/src/main/java/ru/ulstu/tags/model/TagDto.java b/src/main/java/ru/ulstu/tags/model/TagDto.java index 80ce085..582ea5d 100644 --- a/src/main/java/ru/ulstu/tags/model/TagDto.java +++ b/src/main/java/ru/ulstu/tags/model/TagDto.java @@ -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(); - } } diff --git a/src/main/java/ru/ulstu/tags/repository/TagRepository.java b/src/main/java/ru/ulstu/tags/repository/TagRepository.java index c0a014e..038a740 100644 --- a/src/main/java/ru/ulstu/tags/repository/TagRepository.java +++ b/src/main/java/ru/ulstu/tags/repository/TagRepository.java @@ -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 { +public interface TagRepository extends JpaRepository{ } diff --git a/src/main/java/ru/ulstu/tags/service/TagService.java b/src/main/java/ru/ulstu/tags/service/TagService.java index 45c10f2..4d8232f 100644 --- a/src/main/java/ru/ulstu/tags/service/TagService.java +++ b/src/main/java/ru/ulstu/tags/service/TagService.java @@ -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 findAll() { - return tagRepository.findAll(); + public Set saveOrCreate(Set tagDtos) { + return tagDtos + .stream() + .map(tagDto -> { + return tagRepository.exists(tagDto.getId()) ? takeExist(tagDto) : create(tagDto); + }).collect(Collectors.toSet()); } -// public List findAllDto() { -// List 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 findByIds(List 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; } } diff --git a/src/main/resources/db/changelog-20190410_000000-schema.xml b/src/main/resources/db/changelog-20190410_000000-schema.xml new file mode 100644 index 0000000..b8db7f9 --- /dev/null +++ b/src/main/resources/db/changelog-20190410_000000-schema.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 800b2f1..67d3c40 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -23,5 +23,6 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/students/fragments/taskLineFragment.html b/src/main/resources/templates/students/fragments/taskLineFragment.html index 44c2d03..122de6d 100644 --- a/src/main/resources/templates/students/fragments/taskLineFragment.html +++ b/src/main/resources/templates/students/fragments/taskLineFragment.html @@ -4,12 +4,12 @@ -
+
- +
- Добавить задачу diff --git a/src/main/resources/templates/students/task.html b/src/main/resources/templates/students/task.html index 98c4147..ceda3bc 100644 --- a/src/main/resources/templates/students/task.html +++ b/src/main/resources/templates/students/task.html @@ -11,6 +11,7 @@
+
@@ -21,7 +22,7 @@
-
@@ -45,17 +46,19 @@
- +
- +
-
+
- +
@@ -77,18 +80,19 @@ class="alert alert-danger">Incorrect title

-
-
+
- +
@@ -99,7 +103,7 @@
+ th:text="${taskDto.createDate == null ? '' : #dates.format(taskDto.createDate, 'dd.MM.yyyy HH:mm')}"> text
@@ -112,7 +116,7 @@
+ th:text="${taskDto.updateDate == null ? '' : #dates.format(taskDto.updateDate, 'dd.MM.yyyy HH:mm')}"> text
@@ -124,6 +128,7 @@
+
diff --git a/src/main/resources/templates/students/tasks.html b/src/main/resources/templates/students/tasks.html index 37ade14..b1177d8 100644 --- a/src/main/resources/templates/students/tasks.html +++ b/src/main/resources/templates/students/tasks.html @@ -20,7 +20,7 @@
- +
@@ -41,7 +41,6 @@ -