From b7f6092ddc004d1c6852e7201a07286d40af1cb8 Mon Sep 17 00:00:00 2001 From: sui <1202ayasnppk@gmail.com> Date: Sat, 13 Jan 2024 21:30:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?update:=20Java=E3=81=A7=E7=92=B0=E5=A2=83?= =?UTF-8?q?=E6=A7=8B=E7=AF=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++- backend/java/backend-api/db/blog.db | Bin 20480 -> 20480 bytes .../example/backendapi/BlogApiController.java | 33 ++++++++ .../com/example/backendapi/BlogService.java | 41 ++++++++++ frontend/.prettierignore | 2 + frontend/package.json | 2 +- frontend/src/app/Footer.tsx | 4 +- frontend/src/app/articles/[id]/page.tsx | 33 +++----- frontend/src/app/components/ArticleCard.tsx | 20 +++-- .../src/app/components/ArticleContent.tsx | 26 +++++++ frontend/src/app/components/ArticleList.tsx | 5 +- frontend/src/app/components/DeleteButton.tsx | 7 +- frontend/src/app/components/Index.tsx | 38 +++++++++ frontend/src/app/java/articles/[id]/page.tsx | 33 ++------ frontend/src/app/java/articles/new/page.tsx | 5 +- frontend/src/app/java/articles/not-found.tsx | 14 ---- .../src/app/java/components/ArticleCard.tsx | 43 ----------- .../src/app/java/components/ArticleList.tsx | 19 ----- .../src/app/java/components/DeleteButton.tsx | 31 -------- frontend/src/app/java/page.tsx | 43 ++++------- .../src/app/javascript/articles/[id]/page.tsx | 29 ------- .../src/app/javascript/articles/new/page.tsx | 73 ------------------ .../src/app/javascript/articles/not-found.tsx | 14 ---- .../app/javascript/components/ArticleCard.tsx | 46 ----------- .../app/javascript/components/ArticleList.tsx | 19 ----- .../javascript/components/DeleteButton.tsx | 31 -------- frontend/src/app/javascript/page.tsx | 29 ------- frontend/src/app/page.tsx | 39 ++++------ .../src/app/python/articles/[id]/page.tsx | 29 ------- frontend/src/app/python/articles/new/page.tsx | 73 ------------------ .../src/app/python/articles/not-found.tsx | 14 ---- .../src/app/python/components/ArticleCard.tsx | 43 ----------- .../src/app/python/components/ArticleList.tsx | 19 ----- .../app/python/components/DeleteButton.tsx | 31 -------- frontend/src/app/python/page.tsx | 29 ------- frontend/src/app/types/index.ts | 20 +++++ frontend/src/data/db.json | 50 ++++++++---- frontend/src/lib/config.ts | 1 + frontend/src/utils/supabaseClient.ts | 10 --- 39 files changed, 274 insertions(+), 735 deletions(-) create mode 100644 frontend/.prettierignore create mode 100644 frontend/src/app/components/ArticleContent.tsx create mode 100644 frontend/src/app/components/Index.tsx delete mode 100644 frontend/src/app/java/articles/not-found.tsx delete mode 100644 frontend/src/app/java/components/ArticleCard.tsx delete mode 100644 frontend/src/app/java/components/ArticleList.tsx delete mode 100644 frontend/src/app/java/components/DeleteButton.tsx delete mode 100644 frontend/src/app/javascript/articles/[id]/page.tsx delete mode 100644 frontend/src/app/javascript/articles/new/page.tsx delete mode 100644 frontend/src/app/javascript/articles/not-found.tsx delete mode 100644 frontend/src/app/javascript/components/ArticleCard.tsx delete mode 100644 frontend/src/app/javascript/components/ArticleList.tsx delete mode 100644 frontend/src/app/javascript/components/DeleteButton.tsx delete mode 100644 frontend/src/app/javascript/page.tsx delete mode 100644 frontend/src/app/python/articles/[id]/page.tsx delete mode 100644 frontend/src/app/python/articles/new/page.tsx delete mode 100644 frontend/src/app/python/articles/not-found.tsx delete mode 100644 frontend/src/app/python/components/ArticleCard.tsx delete mode 100644 frontend/src/app/python/components/ArticleList.tsx delete mode 100644 frontend/src/app/python/components/DeleteButton.tsx delete mode 100644 frontend/src/app/python/page.tsx delete mode 100644 frontend/src/utils/supabaseClient.ts diff --git a/README.md b/README.md index 84abae4..b06a223 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# micro-blog \ No newline at end of file +# micro-blog + +バックエンドの言語差異を知るための練習用リポジトリ + +### frontend + +```bash +npm run dev +npm run json +``` diff --git a/backend/java/backend-api/db/blog.db b/backend/java/backend-api/db/blog.db index aa49cafb252c1251cd3f4f8449166a363d9f7862..703d27751613c9fb68cbffa397f50f2cca62e929 100644 GIT binary patch delta 610 zcma)&F>ll`7>1Lkxf4oz7AGMFxG11Xr9uXuoy2Fg>H;eh%D{ls&-d8~CrlYSUg^NZ z0J=YL{{U6@A1WbL>dM?7z{Xx%l^77iEw*2O&ui;_&sOo-D*pB;Cf^^wN=fwM?a2kM zUXF*Eh(?#YUw7W@JjkxIlT2KEo@9&X!?YnJvPvjhF-8pl(vp{gLa8;?wG|9z!p}Lt zr$O2reu#t=5@%LZuC<`0o=>T&OifYo3amEMl7WRDo$Y!A*V@i`Wj}U@4o*$subggH-0nl~rY41EW~oFlU@8z~I5)>+$?388nmS zRsLkxVlRQcGYHtj`cIz4$+_HG{>=AcSpUw)@%r-kI35p2HxVhxdy?^Z zu11SzNfU2U@i2g!)H?{^CcUHqJos<+f1+;(a(SKS*Wae_1>>qHr6R#pZ*|Jscy3-q~|dG9dr9pm%oy|Y getBlogs() { public BlogResponse getBlogById(@PathVariable String id) { return blogService.getBlogById(id); } + + /** + * ブログエントリを追加 + * @param blog + * @return + */ + @PostMapping + public ResponseEntity addBlog(@RequestBody BlogResponse blog) { + boolean isAdded = blogService.addBlog(blog); + + if (isAdded) { + return ResponseEntity.ok().build(); + } else { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + // 特定のIDのブログを削除 + @DeleteMapping("/{id}") + public ResponseEntity deleteBlogById(@PathVariable String id) { + boolean isDeleted = blogService.deleteBlogById(id); + + if (isDeleted) { + return ResponseEntity.ok().build(); + } else { + return ResponseEntity.notFound().build(); + } + } } diff --git a/backend/java/backend-api/src/main/java/com/example/backendapi/BlogService.java b/backend/java/backend-api/src/main/java/com/example/backendapi/BlogService.java index 6a10c66..502ceac 100644 --- a/backend/java/backend-api/src/main/java/com/example/backendapi/BlogService.java +++ b/backend/java/backend-api/src/main/java/com/example/backendapi/BlogService.java @@ -21,6 +21,7 @@ private Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:sqlite:./db/blog.db"); } + /** ブログ情報を全件取得する */ public List getAllBlogs() { List blogs = new ArrayList<>(); String sql = "SELECT * FROM blog"; @@ -59,4 +60,44 @@ public BlogResponse getBlogById(String id) { return null; } + /** + * 新規のブログを追加する + */ + public boolean addBlog(BlogResponse blog) { + String sql = "INSERT INTO blog (id, title, content, createdAt) VALUES (?, ?, ?, ?)"; + + try (Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + + pstmt.setString(1, blog.getId()); + pstmt.setString(2, blog.getTitle()); + pstmt.setString(3, blog.getContent()); + pstmt.setString(4, blog.getCreatedAt()); + int affectedRows = pstmt.executeUpdate(); + + return affectedRows > 0; + } catch (Exception e) { + System.out.println(e.getMessage()); + return false; + } + } + + /** + * 取得したIDのブログを削除する + */ + public boolean deleteBlogById(String id) { + String sql = "DELETE FROM blog WHERE id = ?"; + + try (Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + + pstmt.setString(1, id); + int affectedRows = pstmt.executeUpdate(); + + return affectedRows > 0; + } catch (Exception e) { + System.out.println(e.getMessage()); + return false; + } + } } diff --git a/frontend/.prettierignore b/frontend/.prettierignore new file mode 100644 index 0000000..82f419f --- /dev/null +++ b/frontend/.prettierignore @@ -0,0 +1,2 @@ +node_modules +.next \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index ca62b50..f89ea48 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -7,7 +7,7 @@ "build": "next build", "start": "next start", "lint": "next lint", - "json-server": "json-server --watch src/data/db.json --port 3001" + "json": "json-server --watch src/data/db.json --port 3001" }, "dependencies": { "json-server": "^0.17.4", diff --git a/frontend/src/app/Footer.tsx b/frontend/src/app/Footer.tsx index ee32c5b..b5c6325 100644 --- a/frontend/src/app/Footer.tsx +++ b/frontend/src/app/Footer.tsx @@ -2,8 +2,8 @@ import React from 'react'; const Footer = () => { return ( -