From 6496bfe08c2c5bae024ae942ed743a5266b30e6d Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 3 Jun 2022 19:18:38 +0430 Subject: wip, the crud is working, now have to figure out how to update and save the actual text --- spring-front/nginx.conf | 2 +- spring-front/src/App.js | 62 ++++++++++++++-------- spring-front/src/components/Editor.js | 10 ++-- src/main/java/com/terminaldweller/doc/Doc.java | 7 +-- .../java/com/terminaldweller/doc/DocConfig.java | 4 +- .../com/terminaldweller/doc/DocController.java | 8 +-- .../com/terminaldweller/doc/DocRepository.java | 3 ++ .../java/com/terminaldweller/doc/DocService.java | 14 +++-- src/main/resources/application.properties | 1 + 9 files changed, 68 insertions(+), 43 deletions(-) diff --git a/spring-front/nginx.conf b/spring-front/nginx.conf index 10e2785..cf2a208 100644 --- a/spring-front/nginx.conf +++ b/spring-front/nginx.conf @@ -21,7 +21,7 @@ http { tcp_nopush on; add_header X-Content-Type-Options "nosniff" always; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; - add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' unpkg.com cdnjs.cloudflare.com; style-src 'self' 'unsafe-inline' unpkg.com cdnjs.cloudflare.com"; + add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' unpkg.com cdnjs.cloudflare.com; style-src 'self' 'unsafe-inline' unpkg.com cdnjs.cloudflare.com; connect-src *;"; add_header X-Frame-Options SAMEORIGIN always; add_header X-XSS-Protection "1; mode=block" always; add_header Permissions-Policy "geolocation=(self),midi=(self),sync-xhr=(self),microphone=(self),camera=(self),magnetometer=(self),gyroscope=(self),fullscreen=(self),payment=(self),usb=(self)"; diff --git a/spring-front/src/App.js b/spring-front/src/App.js index b2b9866..50f1bf2 100644 --- a/spring-front/src/App.js +++ b/spring-front/src/App.js @@ -13,12 +13,16 @@ export default class App extends React.Component { this.docId = this.genNewRandId(); localStorage.setItem("docId", this.docId); } + console.log(this.docId); + this.markdownText = "tutti"; + this.loaded = false; } genNewRandId() { return Math.floor(Math.random() * (0x1 << 16)); } + // GET handleLoad() { fetch(`https://localhost:9080/api/v1/doc/${this.docId}`) .then((response) => { @@ -26,39 +30,58 @@ export default class App extends React.Component { throw new Error(`request failed with status ${response.status}`); } let res = response.json(); - this.docId = data.Id; + // this.markdownText = response.body; }) .then((data) => { console.log(data); - this.docId = data.Id; + // this.markdownText = data.body; }); } + + // DELETE handleDelete() { - fetch(`https://localhost:9080/api/v1/doc/${this.docId}`); + fetch(`https://localhost:9080/api/v1/doc/${this.docId}`, { + method: "DELETE", + headers: { + Accept: "application/json", + }, + }).then((response) => { + if (!response.ok) { + throw new Error(`request failed with status ${response.status}`); + } + }); } - handleSave() { + + // POST + async handleSave() { let obj = { - Id = this.docId, - Doc = props.markdownText - } - let response = await fetch(`https://localhost:9080/api/v1/doc/${this.docId}`,{ - method: "POST", - body: JSON.stringify(obj), - header: { - "Content-Type": "application/json" + id: this.docId, + name: `${this.docId}`, + lastModified: Math.floor(Date.now() / 1000), + body: this.markdownText, + }; + let response = await fetch( + `https://localhost:9080/api/v1/doc/${this.docId}`, + { + method: "POST", + body: JSON.stringify(obj), + mode: "cors", + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, } - }); + ); - if (!response.ok){ - throw new Error(`request failed with status code ${response.status}`) + if (!response.ok) { + throw new Error(`request failed with status code ${response.status}`); } } render() { return (
- - {/* */} + - {/* */} - {/* */} - {/* */} - {/* */} - {/* */}
); } diff --git a/spring-front/src/components/Editor.js b/spring-front/src/components/Editor.js index f7c4ec5..d82a3fb 100644 --- a/spring-front/src/components/Editor.js +++ b/spring-front/src/components/Editor.js @@ -117,9 +117,9 @@ export default class Editor extends React.Component { this.setState((prevState) => ({ drawerActive: !prevState.drawerActive })); } - render() { - const { markdownText, drawerTitle, drawerChildren } = this.props; - const drawerStyles = this.state.drawerActive ? "is-expanded" : ""; + render(props) { + // const { markdownText, drawerTitle, drawerChildren } = this.props; + // const drawerStyles = this.state.drawerActive ? "is-expanded" : ""; return (
@@ -142,9 +142,7 @@ export default class Editor extends React.Component { onKeyDown={this.handleKeyDown.bind(this)} direction="rtl" tabIndex="0" - > - {markdownText} - + >
diff --git a/src/main/java/com/terminaldweller/doc/Doc.java b/src/main/java/com/terminaldweller/doc/Doc.java index c3fa72b..80247eb 100644 --- a/src/main/java/com/terminaldweller/doc/Doc.java +++ b/src/main/java/com/terminaldweller/doc/Doc.java @@ -1,10 +1,7 @@ package com.terminaldweller.doc; import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.SequenceGenerator; import javax.persistence.Table; /** The document class. */ @@ -12,8 +9,8 @@ import javax.persistence.Table; @Table public class Doc { @Id - @SequenceGenerator(name = "doc_sequence", sequenceName = "doc_sequence", allocationSize = 1) - @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "doc_sequence") + // @SequenceGenerator(name = "doc_sequence", sequenceName = "doc_sequence", allocationSize = 1) + // @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "doc_sequence") private Long id; private String name; diff --git a/src/main/java/com/terminaldweller/doc/DocConfig.java b/src/main/java/com/terminaldweller/doc/DocConfig.java index b75b0d4..8b45535 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(11L, "mydoc1", 0L, "a one"); + Doc mydoc2 = new Doc(111L, "mydoc2", 0L, "and a two"); 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 39ddfe9..80e63a8 100644 --- a/src/main/java/com/terminaldweller/doc/DocController.java +++ b/src/main/java/com/terminaldweller/doc/DocController.java @@ -3,6 +3,7 @@ package com.terminaldweller.doc; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; /** The document controller class. */ @RestController +@CrossOrigin(origins = "https://localhost:7080") @RequestMapping(path = "api/v1/doc") public class DocController { private final DocService docService; @@ -29,10 +31,10 @@ public class DocController { return docService.getDocs(id); } - @PostMapping + @PostMapping(path = "{Id}") @ResponseStatus(HttpStatus.CREATED) - public void postDocs(@RequestBody Doc doc) { - docService.addNewDoc(doc); + public void postDocs(@PathVariable("Id") Long id, @RequestBody Doc doc) { + docService.addNewDoc(id, doc); } @DeleteMapping(path = "{Id}") diff --git a/src/main/java/com/terminaldweller/doc/DocRepository.java b/src/main/java/com/terminaldweller/doc/DocRepository.java index 387835e..56db04e 100644 --- a/src/main/java/com/terminaldweller/doc/DocRepository.java +++ b/src/main/java/com/terminaldweller/doc/DocRepository.java @@ -10,4 +10,7 @@ import org.springframework.stereotype.Repository; public interface DocRepository extends JpaRepository { @Query("SELECT d FROM Doc d WHERE d.name = ?1") Optional findDocByName(String name); + + @Query("SELECT d FROM Doc d WHERE d.id = ?1") + Optional findDocById(Long id); } diff --git a/src/main/java/com/terminaldweller/doc/DocService.java b/src/main/java/com/terminaldweller/doc/DocService.java index c8bab23..df4456e 100644 --- a/src/main/java/com/terminaldweller/doc/DocService.java +++ b/src/main/java/com/terminaldweller/doc/DocService.java @@ -15,7 +15,11 @@ public class DocService { } public Optional getDocs(Long id) { - return docRepository.findById(id); + Optional docOptional = docRepository.findById(id); + if (docOptional.isPresent()) { + return docOptional; + } + throw new IllegalStateException("Id does not exist"); } /** @@ -23,11 +27,13 @@ public class DocService { * * @param doc the new Document to add. */ - public void addNewDoc(Doc doc) { - Optional docOptional = docRepository.findDocByName(doc.getName()); + public void addNewDoc(Long id, Doc doc) { + // Optional docOptional = docRepository.findDocByName(doc.getName()); + Optional docOptional = docRepository.findById(id); if (docOptional.isPresent()) { throw new IllegalStateException("Id is already taken"); } + doc.setId(id); docRepository.save(doc); } @@ -55,7 +61,7 @@ public class DocService { public void deleteDoc(Long id) { boolean exists = docRepository.existsById(id); if (!exists) { - throw new IllegalStateException("doc " + id + " does not exitst"); + throw new IllegalStateException("doc " + Long.toString(id) + " does not exitst"); } docRepository.deleteById(id); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e40aba9..86a848a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,6 +8,7 @@ spring.jpa.properties.hibernate.format_sql=true server.error.include-message=always server.http2.enabled=true +server.profiles.active=development server.port=8443 server.ssl.enabled=true server.ssl.key-store=/certs/cert.p12 -- cgit v1.2.3