📗 self-study/📗 KH정보교육원 당산지원

완료 - [spring - 관리자 코멘트 검색 코드]

천재강쥐 2022. 12. 7. 09:40

💻 adminCommentView

👉🏻 기본적인 더미는 넣을 것을 감안하여 리스트 조건을

(1) 기본 접속 시 전체 코멘트 리스트 출력

(2) 검색 시 검색된 키워드에 해당하는 코멘트가 있다면 해당 코멘트 출력

(3) 검색 시 검색된 키워드에 해당하는 코멘트가 없다면 '조회된 코멘트가 없습니다.' 출력

 

👉🏻 코멘트의 개수에 따라 페이징바도 달라져야 하기 때문에 페이징바도 조건에 함께 넣음

👉🏻 검색어 유지를 위해 condition에 대한 조건, keyword를 value 값으로 넣음

👉🏻 해당 과정에서 검색 form을 싸고 있는 div의 id값(selectUserMail)이 부적절하다고 판단되어 selectCommentInfo로 수정함 (css도)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>관리자 코멘트 관리</title>

	<link href="resources/css/adminCommentView.css" rel="stylesheet">

	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
	<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
	<!--
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="https://bootswatch.com/5/lux/bootstrap.css">
	-->

</head>
<body>
	<!-- 전체 영역 -->
    <div class="wrap">

		<!-- 플로팅 버튼 영역 -->
        <jsp:include page="../common/floatingButton.jsp" />

		<!-- 헤더 영역 -->
        <jsp:include page="../common/header.jsp" />
	
		
        <!-- 컨텐츠 영역 (개별 구현 구역) -->
        <div id="content_container">
        	
	        <table id="admin_profile">
	            <tr>
	                <td width="220"><img src="resources/image/user/profile/admin.png" width="170" height="170"></td>
	                <td width="380" style="font-size:50px; font-weight:900; color:white;">관리자</td>
	            </tr>
	        </table>
	    	
	    	<!-- !!! 본인이 맡은 탭 div에 id="selected_tab" 붙어녛기 !!!-->
		    <div id="admin_mypage_navi">
		        <div><a href="admin_list.me">회원 관리</a></div>
		        <div><a href="">콘텐츠 관리</a></div>
				<div id="selected_tab"><a href="commentList.ad">코멘트 관리</a></div>
		        <div><a href="">이용권 관리</a></div>       
		        <div><a href="">신고 관리</a></div>
		        <div><a href="admin_stat.st">통계 관리</a></div>
			</div>

			<!-- 이곳부터 본인 화면 구현 -->
			<div id="commentManagementArea">
				<br><br>
				<div id="selectCommentInfo">
					<form action="searchComment.ad" method="get">
						<select id="selectCommentMenu" name="condition">
							<option value="userId" name="userId">이메일</option>
							<option value="userNickname" name="userNickname">닉네임</option>
							<option value="contentsTitle" name="contentsTitle">콘텐츠명</option>
						</select>
						<input type="text" id="inputUserMail" name="keyword" value="${ keyword }" placeholder="검색할 유저의 정보를 입력하세요">&nbsp;
						<button type="submit" id="selectButton">검색</button>
					</form>
				</div>
				<!-- 검색어 유지를 위해 추가 -->
				<c:if test="${ not empty condition }">
					<script>
						$(function() {
							$("#selectCommentInfo option[value=${ condition }]").attr("selected", true);
						});
					</script>
				</c:if>
				<br><br>
				<div id="commentListAll">
					<br>
					<c:choose>
						<c:when test="${ not empty list }">
							<table id="commentTable">
								<thead>
									<tr id="comment_head" class="line">
										<th class="comment_head1" width="5%">선택</th>
										<th class="comment_head1" width="15%;">유저 이메일</th>
										<th class="comment_head1" width="10%;">유저 닉네임</th>
										<th class="comment_head1" width="20%;">콘텐츠명</th>
										<th class="comment_head1" width="10%;">별점</th>
										<th class="comment_head1" width="40%">코멘트 내용</th>
									</tr>
								</thead>
								<tbody>
									<c:forEach var="r" items="${ list }">
										<tr class="personalComment">
											<td><input type="checkbox" name="selectContent" id="selectContent"><label for="selectContent"></label></td>
											<td id="userMail">${ r.userId }</td>
											<td id="userNickname">${ r.userNickname }</td>
											<td id="ContentName">
												<c:choose>
													<c:when test="${ not empty r.movieTitle }">
														${ r.movieTitle }
													</c:when>
													<c:otherwise>
														${ r.tvName }
													</c:otherwise>
												</c:choose>
											</td>
											<td id="reviewStar">★ ${  r.reviewStar }</td>
											<td id="review_content">${ r.reviewContent }</td>
										</tr>
									</c:forEach>
									<tr><th colspan="6" style="text-align: right;"><button type="button" class="btn btn-danger">삭제</button></th></tr>
								</tbody>
							</table>
		
							<br><br>                                        
		
							<div id="pagingArea">
								<c:choose>
									<c:when test="${ pi.currentPage eq 1 }">
										<button type="button" onclick="location.href='#';" disabled>«</button>
									</c:when>
									<c:otherwise>
										<button type="button" onclick="location.href='commentList.ad?cpage=${ pi.currentPage - 1}';">«</button>
									</c:otherwise>
								</c:choose>
								
								<c:forEach var="p" begin="${ pi.startPage }" end="${ pi.endPage }" step="1">
									<button type="button" onclick="location.href='commentList.ad?cpage=${ p }';">${ p }</button>
								</c:forEach>
								
								<c:choose>
									<c:when test="${ pi.currentPage eq pi.maxPage }">
										<button type="button" onclick="location.href='#';" disabled>»</button>
									</c:when>
									<c:otherwise>
										<button type="button" onclick="location.href='commentList.ad?cpage=${ pi.currentPage + 1}';">${ p }</button>
									</c:otherwise>
								</c:choose>
								</div>
								</c:when>
								<c:when test="${ not empty searchList }">
									<table id="commentTable">
								<thead>
									<tr id="comment_head" class="line">
										<th class="comment_head1" width="5%">선택</th>
										<th class="comment_head1" width="15%;">유저 이메일</th>
										<th class="comment_head1" width="10%;">유저 닉네임</th>
										<th class="comment_head1" width="20%;">콘텐츠명</th>
										<th class="comment_head1" width="10%;">별점</th>
										<th class="comment_head1" width="40%">코멘트 내용</th>
									</tr>
								</thead>
								<tbody>
									<c:forEach var="r" items="${ searchList }">
										<tr class="personalComment">
											<td><input type="checkbox" name="selectContent" id="selectContent"><label for="selectContent"></label></td>
											<td id="userMail">${ r.userId }</td>
											<td id="userNickname">${ r.userNickname }</td>
											<td id="ContentName">
												<c:choose>
													<c:when test="${ not empty r.movieTitle }">
														${ r.movieTitle }
													</c:when>
													<c:otherwise>
														${ r.tvName }
													</c:otherwise>
												</c:choose>
											</td>
											<td id="reviewStar">★ ${  r.reviewStar }</td>
											<td id="review_content">${ r.reviewContent }</td>
										</tr>
									</c:forEach>
									<tr><th colspan="6" style="text-align: right;"><button type="button" class="btn btn-danger">삭제</button></th></tr>
								</tbody>
							</table>
		
							<br><br>                                        
		
							<div id="pagingArea">
								<c:choose>
									<c:when test="${ pi.currentPage eq 1 }">
										<button type="button" onclick="location.href='#';" disabled>«</button>
									</c:when>
									<c:otherwise>
										<button type="button" onclick="location.href='commentList.ad?cpage=${ pi.currentPage - 1}';">«</button>
									</c:otherwise>
								</c:choose>
								
								<c:forEach var="p" begin="${ pi.startPage }" end="${ pi.endPage }" step="1">
									<button type="button" onclick="location.href='commentList.ad?cpage=${ p }';">${ p }</button>
								</c:forEach>
								
								<c:choose>
									<c:when test="${ pi.currentPage eq pi.maxPage }">
										<button type="button" onclick="location.href='#';" disabled>»</button>
									</c:when>
									<c:otherwise>
										<button type="button" onclick="location.href='commentList.ad?cpage=${ pi.currentPage + 1}';">${ p }</button>
									</c:otherwise>
								</c:choose>
								</div>
							</c:when>
							<c:otherwise>
								조회된 코멘트가 없습니다.
							</c:otherwise>
						</c:choose>

				</div> <!-- commentListAll 영역 끝 -->

			</div> <!-- 본인 구현 영역 끝 -->

        </div> <!-- 컨텐츠 영역 끝 -->

		<!-- 푸터 영역 -->
        <jsp:include page="../common/footer.jsp" />

    </div> <!-- 전체 영역 끝 -->
    
