스프링부트+jsp로 배달사이트 만들기-26 관리자페이지

2021. 12. 12. 01:18카테고리 없음

관리자 계정으로 접속했을 때 관리자페이지로 이동 가능한 버튼을 만듭니다

 

 

reset.css 상단에 추가합니다

body {
	overflow-x: hidden;
}

 

header.jsp와 css에 메뉴버튼을 추가합니다

header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
</head>
<body >
	<!-- <body> -->
	<header>
		<div id="header">
			<a href="/"><img src="/img/baemin.jpg" alt="이미지"> </a>
			<c:set var="ROLE" value="${SPRING_SECURITY_CONTEXT.authentication.principal.user.role }" />
			<c:if test="${ROLE == 'ROLE_ADMIN' }">
			<c:set var="uri" value="${requestScope['javax.servlet.forward.request_uri']}" />
			
			 	
				<div class="menu_tab_box">
					<div class="menu_tab">
						<span> </span>
						<span> </span>
						<span> </span>
					</div>
				</div>
				
				<div id="menu">
					<div class="box">
						<c:choose>
							<c:when test="${fn:contains(uri, '/admin/management') }">
								<div><a href="/admin/myStore">운영중인가게</a></div>
								<div><a href="/admin/main">주문접수</a></div>
								<div><a href="/admin/storeManagement">매장 관리</a></div>
								<div><a href="/admin/salesManagement">매출 확인</a></div>
							</c:when>
							
							<c:otherwise>
								<div><a href="/admin/myStore">운영중인가게</a></div>
								
							</c:otherwise>
							
						</c:choose>
						
						<div><a href="/">홈으로</a></div>
					</div>
				</div>
				<div id="menu_bg"></div>
				
				 <script>
				 	console
				 
					 $(".menu_tab").click(function(){
						 const menuTab = $(this);
						 const menu = $("#menu");
						 const menuBg = $("#menu_bg");
						 
						 function hide(){
							 menuTab.removeClass("active");
							 menu.removeClass("active");
							 menuBg.hide();
						 }
						 
						 function show(){
							 menuTab.addClass("active");
							 menu.addClass("active");
							 menuBg.show();
						 }
						 
						 if($(this).hasClass("active")) {
							 hide();
						 } else {
							 show();
						 }
						 
						 if(menuBg.css("display") != "none") {
							 menuBg.click(function(){
								hide();
							 })
						 }
						 
					});
				</script>
			</c:if>
		</div>
	</header>
	<!-- 헤더 -->

계정의 권한이 ROLE_ADMIN일 경우에만 버튼이 보이게 했습니다

 

header.css

header {
	width: 100%;
	height: 80px;
	background: #2AC1BC;
	position: relative;
}

#header {
	width: 1200px;
	margin: 0 auto;
}

header img {
	width: 160px;
}

.menu_tab_box {
	position: absolute;
	top: 50%;
	right: 20px;
	transform: translateY(-50%);
	z-index: 10;
}


.menu_tab, 
.menu_tab span {
	display: inline-block;
	transition: all 0.4s;
	box-sizing: border-box;
}

.menu_tab {
	position: relative;
	width: 24px;
	height: 18px;
	cursor: pointer;
/* 	display: none; */
}

.menu_tab span {
	position: absolute;
	left: 0;
	width: 100%;
	height: 2px;
	background-color: #fff;
	border-radius: 4px;
}

.menu_tab span:nth-of-type(1) {
	top: 0;
}

.menu_tab span:nth-of-type(2) {
	top: 50%;
	transform: translateY(-50%);
}

.menu_tab span:nth-of-type(3) {
	bottom: 0;
}

.menu_tab.active span:nth-of-type(1) {
	transform: translateY(8px) rotate(-45deg);
}

.menu_tab.active span:nth-of-type(2) {
	opacity: 0;
}

.menu_tab.active span:nth-of-type(3) {
	transform: translateY(-8px) rotate(45deg);
}

header #menu {
	background: rgba(0, 0, 0, 0.9);
	width: 400px;
	height: 100vh;
	position: fixed;
	right: -400px; 
	top: 0;
	z-index: 9;
	transition: all 0.4s;
}

header #menu.active {
	right: 0;
}

header #menu .box {
	margin-top: 80px;
}

header #menu .box a {
	color: #fff;
	display: block;
	width: 100%;
	height: 50px;
	line-height: 50px;
	font-size: 2rem;
	padding-left: 25px;
}

header #menu .box a:hover {
	background: #eee;
	color: #000;
}

