#74 fixing codestyle and tags

This commit is contained in:
ASH 2019-04-14 17:50:23 +04:00
parent 5eae7305c4
commit d6a11a5902
8 changed files with 64 additions and 105 deletions

View File

@ -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<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()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

View File

@ -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<TagDto> tags = new HashSet<>();
private List<Tag> tags = new ArrayList<>();
public String getTitle() {
return title;
@ -107,11 +108,11 @@ public class Task extends BaseEntity {
this.updateDate = updateDate;
}
public Set<TagDto> getTags() {
public List<Tag> getTags() {
return tags;
}
public void setTags(Set<TagDto> tags) {
public void setTags(List<Tag> tags) {
this.tags = tags;
}
}

View File

@ -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<Integer> tagIds;
private Set<TagDto> tags;
private List<Tag> tags;
public TaskDto() {
deadlines.add(new Deadline());
@ -43,7 +41,7 @@ public class TaskDto {
@JsonProperty("status") Task.TaskStatus status,
@JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("tagIds") Set<Integer> tagIds,
@JsonProperty("tags") Set<TagDto> tags) {
@JsonProperty("tags") List<Tag> 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<TagDto> getTags() {
public List<Tag> getTags() {
return tags;
}
public void setTags(Set<TagDto> tags) {
public void setTags(List<Tag> tags) {
this.tags = tags;
}

View File

@ -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<TagDto> getTaskTags() {
// return tagService.;
// }
}

View File

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

View File

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

View File

@ -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<TagDto, Integer> {
public interface TagRepository extends JpaRepository<Tag, Integer> {
@Query("SELECT t FROM Tag t WHERE (t.tagName = :tagName)")
Tag findByName(@Param("tagName") String tagName);
}

View File

@ -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<TagDto> saveOrCreate(Set<TagDto> tagDtos) {
return tagDtos
public List<Tag> saveOrCreate(List<Tag> 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;
}