package com.bb.front; import java.util.HashMap; import java.util.List; import java.util.Map; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.bb.exception.ApiException; import com.bb.jwt.JwtManager; import com.bb.model.ApiResponse; import com.bb.model.Board; import com.bb.model.BoardListSearch; import com.bb.model.Comment; import com.bb.model.Message; import com.bb.model.MessageListSearch; import com.bb.model.PageFormVO; import com.bb.model.Site; import com.bb.service.BoardService; import com.bb.service.CommonService; import com.bb.service.UserService; import com.bb.util.PagingUtil; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @RestController @Slf4j @RequestMapping("/api/board") @RequiredArgsConstructor @SecurityRequirement(name = "Authorization") public class ApiBoardController { private final BoardService boardService; private final UserService userService; private final CommonService commonService; private final JwtManager jwtManager; @ResponseBody @PostMapping(value={"/{boardType}/list"}) public ApiResponse blist(@RequestHeader String token, HttpServletRequest request, @PathVariable String boardType, @Valid @RequestBody BoardListSearch search) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } final String boardTypeKeyword = "answer|bbs|faq|favor|nt|snt"; if(boardType.equals("qna")) { boardType = "faq"; } if(!boardTypeKeyword.contains(boardType)) { log.error("[REQUEST: "+request.getRequestURI()+"] Board["+boardType+"] boardType Error"); throw new ApiException("BRD02", "boardType Error"); } search.setBoardType(boardType); search.setIsUser("Y"); JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); PageFormVO pageVo= new PageFormVO(); if(search.getPage()==0) search.setPage(1); search.setSiteId(tokenInfo.getSid()); int totalCount = boardService.getBoardListCnt(search); if(totalCount != 0) { PageFormVO commonForm = new PageFormVO(); commonForm.setFunction_name("goPage"); commonForm.setPage(search.getPage()); commonForm.setCount_per_page(10); if(search.getCount_per_list()==0) { commonForm.setCount_per_list(10); } else { commonForm.setCount_per_list(search.getCount_per_list()); } commonForm.setTatal_list_count(totalCount); pageVo = PagingUtil.setPageUtil(commonForm); search.setLimit(pageVo.getLimit()); search.setOffset(pageVo.getOffset()); search.setTatal_list_count(totalCount); apiResponse.put("pageInfo", pageVo); } List boardList = boardService.getBoardList(search); List boardIdxList = boardService.getBoardIdxList(search); apiResponse.put("list", boardList); apiResponse.put("idxList", boardIdxList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/{boartType}/datail"}) public ApiResponse datail(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody Board board) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); board.setSiteId(tokenInfo.getSid()); Board boardetail = boardService.getBoard(board); if(!tokenInfo.getSid().equals(boardetail.getSiteId())) { throw new ApiException("1000", "accessDinied"); } if(boardetail.getBoardType().equals("faq") && !tokenInfo.getMid().equals(boardetail.getRegId())) { throw new ApiException("1000", "accessDinied"); } if(boardetail.getBoardType().equals("faq") && boardetail.getStatus().equals("ANSWER") && boardetail.getIsReadUser().equals("N")) { boardService.userAnswerCmtRead(boardetail); } boardService.viewAdd(boardetail); apiResponse.put("board", boardetail); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/{boardType}/save"}) public ApiResponse bsave(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody Board board) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); if(board.getBoardIdx() != null ) { Board boardetail = boardService.getBoard(board); if(!boardetail.getRegId().equals(tokenInfo.getMid())) { throw new ApiException("2000", "accessDinied"); } } board.setSiteId(tokenInfo.getSid()); board.setRegId(tokenInfo.getMid()); int res = boardService.saveBoard(board); try { Map logParam = new HashMap(); logParam.put("siteId", site.getSiteId()); logParam.put("memId", tokenInfo.getMid()); logParam.put("actionType", 2); commonService.updateAcctionLog(logParam); } catch(Exception e){System.out.print(e.toString());} apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/cmtSave"}) public ApiResponse cmtsave(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody Comment comment) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); if(comment.getCmtIdx() != null) { Comment commentrdetail = boardService.getComment(comment); if(!commentrdetail.getRegId().equals(tokenInfo.getMid())) { throw new ApiException("2000", "accessDinied"); } } comment.setRegId(tokenInfo.getMid()); int res = boardService.saveCmt(comment); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/flowBoard"}) public ApiResponse siteFlowBoard(@RequestHeader String token, HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { // Site site = getSiteByCheck(request); if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); BoardListSearch search = new BoardListSearch(); search.setSiteId(tokenInfo.getSid()); Board flowBoard = boardService.getSiteFlowBoard(search); log.info("[REQUEST: "+request.getRequestURI()+"] " + flowBoard); String content = ""; if(flowBoard != null) { content = flowBoard.getContent(); } apiResponse.put("content", content); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/msgList", "/msgHeadList"}) public ApiResponse msgList(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody MessageListSearch search) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); if("msgHeadList".indexOf(request.getRequestURI()) > -1){ search.setMsgType("head"); } else { search.setMsgType("msg"); } search.setHiddenYn("N"); log.info("[REQUEST: "+request.getRequestURI()+"] " + search); PageFormVO pageVo= new PageFormVO(); if(search.getPage()==0)search.setPage(1); search.setSiteId(tokenInfo.getSid()); search.setRecieveId(tokenInfo.getMid()); int totalCount = boardService.getUserMsgListCnt(search); if (totalCount != 0) { PageFormVO commonForm = new PageFormVO(); commonForm.setFunction_name("goPage"); commonForm.setPage(search.getPage()); commonForm.setCount_per_page(10); if(search.getCount_per_list()==0) { commonForm.setCount_per_list(10); } else { commonForm.setCount_per_list(search.getCount_per_list()); } commonForm.setTatal_list_count(totalCount); pageVo = PagingUtil.setPageUtil(commonForm); search.setLimit(pageVo.getLimit()); search.setOffset(pageVo.getOffset()); search.setTatal_list_count(totalCount); apiResponse.put("pageInfo", pageVo); } List boardList = boardService.getUserMsgList(search); apiResponse.put("list", boardList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/msgRead"}) public ApiResponse msgRead(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody Message message) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); message.setSiteId(tokenInfo.getSid()); message.setRecieveId(tokenInfo.getMid()); log.info("[REQUEST: "+request.getRequestURI()+"] " + message); if(message.getMsgIdx() != null && message.getMsgIdx()!=0) { log.info("[MSG READ idx: " + message.getMsgIdx()+"]"); boardService.msgRead(message); } else { log.info("[MSG READ ALL]"); boardService.allRead(message); } apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/msgUpdate"}) public ApiResponse msgUpdate(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody Message message) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); message.setSiteId(tokenInfo.getSid()); message.setRecieveId(tokenInfo.getMid()); log.info("[REQUEST: "+request.getRequestURI()+"] " + message); if(message.getMsgIdx() != null && message.getMsgIdx() != 0) { log.info("[MSG DEL idx: " + message.getMsgIdx()+"]"); message.setStatus("N"); boardService.msgUpdate(message); } else { log.info("[MSG DEL ALL]"); boardService.msgDelList(message); } apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } private Site getSiteByCheck(HttpServletRequest request) { Site site = userService.getSiteInfoByKey(request.getHeader("Authorization").toString()); return site; } }