header #menu_bg {
	background: rgba(0, 0, 0, 0.1);
	width: 100vw;
	height: 100vh;
	position: fixed;
	z-index: 8;
	top: 0;
	right: 0;
	display: none;
}


@media ( max-width :1250px) {
	#header {
		width: 99%;
	}
}

@media ( max-width :767px) {
	header img {
		width: 120px;
		position: relative;
		left: -15px;
	}
	header {
		height: 60px;
	}
	
	header #menu {
		width: 300px; 
		right: -300px;
	}
	
	header #menu .box {
		margin-top: 60px;
	}
}

 

db에서 계정의 권한을 ROLE_ADMIN으로 변경합니다

update bm_user set role = 'ROLE_ADMIN' where username = 'admin';
commit;

 

 

관리자페이지가 아닐 때

관리자페이지 일때

 

 

 

먼저 DB에 내가 관리 할 가게 테이블을 생성합니다

CREATE TABLE BM_MY_STORE (
    USER_ID NUMBER,
    STORE_ID NUMBER
);

ALTER TABLE BM_MY_STORE
ADD CONSTRAINT MY_STORE_U
FOREIGN KEY (USER_ID)
REFERENCES BM_USER(ID)
ON DELETE CASCADE;


ALTER TABLE BM_MY_STORE
ADD CONSTRAINT MY_STORE_S
FOREIGN KEY (STORE_ID)
REFERENCES BM_STORE(ID)
ON DELETE CASCADE;

 

관리하고싶은 가게번호와 계정아이디로 등록합니다

INSERT INTO BM_MY_STORE (USER_ID, STORE_ID) VALUES (13, 7);
COMMIT;

 

운영중인가게 페이지를 추가합니다

myStore.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/include/link.jsp" %>
	<link rel="stylesheet" href="/css/store/store-li.css">

<style>
main h1 {text-align: center; margin: 20px; }

.box .store .is_open {display: none; }

</style>

<%@ include file="/WEB-INF/view/include/header.jsp" %>


<main>
	<h1>운영중인가게</h1>
	<div class="box">
		<ul class="store">
	    	<c:forEach items="${storeList }" var="storeList">
	        	<%@ include file="/WEB-INF/view/store/store-li.jsp" %>
	        </c:forEach>
	    </ul>
	</div>
</main>


</body>
</html>

 

AdminController를 생성합니다

@Controller
public class AdminController {
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private StoreService storeService;
	
	@GetMapping("/admin/myStore") 
	public String myStore(@AuthenticationPrincipal LoginService user, Model model) {
		long userId = user.getUser().getId(); 
		List<Store> storeList = adminService.myStore(userId);
		
		model.addAttribute("storeList", storeList);
		return "admin/myStore";
	}
	
	
	@GetMapping("/admin/management/detail/{id}") 
	public String detail(@PathVariable long id, @AuthenticationPrincipal LoginService user, Model model) {
		long userId = user.getUser().getId();
		StoreDetail storeDetail = storeService.storeDetail(id, userId);
		model.addAttribute("store", storeDetail);
		model.addAttribute("adminPage", true);
		
		return "admin/detail";
	}

}

StoreController를 복사해 약간만 수정햐였습니다

 

AdminService

public interface AdminService {

	List<Store> myStore(long userId);

}

 

AdminServiceImp

@Service
public class AdminServiceImp implements AdminService {

	@Autowired
	private AdminDAO adminDAO;
	
	
	@Override
	public List<Store> myStore(long userId) {
		return adminDAO.myStore(userId);
	}

	
}

 

AdminDAO

List<Store> myStore(long userId);

 

AdminDAOImp

@Override
public List<Store> myStore(long userId) {
	return sql.selectList("admin.myStore", userId);
}

 

AdminMapper

	<select id="myStore" resultType="Store">
	WITH R_COUNT AS (
	    SELECT STORE_ID
                ,ROUND(AVG(SCORE), 1) SCORE
                ,COUNT(REVIEW_CONTENT) REVIEW_COUNT
                ,COUNT(BOSS_COMMENT) BOSS_COMMENT_COUNT 
        FROM    BM_REVIEW 
        GROUP BY STORE_ID
	),
	STORE AS (
	    SELECT  S.*,
	            T.*
	    FROM        BM_STORE S
	    LEFT JOIN   R_COUNT T
	    ON          S.ID = T.STORE_ID
        LEFT JOIN   BM_MY_STORE M
        ON          S.ID = M.STORE_ID
        WHERE       M.USER_ID = #{user_id }
	)
	SELECT * FROM STORE
	</select>