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; import org.springframework.http.ResponseEntity; 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.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; import io.company.localhost.common.dto.MapDto; import io.company.localhost.service.LocalBordService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @RestController @RequestMapping("/api/board") @RequiredArgsConstructor @Slf4j public class BoardController { private final LocalBordService boardService; //공지사항 @GetMapping("/notices") public ApiResponse> getNotices() { return ApiResponse.ok(boardService.getNotices()); } //비밀,자유게시판 @GetMapping("/general") public ApiResponse> getGeneralPosts() { List posts = boardService.getGeneralPosts(); for (Map post : posts) { Object content = post.get("content"); if (content instanceof Blob) { Blob blob = (Blob) content; try { post.put("content", new String(blob.getBytes(1, (int) blob.length()), StandardCharsets.UTF_8)); } catch (Exception e) { post.put("content", ""); // 변환 실패 시 기본 값 설정 } } } return ApiResponse.ok(posts); } //게시물 작성 @PostMapping 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("boardId") Long boardId, @RequestParam(name = "file") MultipartFile file, @RequestParam(name = "CMNFLEPAT") String filePath, @RequestParam(name = "CMNFLEORG") String originalFileName, @RequestParam(name = "CMNFLEEXT") String fileExtension, @RequestParam(name = "CMNFLESIZ") Long fileSize, @RequestParam(name = "CMNFLEREG") Long registrantId, @RequestParam(name = "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(fileData); return ApiResponse.ok("첨부파일이 저장되었습니다."); } // 게시물 상세보기 @GetMapping("/{boardId}") public ApiResponse> getBoardDetail(@PathVariable Long boardId) { log.info("Fetching details for board ID: {}", boardId); // 게시물 상세정보 조회 MapDto boardDetail = boardService.getBoardDetail(boardId); // 첨부파일 목록 조회 List attachments = boardService.getAttachments(boardId); // 댓글 목록 조회 List comments = boardService.getComments(boardId.intValue()); // 결과 조합 Map result = new HashMap<>(); result.put("boardDetail", boardDetail); result.put("attachments", attachments); result.put("comments", comments); return ApiResponse.ok(result); } // 게시물 삭제 @DeleteMapping("/{boardId}") public ApiResponse deleteBoard(@PathVariable Long boardId, @ReqMap MapDto map) { map.put("LOCBRDSEQ", boardId); log.info("Deleting board with ID: {}", boardId); boardService.deleteBoard(map); return ApiResponse.ok("게시물이 삭제되었습니다."); } //게시물 수정 @PutMapping public ApiResponse updateBoard(@ReqMap MapDto map) { boardService.updateBoard(map); return ApiResponse.ok("게시물이 수정되었습니다."); } //게시물과 댓글에 좋아요/싫어요 추가 @PostMapping("/{boardId}/reaction") public ApiResponse reactToBoard(@PathVariable Long boardId, @ReqMap MapDto map) { map.put("LOCBRDSEQ", boardId); boardService.reactToBoard(map); return ApiResponse.ok("반응이 추가되었습니다."); } //댓글/대댓글 조회 @GetMapping("/{boardId}/comments") public ApiResponse> getComments(@PathVariable int boardId) { return ApiResponse.ok(boardService.getComments(boardId)); } //댓글/대댓글 작성 @PostMapping("/{boardId}/comment") public ApiResponse addCommentOrReply(@PathVariable int boardId, @ReqMap MapDto map) { map.put("LOCBRDSEQ", boardId); boardService.addCommentOrReply(map); return ApiResponse.ok("댓글 또는 대댓글이 작성되었습니다."); } //댓글/대댓글 수정 @PutMapping("/comment/{commentId}") public ApiResponse updateComment(@PathVariable int commentId, @ReqMap MapDto map) { map.put("LOCCMTSEQ", commentId); boardService.updateComment(map); return ApiResponse.ok("댓글이 수정되었습니다."); } //댓글/대댓글 삭제 @DeleteMapping("/comment/{commentId}") public ApiResponse deleteComment(@PathVariable int commentId, @ReqMap MapDto map) { map.put("LOCCMTSEQ", commentId); boardService.deleteComment(map); return ApiResponse.ok("댓글이 삭제되었습니다."); } //비밀번호 확인 (게시물) @PostMapping("/comment/{commentId}/password") public ApiResponse checkCommentPassword(@PathVariable int commentId, @ReqMap MapDto map) { map.put("LOCCMTSEQ", commentId); return ApiResponse.ok(boardService.checkCommentPassword(map)); } //비밀번호 확인 (댓글) @PostMapping("/{boardId}/password") public ApiResponse checkBoardPassword(@PathVariable int boardId, @ReqMap MapDto map) { map.put("LOCBRDSEQ", boardId); return ApiResponse.ok(boardService.checkBoardPassword(map)); } // 비밀게시판 여부 확인 @GetMapping("/{boardId}/isSecret") public ApiResponse isSecretBoard(@PathVariable Long boardId) { log.info("Checking if board ID {} is secret", boardId); return ApiResponse.ok(boardService.isSecretBoard(boardId)); } }