#74 fixing codestyle and tags

merge-requests/47/head
ASH 5 years ago
parent 5eae7305c4
commit d6a11a5902

@ -7,10 +7,8 @@ import org.springframework.web.bind.annotation.*;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.students.model.Task; import ru.ulstu.students.model.Task;
import ru.ulstu.students.model.TaskDto; 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 ru.ulstu.students.service.TaskService;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
@ -56,8 +54,9 @@ public class TaskController {
if (taskDto.getDeadlines().isEmpty()) { if (taskDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
} }
if (errors.hasErrors()) if (errors.hasErrors()) {
return TASK_PAGE; return TASK_PAGE;
}
taskService.save(taskDto); taskService.save(taskDto);
return String.format(REDIRECT_TO, TASKS_PAGE); return String.format(REDIRECT_TO, TASKS_PAGE);
} }
@ -65,8 +64,9 @@ public class TaskController {
@PostMapping(value = "/task", params = "addDeadline") @PostMapping(value = "/task", params = "addDeadline")
public String addDeadline(@Valid TaskDto taskDto, Errors errors) { public String addDeadline(@Valid TaskDto taskDto, Errors errors) {
filterEmptyDeadlines(taskDto); filterEmptyDeadlines(taskDto);
if (errors.hasErrors()) if (errors.hasErrors()) {
return TASK_PAGE; return TASK_PAGE;
}
taskDto.getDeadlines().add(new Deadline()); taskDto.getDeadlines().add(new Deadline());
return TASK_PAGE; return TASK_PAGE;
} }
@ -76,20 +76,6 @@ public class TaskController {
return taskService.getTaskStatuses(); 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) { private void filterEmptyDeadlines(TaskDto taskDto) {
taskDto.setDeadlines(taskDto.getDeadlines().stream() taskDto.setDeadlines(taskDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

@ -6,10 +6,11 @@ import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.tags.model.Tag; import ru.ulstu.tags.model.Tag;
import ru.ulstu.tags.model.TagDto;
import javax.persistence.*; import javax.persistence.*;
import java.util.*; import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity @Entity
public class Task extends BaseEntity { public class Task extends BaseEntity {
@ -57,7 +58,7 @@ public class Task extends BaseEntity {
@JoinTable(name = "task_tags", @JoinTable(name = "task_tags",
joinColumns = {@JoinColumn(name = "task_id")}, joinColumns = {@JoinColumn(name = "task_id")},
inverseJoinColumns = {@JoinColumn(name = "tag_id")}) inverseJoinColumns = {@JoinColumn(name = "tag_id")})
private Set<TagDto> tags = new HashSet<>(); private List<Tag> tags = new ArrayList<>();
public String getTitle() { public String getTitle() {
return title; return title;
@ -107,11 +108,11 @@ public class Task extends BaseEntity {
this.updateDate = updateDate; this.updateDate = updateDate;
} }
public Set<TagDto> getTags() { public List<Tag> getTags() {
return tags; return tags;
} }
public void setTags(Set<TagDto> tags) { public void setTags(List<Tag> tags) {
this.tags = tags; this.tags = tags;
} }
} }

@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.Deadline; 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.ArrayList;
import java.util.Date; import java.util.Date;
@ -13,8 +13,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class TaskDto { public class TaskDto {
private final static int MAX_TAGS_LENGTH = 50; private final static int MAX_TAGS_LENGTH = 50;
@ -28,7 +26,7 @@ public class TaskDto {
private Date createDate; private Date createDate;
private Date updateDate; private Date updateDate;
private Set<Integer> tagIds; private Set<Integer> tagIds;
private Set<TagDto> tags; private List<Tag> tags;
public TaskDto() { public TaskDto() {
deadlines.add(new Deadline()); deadlines.add(new Deadline());
@ -43,7 +41,7 @@ public class TaskDto {
@JsonProperty("status") Task.TaskStatus status, @JsonProperty("status") Task.TaskStatus status,
@JsonProperty("deadlines") List<Deadline> deadlines, @JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("tagIds") Set<Integer> tagIds, @JsonProperty("tagIds") Set<Integer> tagIds,
@JsonProperty("tags") Set<TagDto> tags) { @JsonProperty("tags") List<Tag> tags) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -62,7 +60,6 @@ public class TaskDto {
this.createDate = task.getCreateDate(); this.createDate = task.getCreateDate();
this.updateDate = task.getUpdateDate(); this.updateDate = task.getUpdateDate();
this.description = task.getDescription(); this.description = task.getDescription();
// this.tagIds = convert(task.getTags(), tag -> tag.getId());
this.tags = task.getTags(); this.tags = task.getTags();
} }
@ -130,11 +127,11 @@ public class TaskDto {
this.tagIds = tagIds; this.tagIds = tagIds;
} }
public Set<TagDto> getTags() { public List<Tag> getTags() {
return tags; return tags;
} }
public void setTags(Set<TagDto> tags) { public void setTags(List<Tag> tags) {
this.tags = tags; this.tags = tags;
} }

@ -7,12 +7,12 @@ import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.students.model.Task; import ru.ulstu.students.model.Task;
import ru.ulstu.students.model.TaskDto; import ru.ulstu.students.model.TaskDto;
import ru.ulstu.students.repository.TaskRepository; 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 ru.ulstu.tags.service.TagService;
import java.io.IOException; 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 org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -64,9 +64,6 @@ public class TaskService {
task.setUpdateDate(new Date()); task.setUpdateDate(new Date());
task.getTags().clear(); task.getTags().clear();
task.setTags(tagService.saveOrCreate(taskDto.getTags())); 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; return task;
} }
@ -95,7 +92,4 @@ public class TaskService {
return Arrays.asList(Task.TaskStatus.values()); return Arrays.asList(Task.TaskStatus.values());
} }
// public List<TagDto> getTaskTags() {
// return tagService.;
// }
} }

@ -1,20 +1,39 @@
package ru.ulstu.tags.model; 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 ru.ulstu.core.model.BaseEntity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.Size;
@Entity @Entity
@Table(name = "tag") @Table(name = "tag")
public class Tag extends BaseEntity { public class Tag extends BaseEntity {
@NotBlank @NotEmpty
@Size(max = 50)
@Column(name = "tag_name") @Column(name = "tag_name")
private String tagName; 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() { public String getTagName() {
return tagName; return tagName;
} }

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

@ -1,8 +1,12 @@
package ru.ulstu.tags.repository; package ru.ulstu.tags.repository;
import org.springframework.data.jpa.repository.JpaRepository; 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);
} }

@ -1,17 +1,13 @@
package ru.ulstu.tags.service; package ru.ulstu.tags.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.tags.model.Tag;
import ru.ulstu.tags.model.TagDto;
import ru.ulstu.tags.repository.TagRepository; import ru.ulstu.tags.repository.TagRepository;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.Set; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service @Service
public class TagService { public class TagService {
@ -23,23 +19,29 @@ public class TagService {
this.tagRepository = tagRepository; this.tagRepository = tagRepository;
} }
public Set<TagDto> saveOrCreate(Set<TagDto> tagDtos) { public List<Tag> saveOrCreate(List<Tag> tags) {
return tagDtos return tags
.stream() .stream()
.map(tagDto -> { .map(tag -> {
return tagRepository.exists(tagDto.getId()) ? takeExist(tagDto) : create(tagDto); return tag.getId() != null ? getExistById(tag) :
}).collect(Collectors.toSet()); isExistByName(tag.getTagName()) != null ? isExistByName(tag.getTagName()) : create(tag);
}).collect(Collectors.toList());
}
@Transactional
public Tag getExistById(Tag tag) {
return tagRepository.findOne(tag.getId());
} }
@Transactional @Transactional
public TagDto takeExist(TagDto tagDto) { public Tag isExistByName(String tagName) {
return tagRepository.findOne(tagDto.getId()); return tagRepository.findByName(tagName);
} }
@Transactional @Transactional
public TagDto create(TagDto tagDto) { public Tag create(Tag tag) {
TagDto newTag = new TagDto(); Tag newTag = new Tag();
newTag.setTagName(tagDto.getTagName()); newTag.setTagName(tag.getTagName());
newTag = tagRepository.save(newTag); newTag = tagRepository.save(newTag);
return newTag; return newTag;
} }

Loading…
Cancel
Save