diff options
Diffstat (limited to 'src/main/java')
4 files changed, 44 insertions, 4 deletions
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 @@ -33,6 +33,22 @@ public class DocService { } /** + * Update a Document. + * + * @param doc the document to update. + */ + public void updateDoc(Doc doc, Long id) { + Optional<Doc> 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. * * @param id The identifier for the document to be deleted. |