</body>
</html>

 

💻 adminCommentView.css

👉🏻 검색 form을 감싸고 있는 div id값 변경으로 수정해 줌

/* select+검색창+버튼 */
#selectCommentInfo {
    display : inline-block;
    width : 90%;
}

 

💻 ReviewController

	/**
	 * 관리자 페이지 코멘트 관리에서 검색 시 사용할 페이징 바, 검색 시 키워드에 해당하는 코멘트를 조회 - 작성자 : 수빈
	 * @param currentPage
	 * @param condition
	 * @param keyword
	 * @param model
	 * @return
	 */
	@RequestMapping(value="searchComment.ad")
	public String searchAdminCommentList(@RequestParam(value="cpage", defaultValue="1") int currentPage, String condition, String keyword, Model model) {
		
		// System.out.println("condition: " + condition);
		// System.out.println("keyword: " + keyword);
		
		HashMap<String, String> map = new HashMap<>();
		map.put("condition", condition);
		map.put("keyword", keyword);
		
		int searchCount = reviewService.searchAdminCommentListCount(map);
		
		int pageLimit = 10;
		int boardLimit = 10;
		
		PageInfo pi = Pagination.getPageInfo(searchCount, currentPage, pageLimit, boardLimit);
		
		ArrayList<Review> searchList = reviewService.searchAdminCommentList(map, pi);
		
		model.addAttribute("pi", pi);
		model.addAttribute("searchList", searchList);
		
		 // 검색한 조건과 검색어 유지를 위해 다시 jsp로 보내 줌
		model.addAttribute("condition", condition);
		model.addAttribute("keyword", keyword);
		
		return "admin/adminCommentView";
		
	}

 

