Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/main/java/io/khasang/pm/controller/DocumentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -33,6 +37,18 @@ public List<DocumentDto> 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;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/khasang/pm/entity/Document.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/khasang/pm/service/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public interface DocumentService {
* @return all documentDto from DB
*/
List<DocumentDto> 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);
}
10 changes: 10 additions & 0 deletions src/main/java/io/khasang/pm/service/impl/DocumentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public List<DocumentDto> 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;
Expand Down
146 changes: 118 additions & 28 deletions src/main/webapp/WEB-INF/views/doc.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
};
</script>

<body>
Expand Down Expand Up @@ -148,6 +207,37 @@
</table>
</td>
</tr>
<tr>
<td>update document - <code><strong>PUT</strong></code></td>
<td>/document/update{document}</td>
<td>
<table>
<tr> <td>idDocument:</td> <td> <input id="idChangedDoc" value="1"/> </td> </tr>
<tr> <td>docNumber:</td> <td> <input id="newDocNumber" value="new number"/> </td> </tr>
<tr> <td>docDate:</td> <td> <input id="newDocDate" value="2019-06-17"/> </td> </tr>
<tr> <td>author:</td> <td> <input id="newDocAuthor" value="other author"/> </td> </tr>
<tr> <td>type:</td> <td> <input id="newDocType" value="other type"/> </td> </tr>
<tr> <td>content:</td> <td> <input id="newDocContent" value="new content"/> </td> </tr>
<tr> <td></td> <td> <button type="button"
onclick="RestUpdate(
$('#idChangedDoc').val(),
$('#newDocNumber').val(),
$('#newDocDate').val(),
$('#newDocAuthor').val(),
$('#newDocType').val(),
$('#newDocContent').val())">try</button></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Delete document by id - <code><strong>DELETE</strong></code></td>
<td>/doc/delete/{id}</td>
<td>
id: <input id="idDelDoc" value="1"/>
<button type="button" onclick="RestDel($('#idDelDoc').val())">try</button>
</td>
</tr>
</table>

<div class="panel panel-default">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,40 @@ public void checkGettingAllDocuments() {
assertFalse(receivedDocuments.isEmpty());
}

@Test
public void checkUpdatingDocument() {
Document doc = createDoc();
doc.setContent("new content");

ResponseEntity<DocumentDto> 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<DocumentDto> 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();

Expand Down