Skip to content

Commit

Permalink
[FEATURE] 관리자페이지 초안 작성 (#41)
Browse files Browse the repository at this point in the history
* admin_list

* admin_list , detail in Controller -ing

* figuring Request

* feat : temp commit

* figuring JPA Entity Admin

* figuring list.html with thymeleaf

* figuring list controller & list service

* feat : temp commit

* figuring detail controller , service & html with thymeleaf

* figuring Approve

* modify for exception and error

* finished approve , detail and list figuring

* dev

* feat : 테이블 명 수정에 따른 쿼리 변경

* feat : AdminBackEnd완료

* Add Modified admin's BackEnd file

approvePicture 메서드에 isSessionUserAdmin 주석처리

---------

Co-authored-by: donsonioc2010 <whddnjs822@gmail.com>
  • Loading branch information
Romangduck and donsonioc2010 committed Sep 21, 2023
1 parent 061bd83 commit 8bcfa93
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package picasso.server.api.admin.controller;

import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import picasso.server.api.admin.exception.NotAdminUserException;
import picasso.server.api.admin.service.AdminService;
import picasso.server.domain.domains.member.entity.User;
import picasso.server.domain.domains.member.type.UserRole;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController {

private final AdminService adminService;

@PostMapping("/approve/{pictureId}")
public String approvePicture(@PathVariable Long pictureId, HttpSession session) {
// isSessionUserAdmin(session);
adminService.approvePicture(pictureId);
return "redirect:/admin/list";
}

/**
* 관리자 페이지 미승인된 게시물 조회 Page
*
* @param model
* @param session
* @return
*/
@GetMapping("/list")
public String list(Model model, HttpSession session) {
// isSessionUserAdmin(session);
model.addAttribute("pictures", adminService.findAll());
return "admin/list";
}

/**
* 세부 게시물 조회 페이지
*
* @param pictureId
* @param model
* @param session
* @return
*/
@GetMapping("/detail/{pictureId}")
public String detail(@PathVariable Long pictureId, Model model, HttpSession session) {
// isSessionUserAdmin(session);
model.addAttribute(
"picture",
adminService.getBeforeApproveStatusPictureDetailById(pictureId)
);
return "admin/detail";
}

private void isSessionUserAdmin(HttpSession session) {
User user = (User) session.getAttribute("loginUser");
if (user == null || !user.getUserRole().equals(UserRole.ADMIN)) {
log.error("ADMIN Auth Not Enough");
throw NotAdminUserException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package picasso.server.api.admin.exception;

import picasso.server.common.exception.BaseException;

import static picasso.server.common.exception.GlobalException.PICTURE_STATUS_AFTER_APPROVE;

/**
* 이미 관리자가 승인한 게시물인 경우 발생되는 Exception
*/
public class AlreadyChangePictureException extends BaseException {
public static final BaseException EXCEPTION = new AlreadyChangePictureException();
private AlreadyChangePictureException() {
super(PICTURE_STATUS_AFTER_APPROVE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package picasso.server.api.admin.exception;

import picasso.server.common.exception.BaseException;

import static picasso.server.common.exception.GlobalException.NOT_ADMIN;

public class NotAdminUserException extends BaseException {
public static final BaseException EXCEPTION = new NotAdminUserException();

public NotAdminUserException() {
super(NOT_ADMIN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package picasso.server.api.admin.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import picasso.server.api.admin.exception.AlreadyChangePictureException;
import picasso.server.common.exception.NotFoundException;
import picasso.server.domain.domains.items.Picture;
import picasso.server.domain.domains.repository.PictureRepository;

import java.util.List;
import java.util.Optional;

import static picasso.server.domain.domains.items.PictureStatus.AFTER_APPROVE;
import static picasso.server.domain.domains.items.PictureStatus.BEFORE_APPROVE;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class AdminService {
private final PictureRepository pictureRepository;

// TODO : 추후 Pagenation으로의 수정 필요함.
@Transactional(readOnly = true)
public List<Picture> findAll() {
return pictureRepository.findAll();
}


public Picture getBeforeApproveStatusPictureDetailById(Long id) {
Optional<Picture> picture = pictureRepository
.findByPictureIdAndPictureStatus(id, BEFORE_APPROVE);
if (picture.isPresent()) {
return picture.get();
}
throw NotFoundException.EXCEPTION;
}


/**
* 관리자 승인전의 게시물을 관리자 승인 상태로 변경하는 기능
* @param pictureId
*/
public void approvePicture(Long pictureId) {
Optional<Picture> optionalPicture = pictureRepository.findById(pictureId);
if (optionalPicture.isPresent()) {
Picture picture = optionalPicture.get();
if (picture.getPictureStatus().equals(BEFORE_APPROVE)) {
picture.setPictureStatus(AFTER_APPROVE);
pictureRepository.save(picture);
log.info("ApprovePicture Success >>> Picture Id : {}, Title : {}",
picture.getPictureId(), picture.getPictureName());
return;
}
log.warn("ApprovePicture Failure >>> Picture Id : {}, Title : {}",
picture.getPictureId(), picture.getPictureName());
throw AlreadyChangePictureException.EXCEPTION;
}
log.error("ApprovePicture Failure : Fail Reason Is {} Id is Empty", pictureId);
throw NotFoundException.EXCEPTION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public String createForm(Model model) {
@PostMapping
public String add(PictureDTO dto, MultipartFile imageFile, Model model) {
Picture picture = new Picture();
picture.setPicture_id(dto.getPicture_id());
picture.setPictureId(dto.getPictureId());
picture.setPictureName(dto.getPictureName());
picture.setPainterName(dto.getPainterName());
picture.setSize(dto.getSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import picasso.server.common.dto.ErrorDetail;
import picasso.server.common.exception.BaseException;
import picasso.server.common.exception.GlobalException;

//TODO : 현재 `GlobalRestControllerExceptionHandler`와 동일한 코드, 추후 수정 필요
@Slf4j
@RequiredArgsConstructor
@RestControllerAdvice
@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ExceptionHandler(BaseException.class)
protected ResponseEntity<ErrorDetail> baseExceptionHandle(BaseException e, HttpServletRequest request) {
protected String baseExceptionHandle(BaseException e, HttpServletRequest request) {
ErrorDetail errorDetail = e.getErrorCode().getErrorDetail();
log.warn("ExceptionName >>> {}, ErrorCode >>> {}, ExceptionReason >>> {}",
e.getClass(), errorDetail.getStatusCode(), errorDetail.getReason());

return ResponseEntity.status(errorDetail.getStatusCode()).body(errorDetail);
//return ResponseEntity.status(errorDetail.getStatusCode()).body(errorDetail);
return "redirect:/error";
}

@ExceptionHandler(MethodArgumentNotValidException.class)
Expand Down
56 changes: 56 additions & 0 deletions Api/src/main/resources/templates/admin/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Picasso - Picture Detail</title>
</head>
<body>
<h1>Picture Detail</h1>

<table>
<tr>
<th>ID:</th>
<td th:text="${picture.pictureId}"></td>
</tr>
<tr>
<th>그림 URL:</th>
<td th:text="${picture.imgUrl}"></td>
</tr>
<tr>
<th>그림 이름:</th>
<td th:text="${picture.pictureName}"></td>
</tr>
<tr>
<th>화가 이름:</th>
<td th:text="${picture.painterName}"></td>
</tr>
<tr>
<th>그림 설명:</th>
<td th:text="${picture.details}"></td>
</tr>
<tr>
<th>시작 가격:</th>
<td th:text="${picture.startingPrice}"></td>
</tr>
<tr>
<th>최소 입찰 단위:</th>
<td th:text="${picture.incrementAmount}"></td>
</tr>
<tr>
<th>그림 사이즈:</th>
<td th:text="${picture.size}"></td>
</tr>
<tr>
<th>생성일:</th>
<td th:text="${picture.bidStartDate}"></td>
</tr>

<td>
<form th:action="@{'/admin/approve/' + ${picture.pictureId}}" method="post" >
<button type="submit">Approve</button>
</form>
</td>

</table>

</body>
</html>
39 changes: 39 additions & 0 deletions Api/src/main/resources/templates/admin/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Picasso - Approve List</title>
</head>
<body>
<h1>Admin Approve List</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>그림명</th>
<th>시작가</th>
<th>생성일</th>
<th>승인 상태</th>
<th>승인 버튼</th>
</tr>
</thead>
<tbody>
<tr th:each="picture : ${pictures}" th:if="${#strings.equals(picture.pictureStatus,'BEFORE_APPROVE')}">
<td>
<a th:href="@{'/admin/detail/' + ${picture.pictureId}}"><span th:text="${picture.pictureId}"></span></a>
</td>
<td>
<a th:href="@{'/admin/detail/' + ${picture.pictureId}}"><span th:text="${picture.pictureName}"></span></a>
</td>
<td th:text="${picture.startingPrice}"></td>
<td th:text="${picture.bidStartDate}"></td>
<td th:text="${picture.pictureStatus}"></td>
<td>
<form th:action="@{'/admin/approve/' + ${picture.pictureId}}" method="post" >
<button type="submit">Approve</button>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import picasso.server.common.dto.ErrorDetail;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

@Getter
@AllArgsConstructor
Expand All @@ -22,6 +23,9 @@ public enum GlobalException implements BaseErrorCode {
MAIL_SEND_ERROR(INTERNAL_SERVER_ERROR.value(), "메일 발송중 오류가 발생하였습니다"),
ALREADY_EXISTS_PAYMENT_HISTORY_ERROR(INTERNAL_SERVER_ERROR.value(), "이미 존재하는 결제 내역입니다"),
PAYMENT_FAILED_ERROR(INTERNAL_SERVER_ERROR.value(), "결제 실패 오류입니다."),
NOT_ADMIN(UNAUTHORIZED.value(), "관리자만 사용가능한 페이지입니다."),
NOT_FOUND_ERROR(NOT_FOUND.value(), "검색 결과가 존재하지 않습니다"),
PICTURE_STATUS_AFTER_APPROVE(INTERNAL_SERVER_ERROR.value(), "이미 관리자가 승인한 게시물 입니다.")
;

private final Integer statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package picasso.server.common.exception;

import static picasso.server.common.exception.GlobalException.NOT_FOUND_ERROR;

public class NotFoundException extends BaseException{
public static final BaseException EXCEPTION = new NotFoundException();

private NotFoundException() {
super(NOT_FOUND_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Getter @Setter
public class PictureDTO {
private Long picture_id; //그림 ID
private Long pictureId; //그림 ID
private String pictureName; //그림 이름
private String painterName; //화가 이름
private String details; //그림 설명
Expand All @@ -17,3 +17,9 @@ public class PictureDTO {
private LocalDate dateTime;
private String imgUrl; //그림 url
}






Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Picture {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long picture_id;
private Long pictureId;

@NotNull
private String imgUrl; //그림 url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import picasso.server.domain.domains.items.PictureStatus;

import java.util.List;
import java.util.Optional;

@Repository
public interface PictureRepository extends JpaRepository<Picture, Long>{
Expand All @@ -14,4 +15,5 @@ public interface PictureRepository extends JpaRepository<Picture, Long>{

List<Picture> findAllByPictureStatusOrderByBidStartDateAsc(PictureStatus status);

Optional<Picture> findByPictureIdAndPictureStatus(Long id, PictureStatus status);
}
Loading

0 comments on commit 8bcfa93

Please sign in to comment.