프로그래밍/Java_Spring 게시판 만들기

Spring 게시판 만들기 (5) - 관리자 기능 구현

개발계발게발 2021. 9. 3. 17:09
반응형

관리자 기능 구현

 

member.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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="../css/wrapper.css">
<link rel="stylesheet" href="../css/index.css">
<style type="text/css">
#container{width:1200px; margin:0 auto;padding-top:20px;font-size:15px;font-family:'맑은 고딕';}
	.title{overflow:hidden;text-align:center;border-bottom:1px solid #ccc;height:35px;line-height:35px;}
		.title li{text-align: center;}
		.title li:first-child{width:6%;}
		.title li:nth-child(2){width:10%;}
		.title li:nth-child(3){width:8%;}
		.title li:nth-child(4){width:25%;}
		.title li:nth-child(5){width:20%;}
		.title li:nth-child(6){width:15%;}
		.title li:nth-child(7){width:8%;}
		.title li:last-child{width:8%;}
	#subTitle{margin-bottom:5px;font-weight:600;background-color:#F7F7F7;border-top:2px solid #444;color:#444;}
	#main {transition:0.3s all;cursor:pointer; text-align: center;}
	#main:hover{background-color:#EBF5FB;}
	#mainTitle{font-size:14px;font-family:'맑은 고딕';}
	#modify{float: right; margin-right: 10px; margin-top: 10px;}
#bbottom{width:990px;min-height:500px;padding:10px;margin-bottom:0px;text-align:left;}

</style>
<script type="text/javascript">
function gradeModify(sm_no, sm_grade, code){
	if(code == 'up'){
		if(confirm("해당 회원 등급을 올리시겠습니까?")){
			location.href="./gradeModify?code=" + code + "&sm_no=" + sm_no + "&sm_grade=" + sm_grade;
		}
	} else if(code == 'down'){
		if(confirm("해당 회원 등급을 내리시겠습니까?")){
			location.href="./gradeModify?code=" + code + "&sm_no=" + sm_no + "&sm_grade=" + sm_grade;
		}
	}
}
function userDelete(sm_no){	
	if(confirm("해당 회원을 삭제 하시겠습니까?")){
		location.href="./userDelete?sm_no=" + sm_no;
	}
	
}
</script>
</head>
<body>
<div id="wrapper">
	<header>
		<jsp:include page="../header.jsp"/>
		<c:import url="./adminMenu.jsp"/>
	</header>
	<main id="container">
		<ul id="subTitle" class="title">
			<li>번호</li>
			<li>아이디</li>
			<li>이름</li>
			<li>이메일</li>
			<li>가입날짜</li>
			<li>생년월일</li>
			<li>등급</li>			
			<li>삭제</li>			
		</ul>
		<c:choose>
			<c:when test="${fn:length(memberList) gt 0 }">
				<c:forEach items="${memberList }" var="l">
					<ul id="main" class="title" 
					<c:if test="${l.sm_grade lt 5 }">style="background-color: #c0c0c0"</c:if>										
					>
						<li>${l.sm_no }</li>
						<li>${l.sm_id }</li>
						<li>${l.sm_name }</li>
						<li>${l.sm_email }</li>						
						<li>${l.sm_joindate }</li>
						<li>${l.sm_birthdate }</li>
						<li><button <c:if test="${l.sm_grade eq 0}">disabled="disabled</c:if> onclick="gradeModify(${l.sm_no}, ${l.sm_grade}, 'down')">◀</button>
						${l.sm_grade }
						<button <c:if test="${l.sm_grade eq 9}">disabled="disabled</c:if> onclick="gradeModify(${l.sm_no}, ${l.sm_grade }, 'up')">▶</button></li>
						<li><img alt="delete" src="../img/user_delete.png" onclick="userDelete(${l.sm_no})"> </li>
					</ul>
				</c:forEach>
			</c:when>
			<c:otherwise>
				출력할 내용이 없습니다.<br/><br/>
			</c:otherwise>
		</c:choose>
	</main>
	<footer></footer>
</div>
</body>
</html>

 

 

AdminController.java

package com.knowhoon.web;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.knowhoon.web.admin.AdminDTO;
import com.knowhoon.web.admin.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value = "/category", method = RequestMethod.GET)
	public ModelAndView category(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/admin/category");
		List<HashMap<String, Object>> cate = adminService.getCategory();
		
		mv.addObject("category", cate);
	    return mv;
	}
	@PostMapping("/category")
	public String categoryInsert(HttpServletRequest request) {
		String categoryName = request.getParameter("categoryName");
		System.out.println(categoryName);
		int result = adminService.categoryInsert(categoryName);
		System.out.println("저장 결과 : " + result);
		return "redirect:/admin/category";
	}
	@GetMapping("/categoryUpdate")
	public ModelAndView categoryUpdate(HttpServletRequest request) {
		int sca_no = Integer.parseInt(request.getParameter("sca_no"));
		System.out.println("들어온 sca_no : " + sca_no);
		ModelAndView mv = new ModelAndView("/admin/categoryUpdate");
		
		HashMap<String, Object> cate = adminService.getCategory(sca_no);
		System.out.println(cate);
		mv.addObject("cate", cate);
		return mv;
	}
	@PostMapping("/categoryUpdate")
	public String categoryUpdate2(HttpServletRequest request) {
		String categoryName = request.getParameter("sca_category");
		String categoryNo = request.getParameter("sca_no");
		HashMap<String, Object> cate = new HashMap<String, Object>();
		cate.put("sca_category", categoryName);
		cate.put("sca_no", categoryNo);
		adminService.categoryUpdate(cate);
		return "redirect:/admin/category";
	}
	
	@GetMapping("/member")
	public ModelAndView member(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();//페이지없이생성
		mv.setViewName("admin/member");
		if(request.getSession().getAttribute("sm_grade") != null && (int) request.getSession().getAttribute("sm_grade") == 9) {
			mv.addObject("memberList", adminService.memberList());
		}else {
			System.out.println("잘못된 접근");			
		}
		return mv;		
	}
	@GetMapping("/gradeModify")
	public String gradeModify(AdminDTO adminDTO, HttpServletRequest request) {
		int grade = Integer.parseInt(request.getParameter("sm_grade"));
		if(request.getParameter("code").equals("up")) {
			grade++;					
		}else if(request.getParameter("code").equals("down")) {
			grade--;
		}
		adminDTO.setSm_grade(grade);
		int result = adminService.gradeModify(adminDTO);
		if(result == 1) {
			return "redirect:/admin/member";			
		}else {
			return "redirect:/admin/error?gradeModifyError";
		}		
	}
	@GetMapping("/userDelete")
	public String userDelete(@RequestParam("sm_no") Integer sm_no) {
		int result = adminService.userDelete(sm_no);
		if(result == 1) {
			return "redirect:/admin/member";
		}else {
			return "redirect:/admin/error?userDeleteError";
		}		
	}	
}

 

 