💻 ReviewService

	/**
	 * 관리자 페이지 코멘트 관리에서 검색을 위한 페이징바(select)
	 * @return
	 */
	int searchAdminCommentListCount(HashMap map);
	
	
	/**
	 * 관리자 페이지 코멘트 관리에서 검색된 코멘트 조회 (select)
	 * @return
	 */
	ArrayList<Review> searchAdminCommentList(HashMap map, PageInfo pi);

 

💻 ReviewServiceImpl

	/**
	 * 관리자 페이지 코멘트 관리에서 검색을 위한 페이징바(select) - 작성자: 수빈
	 * @return
	 */
	@Override
	public int searchAdminCommentListCount(HashMap map) {
		return reviewDao.searchAdminCommentListCount(sqlSession, map);
	}

	/**
	 * 관리자 페이지 코멘트 관리에서 검색된 코멘트 조회 (select) - 작성자: 수빈
	 * @return
	 */
	@Override
	public ArrayList<Review> searchAdminCommentList(HashMap map, PageInfo pi) {
		return reviewDao.searchAdminCommentList(sqlSession, map, pi);
	}

 

💻 ReviewDao

	public int searchAdminCommentListCount(SqlSessionTemplate sqlSession, HashMap map) {
		
		return sqlSession.selectOne("reviewMapper.searchAdminCommentListCount", map);
	}

	public ArrayList<Review> searchAdminCommentList(SqlSessionTemplate sqlSession, HashMap map, PageInfo pi) {
		
		int limit = pi.getBoardLimit();
		int offset = (pi.getCurrentPage() -1) * limit;
		
		RowBounds rowBounds = new RowBounds(offset, limit);
		
		return (ArrayList)sqlSession.selectList("reviewMapper.searchAdminCommentList", map, rowBounds);
	}

 

💻 review-mapper.xml

	<select id="searchAdminCommentListCount" resultType="_int">
		SELECT COUNT(*)
		FROM REVIEW R
		LEFT JOIN MEMBER MB USING(USER_NO)
        LEFT JOIN MOVIE MV ON(R.MOVIE_ID = MV.MOVIE_ID)
		LEFT JOIN TV ON (R.MOVIE_ID = TV.TV_ID)
		WHERE  REVIEW_STATUS = 'Y'
		<if test="condition == 'contentsTitle'">
			AND TV.NAME LIKE '%' || #{keyword} || '%'
	        OR MV.TITLE LIKE '%' || #{keyword} || '%'
        </if>
        <if test="condition == 'userId'">
        	AND MB.USER_ID LIKE '%' || #{keyword} || '%'
        </if>
        <if test="condition == 'userNickname'">
        	AND MB.USER_NICKNAME LIKE '%' || #{keyword} || '%'
        </if>
	</select>
	
	<select id="searchAdminCommentList" resultMap="reviewResultSet">
		SELECT REVIEW_NO
		     , MB.USER_ID
		     , MB.USER_NICKNAME
		     , MV.TITLE AS "MOVIE_TITLE"
		     , TV.NAME AS "TV_NAME"
		     , TO_CHAR(REVIEW_STAR, 'FM0.0') AS "REVIEW_STAR"
		     , REVIEW_CONTENT
		FROM REVIEW R
		LEFT JOIN MEMBER MB USING(USER_NO)
		LEFT JOIN MOVIE MV ON(R.MOVIE_ID = MV.MOVIE_ID)
		LEFT JOIN TV ON (R.MOVIE_ID = TV.TV_ID)
		WHERE REVIEW_STATUS = 'Y'
		<if test="condition == 'contentsTitle'">
			AND TV.NAME LIKE '%' || #{keyword} || '%'
	        OR MV.TITLE LIKE '%' || #{keyword} || '%'
        </if>
        <if test="condition == 'userId'">
        	AND MB.USER_ID LIKE '%' || #{keyword} || '%'
        </if>
        <if test="condition == 'userNickname'">
        	AND MB.USER_NICKNAME LIKE '%' || #{keyword} || '%'
        </if>
		ORDER BY REVIEW_REGISTER_DATE DESC
	</select>