#74 fixing adding and db everything added except tags

merge-requests/47/head
ASH 5 years ago
parent 9b697c6f3f
commit f019806a8e

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

@ -6,6 +6,7 @@ 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.*;
@ -53,7 +54,10 @@ public class Task extends BaseEntity {
private Date updateDate = new Date(); private Date updateDate = new Date();
@ManyToMany(fetch = FetchType.EAGER) @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() { public String getTitle() {
return title; return title;
@ -103,11 +107,11 @@ public class Task extends BaseEntity {
this.updateDate = updateDate; this.updateDate = updateDate;
} }
public Set<Tag> getTags() { public Set<TagDto> getTags() {
return tags; return tags;
} }
public void setTags(Set<Tag> tags) { public void setTags(Set<TagDto> tags) {
this.tags = tags; this.tags = tags;
} }
} }

@ -43,7 +43,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("tagIds") Set<TagDto> tags){ @JsonProperty("tags") Set<TagDto> tags){
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -62,8 +62,8 @@ 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.tagIds = convert(task.getTags(), tag -> tag.getId());
this.tags = convert(task.getTags(), TagDto::new); this.tags = task.getTags();
} }
public Integer getId() { public Integer getId() {

@ -8,6 +8,7 @@ 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.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;
@ -62,9 +63,10 @@ public class TaskService {
task.setCreateDate(task.getCreateDate() == null ? new Date() : task.getCreateDate()); task.setCreateDate(task.getCreateDate() == null ? new Date() : task.getCreateDate());
task.setUpdateDate(new Date()); task.setUpdateDate(new Date());
task.getTags().clear(); task.getTags().clear();
if (taskDto.getTagIds() != null && !taskDto.getTagIds().isEmpty()) { task.setTags(tagService.saveOrCreate(taskDto.getTags()));
taskDto.getTagIds().forEach(tagIds -> task.getTags().add(tagService.findById(tagIds))); // if (taskDto.getTagIds() != null && !taskDto.getTagIds().isEmpty()) {
} // taskDto.getTagIds().forEach(tagIds -> task.getTags().add(tagService.findById(tagIds)));
// }
return task; return task;
} }
@ -92,7 +94,8 @@ public class TaskService {
public List<Task.TaskStatus> getTaskStatuses() { public List<Task.TaskStatus> getTaskStatuses() {
return Arrays.asList(Task.TaskStatus.values()); return Arrays.asList(Task.TaskStatus.values());
} }
public List<Tag> getTaskTags() {
return tagService.findAll(); // public List<TagDto> getTaskTags() {
} // return tagService.;
// }
} }

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

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

@ -1,8 +1,8 @@
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.Tag; import ru.ulstu.tags.model.TagDto;
public interface TagRepository extends JpaRepository<Tag, Integer> { public interface TagRepository extends JpaRepository<TagDto, Integer>{
} }

@ -2,11 +2,12 @@ package ru.ulstu.tags.service;
import org.apache.commons.lang3.StringUtils; 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.model.TagDto;
import ru.ulstu.tags.repository.TagRepository; import ru.ulstu.tags.repository.TagRepository;
import javax.transaction.Transactional;
import java.util.List; import java.util.Set;
import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -21,26 +22,25 @@ public class TagService {
this.tagRepository = tagRepository; this.tagRepository = tagRepository;
} }
public List<Tag> findAll() { public Set<TagDto> saveOrCreate(Set<TagDto> tagDtos) {
return tagRepository.findAll(); return tagDtos
.stream()
.map(tagDto -> {
return tagRepository.exists(tagDto.getId()) ? takeExist(tagDto) : create(tagDto);
}).collect(Collectors.toSet());
} }
// public List<TagDto> findAllDto() { @Transactional
// List<TagDto> tag = convert(findAll(), TagDto::new); public TagDto takeExist(TagDto tagDto) {
// tags.forEach(tagDto -> tagDto.setTitle(StringUtils.abbreviate(tagDto.getTitle(), MAX_DISPLAY_SIZE))); return tagRepository.findOne(tagDto.getId());
// return tags;
// }
//
// public TagDto findOneDto(Integer id) {
// return new TagDto(tagRepository.findOne(id));
// }
public List<Tag> findByIds(List<Integer> ids) {
return tagRepository.findAll(ids);
} }
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;
} }
} }

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

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

