From cd7da22addad4549dc631f2dcb2009f2caf94d1e Mon Sep 17 00:00:00 2001 From: Serg N Date: Mon, 17 Jun 2019 16:43:47 +0900 Subject: [PATCH 1/2] create update@delete for Document with integrational test --- .../pm/controller/DocumentController.java | 12 ++ .../khasang/pm/service/DocumentService.java | 16 ++ .../pm/service/impl/DocumentServiceImpl.java | 10 ++ src/main/webapp/WEB-INF/views/doc.jsp | 146 ++++++++++++++---- .../DocumentControllerIntegrationTest.java | 36 +++++ 5 files changed, 192 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/khasang/pm/controller/DocumentController.java b/src/main/java/io/khasang/pm/controller/DocumentController.java index 4d5f13a..cb9a8d7 100644 --- a/src/main/java/io/khasang/pm/controller/DocumentController.java +++ b/src/main/java/io/khasang/pm/controller/DocumentController.java @@ -33,6 +33,18 @@ public List getAll() { return documentService.getAll(); } + @RequestMapping(value = "/update", method = RequestMethod.PUT, produces = "application/json;charset=utf-8") + @ResponseBody + public DocumentDto update(@RequestBody Document document){ + return documentService.update(document); + } + + @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8") + @ResponseBody + public DocumentDto delete(@PathVariable("id") long id){ + return documentService.delete(id); + } + @Autowired public void setDocumentService(DocumentService documentService) { this.documentService = documentService; diff --git a/src/main/java/io/khasang/pm/service/DocumentService.java b/src/main/java/io/khasang/pm/service/DocumentService.java index 558329a..acd27f0 100644 --- a/src/main/java/io/khasang/pm/service/DocumentService.java +++ b/src/main/java/io/khasang/pm/service/DocumentService.java @@ -28,4 +28,20 @@ public interface DocumentService { * @return all documentDto from DB */ List getAll(); + + /** + * updating document in db + * + * @param document - document for updating + * @return updated document + */ + DocumentDto update(Document document); + + /** + * deleting document by id from db + * + * @param id - id document's for deletint + * @return deleting document + */ + DocumentDto delete(long id); } diff --git a/src/main/java/io/khasang/pm/service/impl/DocumentServiceImpl.java b/src/main/java/io/khasang/pm/service/impl/DocumentServiceImpl.java index e77cac2..84f95f0 100644 --- a/src/main/java/io/khasang/pm/service/impl/DocumentServiceImpl.java +++ b/src/main/java/io/khasang/pm/service/impl/DocumentServiceImpl.java @@ -38,6 +38,16 @@ public List getAll() { return documentDtos; } + @Override + public DocumentDto update(Document document) { + return documentDto.getDocumentDto(documentDao.update(document)); + } + + @Override + public DocumentDto delete(long id) { + return documentDto.getDocumentDto(documentDao.delete(documentDao.getById(id))); + } + @Autowired public void setDocumentDao(DocumentDao documentDao) { this.documentDao = documentDao; diff --git a/src/main/webapp/WEB-INF/views/doc.jsp b/src/main/webapp/WEB-INF/views/doc.jsp index d6baa86..f0bf545 100644 --- a/src/main/webapp/WEB-INF/views/doc.jsp +++ b/src/main/webapp/WEB-INF/views/doc.jsp @@ -48,53 +48,59 @@ }); }; - var checkData= function (docNumber, dateDoc, author, type, content){ - if (docNumber == ""){ - alert("не указано значение docNumber") - return false; - } + var RestPost = function (docNumber, dateDoc, author, type, content) { - if (dateDoc == ""){ - alert("не указано значение dateDoc") - return false; + if (!checkData(docNumber, dateDoc, author, type, content)) { + return; } - var reg_exp = /^\d{4}-\d{2}-\d{2}$/; - if(!reg_exp.test(dateDoc)) - { - alert("Дату следует ввести в формате yyyy-mm-dd"); - return false; - } + var JSONObject = { + 'docNumber': docNumber, + 'docDate': dateDoc, + 'author':author, + 'type':type, + 'content':content + }; - if (author == ""){ - alert("не указано значение author") - return false; - } + $.ajax({ + type: 'POST', + url: service + '/add', + dataType: 'json', + data: JSON.stringify(JSONObject), + accept: 'json', + contentType: 'application/json;utf-8', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); + }; - if (type == ""){ - alert("не указано значение type") - return false; - } - return true; - } + var RestUpdate = function (id, docNumber, dateDoc, author, type, content){ - var RestPost = function (docNumber, dateDoc, author, type, content) { + if (id == ""){ + alert("не указано значение id") + return; + } if (!checkData(docNumber, dateDoc, author, type, content)) { return; } var JSONObject = { + 'id':id, 'docNumber': docNumber, 'docDate': dateDoc, 'author':author, 'type':type, 'content':content }; - $.ajax({ - type: 'POST', - url: service + '/add', + type: 'PUT', + url: service + '/update', dataType: 'json', data: JSON.stringify(JSONObject), accept: 'json', @@ -108,6 +114,59 @@ } }); }; + + var RestDel = function (id) { + + if (id == ""){ + alert("не указано значение id") + return; + } + + $.ajax({ + type: 'DELETE', + url: service + '/del/' + id, + dataType: 'json', + accept: 'json', + contentType: 'application/json;utf-8', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); + }; + + var checkData= function (docNumber, dateDoc, author, type, content){ + if (docNumber == ""){ + alert("не указано значение docNumber") + return false; + } + + if (dateDoc == ""){ + alert("не указано значение dateDoc") + return false; + } + + var reg_exp = /^\d{4}-\d{2}-\d{2}$/; + if(!reg_exp.test(dateDoc)) + { + alert("Дату следует ввести в формате yyyy-mm-dd"); + return false; + } + + if (author == ""){ + alert("не указано значение author") + return false; + } + + if (type == ""){ + alert("не указано значение type") + return false; + } + return true; + }; @@ -148,6 +207,37 @@ + + update document - PUT + /document/update{document} + + + + + + + + + + +
idDocument:
docNumber:
docDate:
author:
type:
content:
+ + + + Delete document by id - DELETE + /doc/delete/{id} + + id: + + +
diff --git a/src/test/java/io/khasang/pm/controller/DocumentControllerIntegrationTest.java b/src/test/java/io/khasang/pm/controller/DocumentControllerIntegrationTest.java index 16c8af0..7486f66 100644 --- a/src/test/java/io/khasang/pm/controller/DocumentControllerIntegrationTest.java +++ b/src/test/java/io/khasang/pm/controller/DocumentControllerIntegrationTest.java @@ -24,6 +24,8 @@ public class DocumentControllerIntegrationTest { private static final String ROOT = "http://localhost:8080/doc"; private static final String ADD = "/add"; + private static final String DELETE = "/del"; + private static final String UPDATE = "/update"; private static final String GET = "/get"; private static final String ALL = "/all"; private RestTemplate template; @@ -67,6 +69,40 @@ public void checkGettingAllDocuments() { assertFalse(receivedDocuments.isEmpty()); } + @Test + public void checkUpdatingDocument() { + Document doc = createDoc(); + doc.setContent("new content"); + + ResponseEntity responseEntity = template.exchange( + ROOT + UPDATE, + HttpMethod.PUT, + createHttpEntity(doc), + DocumentDto.class); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + DocumentDto reseivedDoc = responseEntity.getBody(); + assertNotNull(reseivedDoc); + assertEquals(doc.getContent(), reseivedDoc.getContent()); + } + + @Test + public void checkDeletingDocument() { + Document doc = createDoc(); + + ResponseEntity responseEntity = template.exchange( + ROOT + DELETE + "/{id}", + HttpMethod.DELETE, + createHttpEntity(doc), + DocumentDto.class, + doc.getId()); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + DocumentDto deletedDoc = responseEntity.getBody(); + assertNotNull(deletedDoc); + assertEquals(doc.getId(), deletedDoc.getId()); + } + private Document createDoc() { Document doc = prefillDocument(); From 871010829cb780c8569b78c444a07937ca51f76c Mon Sep 17 00:00:00 2001 From: SVN Date: Mon, 17 Jun 2019 21:46:13 +1000 Subject: [PATCH 2/2] refractoring of DocumentController & Document --- .../java/io/khasang/pm/controller/DocumentController.java | 6 +++++- src/main/java/io/khasang/pm/entity/Document.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/khasang/pm/controller/DocumentController.java b/src/main/java/io/khasang/pm/controller/DocumentController.java index cb9a8d7..d4a0d73 100644 --- a/src/main/java/io/khasang/pm/controller/DocumentController.java +++ b/src/main/java/io/khasang/pm/controller/DocumentController.java @@ -5,7 +5,11 @@ import io.khasang.pm.service.DocumentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; diff --git a/src/main/java/io/khasang/pm/entity/Document.java b/src/main/java/io/khasang/pm/entity/Document.java index 957e850..e1fbb30 100644 --- a/src/main/java/io/khasang/pm/entity/Document.java +++ b/src/main/java/io/khasang/pm/entity/Document.java @@ -1,6 +1,10 @@ package io.khasang.pm.entity; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; import java.time.LocalDate; @Entity