2024-02-15(46일차) - Spring

민짱·2024년 2월 15일

📅2024. 02.15 46일차


🎬2024_01_Spring_AM

좋아요 기능 구현

  • 버튼으로 구현

띵킹

  • 일단 detail.jsp에서 버튼을 구현하자.
<tr>
	<th>추천</th>
	<td><div class="flex items-center"><span class="text-blue-700">${article.goodReactionPoint }</span>
	<span>&nbsp;</span>
    <a href="/usr/reactionPoint/doGoodReactionrelTypeCode=article&relId=${param.id}&replaceUri=${rq.currentUri}" 	class="reaction btn btn-xs btn-primary">좋아요👍</a>
	<span>&nbsp;</span>
	<a href="/usr/reactionPoint/doBadReaction?relTypeCode=article&relId=${param.id }&replaceUri=${rq.currentUri}" class="reaction btn btn-xs btn-secondary">싫어요👎</a>
					
	 	</div>
    </td>
</tr>
  • UsrReactionPointController 구현
  • 현재 로그인되어있는 Id을 통해 로그인 중인 사람의 userReaction를 select하여 1이면 이미 좋아요를 눌른 것이니 이미 했다고 알림창이 나오게 만들면 될 듯. 반대로 -1이 나오면 이미 싫어요를 눌른 것이니 똑같이 이미 했다고 알림창이 나오면 됨.
@RequestMapping("/usr/reactionPoint/doGoodReaction")
	@ResponseBody
	public String doGoodReaction(String relTypeCode, int relId, String replaceUri) {

		int usersReaction = reactionPointService.usersReaction(rq.getLoginedMemberId(), relTypeCode, relId);

		if (usersReaction == 1) {
			
			return Ut.jsHistoryBack("F-1", "이미 함");
		}

		ResultData reactionRd = reactionPointService.increaseReactionPoint(rq.getLoginedMemberId(), relTypeCode, relId);

		return Ut.jsReplace(reactionRd.getResultCode(), reactionRd.getMsg(), replaceUri);
	}
	
	@RequestMapping("/usr/reactionPoint/doBadReaction")
	@ResponseBody
	public String doBadReaction(String relTypeCode, int relId, String replaceUri) {

		int usersReaction = reactionPointService.usersReaction(rq.getLoginedMemberId(), relTypeCode, relId);

		if (usersReaction == -1) {
			
			return Ut.jsHistoryBack("F-1", "이미 함");
		}

		ResultData reactionRd = reactionPointService.decreaseReactionPoint(rq.getLoginedMemberId(), relTypeCode, relId);

		return Ut.jsReplace(reactionRd.getResultCode(), reactionRd.getMsg(), replaceUri);
	}
  • ReactionPointService 구현
  • 로그인 했을 때만 좋아요, 싫어요 가능하게 구현
public int usersReaction(int loginedMemberId, String relTypeCode, int relId) {

		// 로그인 x
		if (loginedMemberId == 0) {
			return -2;
		}

		return reactionPointRepository.getSumReactionPoint(loginedMemberId, relTypeCode, relId);
	}

	public ResultData increaseReactionPoint(int loginedMemberId, String relTypeCode, int relId) {

		int affectedRow = reactionPointRepository.increaseReactionPoint(loginedMemberId, relTypeCode, relId);

		if (affectedRow != 1) {
			return ResultData.from("F-2", "좋아요 실패");
		}

		switch (relTypeCode) {
		case "article":
			articleService.increaseGoodReactionPoint(relId);
			break;
		}

		return ResultData.from("S-1", "좋아요!");
	}

	public ResultData decreaseReactionPoint(int loginedMemberId, String relTypeCode, int relId) {
		int affectedRow = reactionPointRepository.decreaseReactionPoint(loginedMemberId, relTypeCode, relId);

		if (affectedRow != 1) {
			return ResultData.from("F-2", "싫어요 실패");
		}

		switch (relTypeCode) {
		case "article":
			articleService.increaseBadReactionPoint(relId);
			break;
		}

		return ResultData.from("S-1", "싫어요!");
	}
  • ReactionPointRepository 구현
@Mapper
public interface ReactionPointRepository {

	@Select("""
			SELECT IFNULL(SUM(RP.point),0)
			FROM reactionPoint AS RP
			WHERE RP.relTypeCode = #{relTypeCode}
			AND RP.relId = #{relId}
			AND RP.memberId = #{memberId}
			""")
	public int getSumReactionPoint(int memberId, String relTypeCode, int relId);

	@Insert("""
			INSERT INTO reactionPoint
			SET regDate = NOW(),
			updateDate = NOW(),
			relTypeCode = #{relTypeCode},
			relId = #{relId},
			memberId = #{memberId},
			`point` = 1
			""")
	public int increaseReactionPoint(int memberId, String relTypeCode, int relId);
	
	@Insert("""
			INSERT INTO reactionPoint
			SET regDate = NOW(),
			updateDate = NOW(),
			relTypeCode = #{relTypeCode},
			relId = #{relId},
			memberId = #{memberId},
			`point` = -1
			""")
	public int decreaseReactionPoint(int memberId, String relTypeCode, int relId);

}
  • ArticleRepository 쿼리 문 추가
  • article table에 있는 column에 update
@Update("""
			UPDATE article
			SET goodReactionPoint = goodReactionPoint + 1
			WHERE id = #{relId}
			""")
	public int increaseGoodReactionPoint(int relId);
	
	
	@Update("""
			UPDATE article
			SET badReactionPoint = badReactionPoint + 1
			WHERE id = #{relId}
			""")
	public int increaseBadReactionPoint(int relId);

0개의 댓글