diff --git a/src/main/java/ru/ulstu/students/controller/TaskController.java b/src/main/java/ru/ulstu/students/controller/TaskController.java index 4b4d12d..8fd9575 100644 --- a/src/main/java/ru/ulstu/students/controller/TaskController.java +++ b/src/main/java/ru/ulstu/students/controller/TaskController.java @@ -7,10 +7,8 @@ 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 ru.ulstu.tags.model.TagDto; -import springfox.documentation.annotations.ApiIgnore; import ru.ulstu.students.service.TaskService; +import springfox.documentation.annotations.ApiIgnore; import javax.validation.Valid; import java.io.IOException; @@ -56,8 +54,9 @@ public class TaskController { if (taskDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } - if (errors.hasErrors()) + if (errors.hasErrors()) { return TASK_PAGE; + } taskService.save(taskDto); return String.format(REDIRECT_TO, TASKS_PAGE); } @@ -65,8 +64,9 @@ public class TaskController { @PostMapping(value = "/task", params = "addDeadline") public String addDeadline(@Valid TaskDto taskDto, Errors errors) { filterEmptyDeadlines(taskDto); - if (errors.hasErrors()) + if (errors.hasErrors()) { return TASK_PAGE; + } taskDto.getDeadlines().add(new Deadline()); return TASK_PAGE; } @@ -76,20 +76,6 @@ public class TaskController { return taskService.getTaskStatuses(); } -// @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() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/students/model/Task.java b/src/main/java/ru/ulstu/students/model/Task.java index 1ffa95c..be1b1c6 100644 --- a/src/main/java/ru/ulstu/students/model/Task.java +++ b/src/main/java/ru/ulstu/students/model/Task.java @@ -6,10 +6,11 @@ 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.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; @Entity public class Task extends BaseEntity { @@ -57,7 +58,7 @@ public class Task extends BaseEntity { @JoinTable(name = "task_tags", joinColumns = {@JoinColumn(name = "task_id")}, inverseJoinColumns = {@JoinColumn(name = "tag_id")}) - private Set tags = new HashSet<>(); + private List tags = new ArrayList<>(); public String getTitle() { return title; @@ -107,11 +108,11 @@ public class Task extends BaseEntity { this.updateDate = updateDate; } - public Set getTags() { + public List getTags() { return tags; } - public void setTags(Set tags) { + public void setTags(List 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 dc41be8..941a6df 100644 --- a/src/main/java/ru/ulstu/students/model/TaskDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskDto.java @@ -5,7 +5,7 @@ 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 ru.ulstu.tags.model.Tag; import java.util.ArrayList; import java.util.Date; @@ -13,8 +13,6 @@ 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; @@ -28,7 +26,7 @@ public class TaskDto { private Date createDate; private Date updateDate; private Set tagIds; - private Set tags; + private List tags; public TaskDto() { deadlines.add(new Deadline()); @@ -43,7 +41,7 @@ public class TaskDto { @JsonProperty("status") Task.TaskStatus status, @JsonProperty("deadlines") List deadlines, @JsonProperty("tagIds") Set tagIds, - @JsonProperty("tags") Set tags) { + @JsonProperty("tags") List tags) { this.id = id; this.title = title; this.status = status; @@ -62,7 +60,6 @@ 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 = task.getTags(); } @@ -130,11 +127,11 @@ public class TaskDto { this.tagIds = tagIds; } - public Set getTags() { + public List getTags() { return tags; } - public void setTags(Set tags) { + public void setTags(List tags) { this.tags = tags; } diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index 5e969ac..7b67a44 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -7,12 +7,12 @@ 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.model.TagDto; import ru.ulstu.tags.service.TagService; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Date; +import java.util.List; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -64,9 +64,6 @@ public class TaskService { task.setUpdateDate(new Date()); task.getTags().clear(); 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; } @@ -95,7 +92,4 @@ public class TaskService { return Arrays.asList(Task.TaskStatus.values()); } -// 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 d299b22..aed000f 100644 --- a/src/main/java/ru/ulstu/tags/model/Tag.java +++ b/src/main/java/ru/ulstu/tags/model/Tag.java @@ -1,20 +1,39 @@ package ru.ulstu.tags.model; -import org.hibernate.validator.constraints.NotBlank; +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; @Entity @Table(name = "tag") public class Tag extends BaseEntity { - @NotBlank + @NotEmpty + @Size(max = 50) @Column(name = "tag_name") private String tagName; + public Tag() { + + } + + @JsonCreator + public Tag(@JsonProperty("id") Integer id, + @JsonProperty("tag_name") String tagName) { + this.setId(id); + this.tagName = tagName; + } + + public Tag(String name) { + this.tagName = name; + } + public String getTagName() { return tagName; } diff --git a/src/main/java/ru/ulstu/tags/model/TagDto.java b/src/main/java/ru/ulstu/tags/model/TagDto.java deleted file mode 100644 index 582ea5d..0000000 --- a/src/main/java/ru/ulstu/tags/model/TagDto.java +++ /dev/null @@ -1,44 +0,0 @@ -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; - -@Entity -@Table(name = "tag") -public class TagDto extends BaseEntity { - - @NotEmpty - @Size(max = 50) - @Column(name = "tag_name") - private String tagName; - - public TagDto() { - - } - - @JsonCreator - public TagDto(@JsonProperty("id") Integer id, - @JsonProperty("tag_name") String tagName) { - this.setId(id); - this.tagName = tagName; - } - - public TagDto(String name) { - this.tagName = name; - } - - public String getTagName() { - return tagName; - } - - public void setTagName(String tagName) { - this.tagName = tagName; - } -} diff --git a/src/main/java/ru/ulstu/tags/repository/TagRepository.java b/src/main/java/ru/ulstu/tags/repository/TagRepository.java index d4a9c76..213abae 100644 --- a/src/main/java/ru/ulstu/tags/repository/TagRepository.java +++ b/src/main/java/ru/ulstu/tags/repository/TagRepository.java @@ -1,8 +1,12 @@ package ru.ulstu.tags.repository; import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.tags.model.TagDto; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import ru.ulstu.tags.model.Tag; -public interface TagRepository extends JpaRepository { +public interface TagRepository extends JpaRepository { + @Query("SELECT t FROM Tag t WHERE (t.tagName = :tagName)") + Tag findByName(@Param("tagName") String tagName); } diff --git a/src/main/java/ru/ulstu/tags/service/TagService.java b/src/main/java/ru/ulstu/tags/service/TagService.java index adf2a82..681a506 100644 --- a/src/main/java/ru/ulstu/tags/service/TagService.java +++ b/src/main/java/ru/ulstu/tags/service/TagService.java @@ -1,17 +1,13 @@ package ru.ulstu.tags.service; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; - -import ru.ulstu.tags.model.TagDto; +import ru.ulstu.tags.model.Tag; import ru.ulstu.tags.repository.TagRepository; import javax.transaction.Transactional; -import java.util.Set; +import java.util.List; import java.util.stream.Collectors; -import static ru.ulstu.core.util.StreamApiUtils.convert; - @Service public class TagService { @@ -23,23 +19,29 @@ public class TagService { this.tagRepository = tagRepository; } - public Set saveOrCreate(Set tagDtos) { - return tagDtos + public List saveOrCreate(List tags) { + return tags .stream() - .map(tagDto -> { - return tagRepository.exists(tagDto.getId()) ? takeExist(tagDto) : create(tagDto); - }).collect(Collectors.toSet()); + .map(tag -> { + return tag.getId() != null ? getExistById(tag) : + isExistByName(tag.getTagName()) != null ? isExistByName(tag.getTagName()) : create(tag); + }).collect(Collectors.toList()); } @Transactional - public TagDto takeExist(TagDto tagDto) { - return tagRepository.findOne(tagDto.getId()); + public Tag getExistById(Tag tag) { + return tagRepository.findOne(tag.getId()); } @Transactional - public TagDto create(TagDto tagDto) { - TagDto newTag = new TagDto(); - newTag.setTagName(tagDto.getTagName()); + public Tag isExistByName(String tagName) { + return tagRepository.findByName(tagName); + } + + @Transactional + public Tag create(Tag tag) { + Tag newTag = new Tag(); + newTag.setTagName(tag.getTagName()); newTag = tagRepository.save(newTag); return newTag; }