From 056dcb5b4fda70f963bfb392fcd2816170f2bc12 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 3 May 2022 14:58:42 +0430 Subject: added PUT, the CRUD is done now. just need to test the crud --- spring-front/Dockerfile | 2 +- src/main/java/com/terminaldweller/doc/Doc.java | 22 ++++++++++++++++++++-- .../java/com/terminaldweller/doc/DocConfig.java | 4 ++-- .../com/terminaldweller/doc/DocController.java | 6 ++++++ .../java/com/terminaldweller/doc/DocService.java | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/spring-front/Dockerfile b/spring-front/Dockerfile index 5e236e6..e76de11 100644 --- a/spring-front/Dockerfile +++ b/spring-front/Dockerfile @@ -1,4 +1,4 @@ -FROM node:lts-alpine3.15 as certbuilder +FROM alpine:3.15 as certbuilder RUN apk add openssl WORKDIR /certs RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert diff --git a/src/main/java/com/terminaldweller/doc/Doc.java b/src/main/java/com/terminaldweller/doc/Doc.java index 1ec9dca..c3fa72b 100644 --- a/src/main/java/com/terminaldweller/doc/Doc.java +++ b/src/main/java/com/terminaldweller/doc/Doc.java @@ -18,6 +18,7 @@ public class Doc { private String name; private long lastModified; + private String body; public Doc() {} @@ -28,15 +29,24 @@ public class Doc { * @param name the name of the documment given by the user. * @param lastModified the date of the last modification in unix epoch. */ - public Doc(Long id, String name, long lastModified) { + public Doc(Long id, String name, long lastModified, String body) { this.id = id; this.name = name; this.lastModified = lastModified; + this.body = body; } - public Doc(String name, long lastModified) { + /** + * Constructor without the id. + * + * @param name document name + * @param lastModified date of last modification + * @param body document content + */ + public Doc(String name, long lastModified, String body) { this.name = name; this.lastModified = lastModified; + this.body = body; } public Long getId() { @@ -51,6 +61,10 @@ public class Doc { return this.lastModified; } + public String getBody() { + return this.body; + } + public void setId(Long id) { this.id = id; } @@ -62,4 +76,8 @@ public class Doc { public void setLastModified(long lastModified) { this.lastModified = lastModified; } + + public void setBody(String body) { + this.body = body; + } } diff --git a/src/main/java/com/terminaldweller/doc/DocConfig.java b/src/main/java/com/terminaldweller/doc/DocConfig.java index 1826817..b75b0d4 100644 --- a/src/main/java/com/terminaldweller/doc/DocConfig.java +++ b/src/main/java/com/terminaldweller/doc/DocConfig.java @@ -11,8 +11,8 @@ public class DocConfig { @Bean CommandLineRunner commandLineRunner(DocRepository repository) { return args -> { - Doc mydoc1 = new Doc("mydoc1", 0L); - Doc mydoc2 = new Doc("mydoc2", 0L); + Doc mydoc1 = new Doc("mydoc1", 0L, ""); + Doc mydoc2 = new Doc("mydoc2", 0L, ""); repository.saveAll(List.of(mydoc1, mydoc2)); }; diff --git a/src/main/java/com/terminaldweller/doc/DocController.java b/src/main/java/com/terminaldweller/doc/DocController.java index a91fb0d..e73ef41 100644 --- a/src/main/java/com/terminaldweller/doc/DocController.java +++ b/src/main/java/com/terminaldweller/doc/DocController.java @@ -7,6 +7,7 @@ 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.ResponseStatus; @@ -38,4 +39,9 @@ public class DocController { public void deleteDocs(@PathVariable("Id") Long id) { docService.deleteDoc(id); } + + @PutMapping(path = "{Id}") + public void updatDoc(@RequestBody Doc doc, @PathVariable("Id") long id) { + docService.updateDoc(doc, id); + } } diff --git a/src/main/java/com/terminaldweller/doc/DocService.java b/src/main/java/com/terminaldweller/doc/DocService.java index b682087..5a07c23 100644 --- a/src/main/java/com/terminaldweller/doc/DocService.java +++ b/src/main/java/com/terminaldweller/doc/DocService.java @@ -32,6 +32,22 @@ public class DocService { docRepository.save(doc); } + /** + * Update a Document. + * + * @param doc the document to update. + */ + public void updateDoc(Doc doc, Long id) { + Optional docOptional = docRepository.findById(id); + if (!docOptional.isPresent()) { + throw new IllegalStateException("Resource must be created before update"); + } + doc.setId(id); + doc.setLastModified(System.currentTimeMillis() / 1000L); + doc.setBody(doc.getBody()); + docRepository.save(doc); + } + /** * Deletes a document from the data store. * -- cgit v1.2.3