21.02.03 JSPCommunity 프로젝트(서버연결완료, 게시물 좋아요,싫어요 기능 추가, 댓글기능 완료(without ajax ver.))

2021. 2. 3. 21:40JAVA/JSP Community 사이트 프로젝트

<좋아요기능, 댓글기능>

// 좋아요 업데이트(without ajax)
	public String updateLikesCount(HttpServletRequest request, HttpServletResponse response) {
		// 게시물 번호가 입력됐는지 확인
		int articleId = Util.getAsInt(request.getParameter("articleId"), 0);
		if (articleId == 0) {
			return msgAndBack(request, "게시물 번호를 입력하세요.");
		}

		// 회원 아이디 확인
		int memberId = Util.getAsInt(request.getParameter("memberId"), 0);
		if (memberId == 0) {
			return msgAndBack(request, "회원 아이디를 입력하세요.");
		}

		// like
		int addLike = Util.getAsInt(request.getParameter("addLike"), 0);

		// unlike
		int addUnLike = Util.getAsInt(request.getParameter("addUnLike"), 0);

		System.out.println("articleId: " + articleId);
		System.out.println("memberId: " + memberId);
		System.out.println("addLike: " + addLike);
		System.out.println("addUnLike: " + addUnLike);

		// 이미 좋아요, 싫어요한 회원인지 확인
		boolean isAlreadylikeMember = articleService.isAlreadylikeMember(memberId, articleId);

		// 기존 좋아요,싫어요 기록 삭제
		if (isAlreadylikeMember) {
			articleService.removeLikeMember(memberId, articleId);

			// 게시물에 대한 LikesCount 가져오기
			int getArticleLikesCount = articleService.getArticleLikesCount(articleId);

			// 게시물에 대한 unLikesCount 가져오기
			int getArticleUnLikesCount = articleService.getArticleUnLikesCount(articleId);

			// 게시물 정보 수정
			Map<String, Object> args2 = new HashMap<>();
			args2.put("id", articleId);
			args2.put("likesCount", getArticleLikesCount);
			args2.put("unLikesCount", getArticleUnLikesCount);

			articleService.articleModify(args2);

			// 새로고침
			return noMsgAndReplaceUrl(request, "detail?id=" + articleId);
		}

		// 좋아요,싫어요 추가
		Map<String, Object> args = new HashMap<>();
		args.put("memberId", memberId);
		args.put("relTypeCode", "article");
		args.put("relId", articleId);

		if (addLike != 0) {
			args.put("point", addLike);
		}
		if (addUnLike != 0) {
			args.put("point", addUnLike);
		}

		articleService.addLikesCount(args);

		// 게시물에 대한 LikesCount 가져오기
		int getArticleLikesCount = articleService.getArticleLikesCount(articleId);

		// 게시물에 대한 unLikesCount 가져오기
		int getArticleUnLikesCount = articleService.getArticleUnLikesCount(articleId);

		// 게시물 정보 수정
		Map<String, Object> args2 = new HashMap<>();
		args2.put("id", articleId);
		args2.put("likesCount", getArticleLikesCount);
		args2.put("unLikesCount", getArticleUnLikesCount);

		articleService.articleModify(args2);

		// 새로고침
		return noMsgAndReplaceUrl(request, "detail?id=" + articleId);
	}

	// 댓글 등록
	public String reply(HttpServletRequest request, HttpServletResponse response) {

		// 게시물 번호가 입력됐는지 확인
		int articleId = Util.getAsInt(request.getParameter("articleId"), 0);
		if (articleId == 0) {
			return msgAndBack(request, "게시물 번호를 입력하세요.");
		}

		// 회원 아이디 확인
		int memberId = Util.getAsInt(request.getParameter("memberId"), 0);
		if (memberId == 0) {
			return msgAndBack(request, "회원 아이디를 입력하세요.");
		}

		// 댓글이 입력됐는지 확인
		String replyBody = request.getParameter("replyBody");
		if (Util.isEmpty(replyBody)) {
			return msgAndBack(request, "내용을 입력하세요.");
		}

		String relTypeCode = "article";

		// 댓글 등록
		articleService.addReply(articleId, memberId, relTypeCode, replyBody);

		// 게시물에 대한 댓글수 가져오기
		int getArticleRepliesCount = articleService.getArticleRepliesCount(articleId);

		// 게시물 정보 수정
		Map<String, Object> args2 = new HashMap<>();
		args2.put("id", articleId);
		args2.put("repliesCount", getArticleRepliesCount);

		articleService.articleModify(args2);

		// 새로고침
		return noMsgAndReplaceUrl(request, "detail?id=" + articleId);

	}

	// 댓글 수정
	public String doModifyReply(HttpServletRequest request, HttpServletResponse response) {
		// 댓글 번호가 입력됐는지 확인
		int id = Util.getAsInt(request.getParameter("id"), 0);
		if (id == 0) {
			return msgAndBack(request, "댓글 번호를 입력하세요.");
		}

		// 대상 번호
		int relId = Util.getAsInt(request.getParameter("relId"), 0);
		if (relId == 0) {
			return msgAndBack(request, "번호를 입력하세요.");
		}

		// 댓글 내용이 입력됐는지 확인
		String body = request.getParameter("body");
		if (Util.isEmpty(body)) {
			return msgAndBack(request, "내용을 입력하세요.");
		}

		// 댓글 수정
		Map<String, Object> args = new HashMap<>();
		args.put("id", id);
		args.put("body", body);

		articleService.replyModify(args);

		// 수정 알림창 보여주고 새로고침
		return msgAndReplaceUrl(request, id + "번 댓글이 수정되었습니다.", String.format("detail?id=%d", relId));
	}

	// 댓글 삭제
	public String doDeleteReply(HttpServletRequest request, HttpServletResponse response) {
		int memberId = (int) request.getAttribute("loginedMemberId");

		// 댓글 번호가 입력됐는지 확인
		int id = Util.getAsInt(request.getParameter("id"), 0);
		if (id == 0) {
			return msgAndBack(request, "댓글 번호를 입력하세요.");
		}

		// 대상 번호
		int relId = Util.getAsInt(request.getParameter("relId"), 0);
		if (relId == 0) {
			return msgAndBack(request, "번호를 입력하세요.");
		}

		// 해당 댓글이 존재하는지 확인
		Reply reply = articleService.getReplyById(id);

		if (reply == null) {
			return msgAndBack(request, id + "번 댓글은 존재하지 않습니다. 댓글 번호를 확인하세요.");
		}

		// 작성자 본인 여부 체크
		if (reply.getMemberId() != memberId) {
			return msgAndBack(request, "작성자만 삭제가 가능합니다.");
		}

		// 댓글 삭제
		articleService.replyDelete(id);

		// 게시물에 대한 댓글수 가져오기
		int getArticleRepliesCount = articleService.getArticleRepliesCount(relId);

		// 게시물 정보 수정
		Map<String, Object> args2 = new HashMap<>();
		args2.put("id", relId);
		args2.put("repliesCount", getArticleRepliesCount);

		articleService.articleModify(args2);

		// 삭제 알림창 보여주고 새로고침
		return msgAndReplaceUrl(request, "삭제되었습니다.", "detail?id=" + relId);

	}

<환경별 DB접속정보 분기로직 적용>

// 환경별 DB접속정보 분기로직 적용(21.02.03)
		String profilesActive = System.getProperty("spring.profiles.active");
		
		boolean isProductionMode = false;

		if (profilesActive != null && profilesActive.equals("production")) {
		  isProductionMode = true;
		}
				
		if ( isProductionMode ) {
		  MysqlUtil.setDBInfo("127.0.0.1", "sbsstLocal", "sbs123414", "jspCommunity");
		}
		else {
		  MysqlUtil.setDBInfo("127.0.0.1", "sbsst", "sbs123414", "jspCommunity");			
		}