AdminService.java

package com.knowhoon.web.admin;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
	@Autowired
	private AdminDAO adminDAO;

	public List<HashMap<String, Object>> getCategory() {
		return adminDAO.getCategory();
	}

	public int categoryInsert(String categoryName) {
		return adminDAO.categoryInsert(categoryName);
	}

	public HashMap<String, Object> getCategory(int sca_no) {
		return adminDAO.getCategory(sca_no);
	}

	public Object categoryUpdate(HashMap<String, Object> cate) {
		return adminDAO.categoryUpdate(cate);
		
	}

	public List<AdminDTO> memberList() {
		return adminDAO.memberList();
	}
	public int gradeModify(AdminDTO adminDTO) {
		return adminDAO.gradeModify(adminDTO);
	}

	public int userDelete(Integer sm_no) {
		return adminDAO.userDelete(sm_no);
	}
}

 

 

AdminDAO.java

package com.knowhoon.web.admin;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AdminDAO {
	@Autowired
	private SqlSession sqlSession;
	
	public List<HashMap<String, Object>> getCategory(){
		return sqlSession.selectList("admin.getCategory");
	}

	public int categoryInsert(String categoryName) {
		return sqlSession.insert("admin.categoryInsert", categoryName);
	}

	public HashMap<String, Object> getCategory(int sca_no) {
		return sqlSession.selectOne("admin.getCategory", sca_no);
	}

	public Object categoryUpdate(HashMap<String, Object> cate) {
		return sqlSession.update("admin.categoryUpdate", cate);
	}

	public List<AdminDTO> memberList() {
		return sqlSession.selectList("admin.memberList");
	}

	public int gradeModify(AdminDTO adminDTO) {
		return sqlSession.update("admin.gradeModify", adminDTO);
	}

	public int userDelete(Integer sm_no) {
		return sqlSession.update("admin.userDelete", sm_no);
	}	
}

 

 

adminMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="admin">		
	<select id="getCategory" resultType="hashMap">
		SELECT sca_no, sca_category, sca_date FROM scategory 
		<if test="sca_no != null">
			WHERE sca_no=#{sca_no}
		</if>
	</select>
	
	<insert id="categoryInsert">
		INSERT INTO scategory (sca_category) VALUES (#{categoryName})
	</insert>
	
	<update id="categoryUpdate" parameterType="hashMap">
		UPDATE scategory SET sca_category= #{sca_category} WHERE sca_no=#{sca_no}
	</update>
	
	<select id="memberList" resultType="AdminDTO" >
		SELECT sm_no, sm_name, sm_id, sm_pw, sm_joindate, sm_birthdate, sm_email, sm_grade FROM smember	
	</select>
	
	<update id="gradeModify" parameterType="AdminDTO">
		UPDATE smember SET sm_grade=#{sm_grade} WHERE sm_no=#{sm_no}
	</update>
	
	<update id="userDelete" parameterType="Integer">
		UPDATE smember SET sm_grade=0 WHERE sm_no=#{sm_no}
	</update>	
</mapper>

 

 

회원 정보 확인 및 등급 조절. 삭제 기능(등급0)

반응형