From 36f255923212db44fb75381e3a9b893da2a8b77f Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Mon, 13 Jan 2025 12:34:44 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=ED=8C=90=20=EB=B0=B1=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../security/config/SecurityConfig.java | 2 +- .../controller/api/BoardController.java | 51 +++++++++++++++---- .../localhost/mapper/LocalBordMapper.java | 6 +-- .../localhost/service/LocalBordService.java | 12 +++-- src/main/resources/mapper/LocalBordMapper.xml | 39 +++++++------- 6 files changed, 73 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index ace8992..9a467ae 100644 --- a/build.gradle +++ b/build.gradle @@ -131,3 +131,4 @@ tasks.javadoc { tasks.named('test') { useJUnitPlatform() } + diff --git a/src/main/java/io/company/localhost/common/security/config/SecurityConfig.java b/src/main/java/io/company/localhost/common/security/config/SecurityConfig.java index 2432122..ae11f31 100644 --- a/src/main/java/io/company/localhost/common/security/config/SecurityConfig.java +++ b/src/main/java/io/company/localhost/common/security/config/SecurityConfig.java @@ -76,7 +76,7 @@ public class SecurityConfig { http .securityMatcher("/api/**") // '/api/**' 경로에 대해서만 보안 적용 .authorizeHttpRequests(auth -> - auth.requestMatchers("/api/board/general").permitAll() // 특정 엔드포인트 허용 + auth.requestMatchers("/api/**").permitAll() // 특정 엔드포인트 허용 .anyRequest().authenticated() // 나머지 요청은 인증 필요 //auth.anyRequest().access(authorizationManager) // 모든 요청에 대해 권한 관리 ) diff --git a/src/main/java/io/company/localhost/controller/api/BoardController.java b/src/main/java/io/company/localhost/controller/api/BoardController.java index 4b1888d..0b13f60 100644 --- a/src/main/java/io/company/localhost/controller/api/BoardController.java +++ b/src/main/java/io/company/localhost/controller/api/BoardController.java @@ -1,7 +1,10 @@ package io.company.localhost.controller.api; +import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.sql.Blob; +import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -15,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; import io.company.localhost.common.annotation.ReqMap; import io.company.localhost.common.dto.ApiResponse; @@ -32,13 +36,13 @@ public class BoardController { //공지사항 @GetMapping("/notices") - public ApiResponse>> getNotices() { + public ApiResponse> getNotices() { return ApiResponse.ok(boardService.getNotices()); } //비밀,자유게시판 @GetMapping("/general") - public ApiResponse>> getGeneralPosts() { - List> posts = boardService.getGeneralPosts(); + public ApiResponse> getGeneralPosts() { + List posts = boardService.getGeneralPosts(); for (Map post : posts) { Object content = post.get("content"); if (content instanceof Blob) { @@ -50,23 +54,48 @@ public class BoardController { } } } - System.out.println(posts); return ApiResponse.ok(posts); } //게시물 작성 @PostMapping - public ApiResponse createBoard(@ReqMap MapDto map) { - boardService.createBoard(map); - return ApiResponse.ok("게시물이 작성되었습니다."); + public ApiResponse createBoard(@ReqMap MapDto map) { + BigInteger createdIdx = boardService.createBoard(map); // 작성된 게시물의 idx를 반환 + Map responseData = new HashMap<>(); + responseData.put("boardId", createdIdx); + responseData.put("message", "게시물이 작성되었습니다."); + return ApiResponse.ok(responseData); } // 첨부파일 추가 @PostMapping("/{boardId}/attachments") - public ApiResponse uploadAttachment(@PathVariable Long boardId, @ReqMap MapDto map) { - map.put("LOCBRDSEQ", boardId); + public ApiResponse uploadAttachment( + @PathVariable("boardId") Long boardId, + @RequestParam("file") MultipartFile file, + @RequestParam("CMNFLEPAT") String filePath, + @RequestParam("CMNFLEORG") String originalFileName, + @RequestParam("CMNFLEEXT") String fileExtension, + @RequestParam("CMNFLESIZ") Long fileSize, + @RequestParam("CMNFLEREG") Long registrantId, + @RequestParam("CMNFLENAM") String fileName + ) { + // 데이터 준비 + MapDto fileData = new MapDto(); + fileData.put("CMNBRDSEQ", boardId); + fileData.put("CMNFLENAM", fileName); // 업로드된 파일 이름 + fileData.put("CMNFLEORG", originalFileName); + fileData.put("CMNFLEPAT", filePath); + fileData.put("CMNFLEEXT", fileExtension); + fileData.put("CMNFLESIZ", fileSize); + fileData.put("CMNFLEREG", registrantId); + fileData.put("CMNFLERDT", new Date()); // 등록일 현재 시간 + log.info("Uploading attachment for board ID: {}", boardId); - boardService.addAttachment(map); + + // 파일 데이터 저장 + boardService.addAttachment(fileData); + return ApiResponse.ok("첨부파일이 저장되었습니다."); } + // 게시물 삭제 @DeleteMapping("/{boardId}") public ApiResponse deleteBoard(@PathVariable Long boardId, @ReqMap MapDto map) { @@ -90,7 +119,7 @@ public class BoardController { } //댓글/대댓글 조회 @GetMapping("/{boardId}/comments") - public ApiResponse>> getComments(@PathVariable int boardId) { + public ApiResponse> getComments(@PathVariable int boardId) { return ApiResponse.ok(boardService.getComments(boardId)); } //댓글/대댓글 작성 diff --git a/src/main/java/io/company/localhost/mapper/LocalBordMapper.java b/src/main/java/io/company/localhost/mapper/LocalBordMapper.java index d86c9a3..4be4d47 100644 --- a/src/main/java/io/company/localhost/mapper/LocalBordMapper.java +++ b/src/main/java/io/company/localhost/mapper/LocalBordMapper.java @@ -11,10 +11,10 @@ import io.company.localhost.common.dto.MapDto; @Mapper public interface LocalBordMapper { // 공지사항 조회 - List> getNotices(); + List getNotices(); // 자유/비밀 게시판 조회 - List> getGeneralPosts(); + List getGeneralPosts(); // 게시물 작성 void createBoard(MapDto map); @@ -32,7 +32,7 @@ public interface LocalBordMapper { void reactToBoard(MapDto map); // 댓글 조회 - List> getComments(int boardSeq); + List getComments(int boardSeq); // 댓글/대댓글 작성 void addCommentOrReply(MapDto map); diff --git a/src/main/java/io/company/localhost/service/LocalBordService.java b/src/main/java/io/company/localhost/service/LocalBordService.java index 29490f0..8e1ee8c 100644 --- a/src/main/java/io/company/localhost/service/LocalBordService.java +++ b/src/main/java/io/company/localhost/service/LocalBordService.java @@ -1,5 +1,6 @@ package io.company.localhost.service; +import java.math.BigInteger; import java.util.List; import java.util.Map; @@ -14,16 +15,17 @@ import lombok.RequiredArgsConstructor; public class LocalBordService { private final LocalBordMapper boardMapper; - public List> getNotices() { + public List getNotices() { return boardMapper.getNotices(); } - public List> getGeneralPosts() { + public List getGeneralPosts() { return boardMapper.getGeneralPosts(); } - public void createBoard(MapDto map) { - boardMapper.createBoard(map); + public BigInteger createBoard(MapDto map) { + boardMapper.createBoard(map); // 게시물 작성 + return (BigInteger) map.get("LOCBRDSEQ"); // Mapper에서 자동 생성된 key를 가져옴 } public void addAttachment(MapDto map) { @@ -42,7 +44,7 @@ public class LocalBordService { boardMapper.reactToBoard(map); } - public List> getComments(int boardSeq) { + public List getComments(int boardSeq) { return boardMapper.getComments(boardSeq); } diff --git a/src/main/resources/mapper/LocalBordMapper.xml b/src/main/resources/mapper/LocalBordMapper.xml index bf80815..ca1cd6d 100644 --- a/src/main/resources/mapper/LocalBordMapper.xml +++ b/src/main/resources/mapper/LocalBordMapper.xml @@ -5,18 +5,14 @@ @@ -32,16 +28,23 @@ - - INSERT INTO localbord (LOCBRDTTL, LOCBRDCON, LOCBRDCAT, MEMBERSEQ, LOCBRDCNT, LOCBRDRDT, LOCBRDUDT, LOCBRDPWD, LOCBRDTYP) - VALUES (#{LOCBRDTTL}, #{LOCBRDCON}, #{LOCBRDCAT}, #{MEMBERSEQ}, 0, NOW(), NOW(), #{LOCBRDPWD}, #{LOCBRDTYP}) - + + INSERT INTO localbord (LOCBRDTTL, LOCBRDCON, LOCBRDCAT, MEMBERSEQ, LOCBRDCNT, LOCBRDRDT, LOCBRDUDT, LOCBRDPWD, LOCBRDTYP) + VALUES (#{LOCBRDTTL}, #{LOCBRDCON}, #{LOCBRDCAT}, #{MEMBERSEQ}, 0, NOW(), NOW(), #{LOCBRDPWD}, #{LOCBRDTYP}) + + - - INSERT INTO commonfil (CMNBRDSEQ, CMNFLEPAT, CMNFLENAM, CMNFLEORG, CMNFLEEXT, CMNFLESIZ, CMNFLEREG, CMNFLERDT) - VALUES (#{CMNBRDSEQ}, #{CMNFLEPAT}, #{CMNFLENAM}, #{CMNFLEORG}, #{CMNFLEEXT}, #{CMNFLESIZ}, #{CMNFLEREG}, NOW()) + + INSERT INTO commonfil ( + CMNBRDSEQ,CMNFLENAM,CMNFLEORG,CMNFLEPAT, + CMNFLEEXT,CMNFLESIZ,CMNFLEREG,CMNFLERDT + ) VALUES ( + #{CMNBRDSEQ},#{CMNFLENAM},#{CMNFLEORG},#{CMNFLEPAT}, + #{CMNFLEEXT},#{CMNFLESIZ},#{CMNFLEREG},#{CMNFLERDT} + ) +