fix create paper

This commit is contained in:
Anton Romanov 2018-12-05 16:51:26 +04:00
parent 67f440ac8a
commit 54bf5735e5
5 changed files with 75 additions and 17 deletions

View File

@ -0,0 +1,58 @@
package ru.ulstu.paper.controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.service.PaperService;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import static ru.ulstu.paper.controller.PaperRestController.URL;
@RestController
@RequestMapping(URL)
public class PaperRestController {
public static final String URL = Constants.API_1_0 + "papers";
private final PaperService paperService;
public PaperRestController(PaperService paperService) {
this.paperService = paperService;
}
@GetMapping
public Response<List<PaperDto>> getPapers() {
return new Response<>(paperService.findAllDto());
}
@GetMapping("/{paper-id}")
public Response<PaperDto> getPaper(@PathVariable("paper-id") Integer paperId){
return new Response(paperService.findById(paperId));
}
@PostMapping
public Response<Integer> createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response<>(paperService.create(paperDto));
}
@PutMapping
public Response<Integer> updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response<>(paperService.update(paperDto));
}
@DeleteMapping("/{paper-id}")
public Response<Boolean> delete(@PathVariable("paper-id") Integer paperId) throws IOException {
paperService.delete(paperId);
return new Response<>(true);
}
}

View File

@ -42,15 +42,6 @@ public class Paper extends BaseEntity implements UserContainer {
}
}
/*public enum PaperStatus {
ATTENTION,
ON_PREPARATION,
ON_REVIEW,
DRAFT,
COMPLETED,
FAILED;
}*/
@NotBlank
private String title;

View File

@ -158,4 +158,8 @@ public class PaperService {
update(paperDto);
}
}
public PaperDto findById(Integer paperId) {
return new PaperDto(paperRepository.findOne(paperId));
}
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:form="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
</head>
@ -26,23 +26,27 @@
<div class="row">
<div class="col-lg-12">
<form method="post" th:action="@{'/papers/paper?id='+*{id}}" th:object="${paperDto}">
<form method="post" th:action="@{'/papers/paper?id='+ *{id == null ? '' : id} + ''}"
th:object="${paperDto}">
<div class="row">
<div class="col-md-6 col-sm-12">
<input type="hidden" name="id" th:field="*{id}"/>
<div class="form-group">
<label for="title">Название:</label>
<input type="hidden" name="id" th:field="*{id}"/>
<input class="form-control" id="title" type="text"
placeholder="Название статьи"
th:field="*{title}"/>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="alert alert-danger">Incorrect title</p>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}"
class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<label for="status">Статус:</label>
<select class="form-control" th:field="*{status}" id="status">
<option th:each="status : ${allStatuses}" th:value="${status}" th:text="${status.statusName}">Status</option>
<option th:each="status : ${allStatuses}" th:value="${status}"
th:text="${status.statusName}">Status
</option>
</select>
</div>
@ -55,7 +59,8 @@
<div class="form-group">
<label>Дедлайн:</label>
<input type="date" class="form-control" name="deadline" th:field="*{deadlineDate}"/>
<p th:if="${#fields.hasErrors('deadlineDate')}" th:errors="*{deadlineDate}" class="alert alert-danger">Incorrect title</p>
<p th:if="${#fields.hasErrors('deadlineDate')}" th:errors="*{deadlineDate}"
class="alert alert-danger">Incorrect title</p>
</div>
<div class="form-check">
@ -161,9 +166,9 @@
type="submit">
Сохранить
</button>
<button id="cancelButton" class="btn btn-default text-uppercase" type="button">
<a id="cancelButton" class="btn btn-default text-uppercase" href="/papers/papers">
Отмена
</button>
</a>
</div>
</div>