댓글 페이지 작업

오창진·2024년 3월 29일

프로젝트

목록 보기
2/17

댓글 페이지 작업 하기

(#######detail 부분######)

//상세보기
	@RequestMapping("/usr/article/detail")
	public String showDetail(HttpServletRequest req, Model model, int id, @RequestParam(defaultValue = "1") int page) {
		Rq rq = (Rq) req.getAttribute("rq");

		Article article = articleService.getForPrintArticle(rq.getLoginedMemberId(), id);
		ResultData usersReactionRd = reactionPointService.usersReaction(rq.getLoginedMemberId(), "article", id);

		if (usersReactionRd.isSuccess()) {
			model.addAttribute("userCanMakeReaction", usersReactionRd.isSuccess());
		}
		
		String[] tags = article.getTag().split("#");

		//여기까지 게시물 상세보기에 대한 작업
		
		//여기서부터 댓글 작업
		//댓글의 총 개수 가져오기
		int repliesCount = replyService.getRepliesCount(id);
		
		//이전 list에서 페이지 작업 하는것을 모방하여 만들기
		
		//한 페이지에 보여줄 댓글 수
		int itemsInAPageReply = 10;
		//총 페이지 버튼의 개수 (댓글이 218개면 22개의 버튼이 필요함)
		int replyPagesCount = (int) Math.ceil(repliesCount / (double) itemsInAPageReply);
		
		//댓글 컨트롤하기
		List<Reply> replies = replyService.getForPrintReplies(rq.getLoginedMemberId(), "article", id,itemsInAPageReply, page);
		
		model.addAttribute("page", page);
		model.addAttribute("replyPagesCount", replyPagesCount);
		
		
		
		model.addAttribute("tags", tags);
		model.addAttribute("article", article);
		model.addAttribute("replies", replies);
		model.addAttribute("repliesCount", repliesCount);
		model.addAttribute("isAlreadyAddGoodRp",
				reactionPointService.isAlreadyAddGoodRp(rq.getLoginedMemberId(), id, "article"));
		model.addAttribute("isAlreadyAddBadRp",
				reactionPointService.isAlreadyAddBadRp(rq.getLoginedMemberId(), id, "article"));
		

		return "usr/article/detail";
	}

(####service getForprintReplies 부분######)

public List<Reply> getForPrintReplies(int loginedMemberId, String relTypeCode, int relId, int itemsInAPageReply, int page) {
		//리스트 방식 참고하여 sql에서 limit 할때 시작과 끝부분을 설정
		int limitFrom = (page - 1) * itemsInAPageReply;
		int limitTake = itemsInAPageReply;
		
		List<Reply> replies = replyRepository.getForPrintReplies(loginedMemberId, relTypeCode, relId,limitFrom,limitTake);

		for (Reply reply : replies) {
			controlForPrintData(loginedMemberId, reply);
		}

		return replies;
	}

(####### service controlForPrintDate 부분#################)

private void controlForPrintData(int loginedMemberId, Reply reply) {
		if (reply == null) {
			return;
		}
		ResultData userCanModifyRd = userCanModify(loginedMemberId, reply);
		reply.setUserCanModify(userCanModifyRd.isSuccess());

		ResultData userCanDeleteRd = userCanDelete(loginedMemberId, reply);
		reply.setUserCanDelete(userCanDeleteRd.isSuccess());
	}

	public ResultData userCanDelete(int loginedMemberId, Reply reply) {

		if (reply.getMemberId() != loginedMemberId) {
			return ResultData.from("F-2", Ut.f("%d번 댓글에 대한 삭제 권한이 없습니다", reply.getId()));
		}

		return ResultData.from("S-1", Ut.f("%d번 댓글이 삭제 되었습니다", reply.getId()));
	}

	public ResultData userCanModify(int loginedMemberId, Reply reply) {

		if (reply.getMemberId() != loginedMemberId) {
			return ResultData.from("F-2", Ut.f("%d번 댓글에 대한 수정 권한이 없습니다", reply.getId()));
		}

		return ResultData.from("S-1", Ut.f("%d번 댓글을 수정했습니다", reply.getId()));
	}

수정삭제 권한 체크 하는것
(##################repository 쿼리문####################)

@Select("""
				<script>
				SELECT R.*, M.nickname AS extra__writer
				FROM reply AS R
				INNER JOIN `member` AS M
				ON R.memberId = M.id
				WHERE relTypeCode = #{relTypeCode}
				AND relId = #{relId}
				ORDER BY R.id ASC
				<if test="limitFrom >= 0 ">
					LIMIT #{limitFrom}, #{limitTake}
				</if>
				</script>
			""")
	List<Reply> getForPrintReplies(int loginedMemberId, String relTypeCode, int relId, int limitFrom, int limitTake);

리미트 부분 추가

상세보기 페이지에서 댓글 페이지 작업 완료
-> 개선점 ajax를 통해서 전체 새로고침이 아닌 부분새로고침으로 댓글부분만 계속 움직일수 있으면 좋겟다

0개의 댓글