@ -4,12 +4,12 @@
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
</head> </head>
<body> <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"> <div class="col">
<span th:replace="students/fragments/taskStatusFragment :: taskStatus(taskStatus=${task.status})" /> <span th:replace="students/fragments/taskStatusFragment :: taskStatus(taskStatus=${task.status})" />
<a th:href="@{'task?id='+${task.id}}"> <a th:href="@{'task?id='+${task.id}}">
<span class="h6" th:text="${task.title}"></span> <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> </a>
<input class="id-class" type="hidden" th:value="${task.id}" /> <input class="id-class" type="hidden" th:value="${task.id}" />
<a class="remove-task pull-right d-none" th:href="@{'/students/delete/'+${task.id}}" <a class="remove-task pull-right d-none" th:href="@{'/students/delete/'+${task.id}}"

@ -15,7 +15,7 @@
</div> </div>
<div class="col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3"> <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> aria-hidden="true"></i>
Добавить задачу</a> Добавить задачу</a>

@ -11,6 +11,7 @@
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<section id="paper"> <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="container">
<div class="row"> <div class="row">
<div class="col-lg-12 text-center"> <div class="col-lg-12 text-center">
@ -21,7 +22,7 @@
<hr/> <hr/>
<div class="row"> <div class="row">
<div class="col-lg-12"> <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}"> th:object="${taskDto}">
<div class="row"> <div class="row">
<div class="col-md-7 col-sm-12"> <div class="col-md-7 col-sm-12">
@ -45,17 +46,19 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="comment">Описание задачи:</label> <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>
<div class="form-group"> <div class="form-group">
<label for="tags">Теги:</label> <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>
<div class="form-group" > <div class="form-group">
<label>Дедлайны задачи:</label> <label>Дедлайны задачи:</label>
<div class="row" th:each="deadline, rowStat : *{deadlines}"> <div class="row" th:each="deadline, rowStat : *{deadlines}">
<input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}" /> <input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}"/>
<div class="col-6"> <div class="col-6">
<input type="date" class="form-control" name="deadline" <input type="date" class="form-control" name="deadline"
th:field="*{deadlines[__${rowStat.index}__].date}"/> th:field="*{deadlines[__${rowStat.index}__].date}"/>
@ -77,18 +80,19 @@
class="alert alert-danger">Incorrect title</p> class="alert alert-danger">Incorrect title</p>
</div> </div>
<div class="form-group"> <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>
<div class="clearfix"></div> <div class="clearfix"></div>
<div class="form-group"> <div class="form-group">
<button id="sendMessageButton" name="save" class="btn btn-success text-uppercase" <button id="sendMessageButton" name="save" class="btn btn-success text-uppercase"
type="submit"> type="submit">
Сохранить Сохранить
</button> </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> </div>
<div class="col-md-4 offset-md-1 col-sm-12 offset-sm-0"> <div class="col-md-4 offset-md-1 col-sm-12 offset-sm-0">
@ -99,7 +103,7 @@
</div> </div>
<div class="col"> <div class="col">
<small class="text-muted" <small class="text-muted"
th:text="${taskDto.createDate == null ? '' : #dates.format(taskDto.createDate, 'dd.MM.yyyy HH:mm')}"> th:text="${taskDto.createDate == null ? '' : #dates.format(taskDto.createDate, 'dd.MM.yyyy HH:mm')}">
text text
</small> </small>
</div> </div>
@ -112,7 +116,7 @@
</div> </div>
<div class="col"> <div class="col">
<small class="text-muted" <small class="text-muted"
th:text="${taskDto.updateDate == null ? '' : #dates.format(taskDto.updateDate, 'dd.MM.yyyy HH:mm')}"> th:text="${taskDto.updateDate == null ? '' : #dates.format(taskDto.updateDate, 'dd.MM.yyyy HH:mm')}">
text text
</small> </small>
</div> </div>
@ -124,6 +128,7 @@
</div> </div>
</div> </div>
</div> </div>
<script src="/js/tasks.js"></script>
</section> </section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
</div> </div>

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

Loading…
Cancel
Save