Skip to content

Commit

Permalink
fix: 어드민 히스토리 기능 수정 (#1131)
Browse files Browse the repository at this point in the history
* fix: 반환 날짜 형식 변경

* feat: 검색 정렬 옵션 추가

* fix: String 필드 Enum으로 변경

* style: 코드 포멧팅

* chore: 미사용 import 삭제

* style: 코드 포멧팅

* chore: 코드 블럭 처리

* chore: historys -> histories으로 변경

* chore: 정적 메소드 네이밍 변경

* style: 코드 개행

* style: 코드 개행

* fix: 로깅 어노테이션으로 변경

* Revert "fix: 로깅 어노테이션으로 변경"

This reverts commit 7c1ecb2.

* chore: 리뷰 반영

* style: 코드 포멧팅
  • Loading branch information
Soundbar91 authored Dec 13, 2024
1 parent 14d9d8e commit 885875d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.util.ContentCachingRequestWrapper;

import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import in.koreatech.koin.admin.history.model.AdminActivityHistory;
import in.koreatech.koin.admin.history.repository.AdminActivityHistoryRepository;
import in.koreatech.koin.admin.user.model.Admin;
Expand Down Expand Up @@ -58,7 +59,7 @@ private void excludeSpecificMethods() {
public Object logAdminActivity(ProceedingJoinPoint joinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
String requestURI = request.getRequestURI();
String requestMethod = request.getMethod();
HttpMethodType requestMethod = HttpMethodType.valueOf(request.getMethod());

ContentCachingRequestWrapper cachingRequest = (ContentCachingRequestWrapper)request;
String requestMessage = new String(cachingRequest.getContentAsByteArray());
Expand All @@ -72,9 +73,10 @@ public Object logAdminActivity(ProceedingJoinPoint joinPoint) throws Throwable {
.domainId(domainInfo.domainId())
.admin(admin)
.requestMethod(requestMethod)
.domainName(domainInfo.domainName())
.domainName(DomainType.valueOf(domainInfo.domainName()))
.requestMessage(requestMessage)
.build());
.build()
);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package in.koreatech.koin.admin.history.controller;

import static in.koreatech.koin.domain.user.model.UserType.ADMIN;
import static in.koreatech.koin.global.model.Criteria.Sort;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import in.koreatech.koin.admin.history.dto.AdminHistoryResponse;
import in.koreatech.koin.admin.history.dto.AdminHistorysResponse;
import in.koreatech.koin.admin.history.dto.AdminHistoriesResponse;
import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import in.koreatech.koin.global.auth.Auth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -19,6 +22,7 @@

@Tag(name = "(Admin) History: 기록", description = "관리자 기록 관련 API")
public interface HistoryApi {

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
Expand All @@ -29,13 +33,14 @@ public interface HistoryApi {
}
)
@Operation(summary = "히스토리 리스트 조회")
@GetMapping("/admin/historys")
ResponseEntity<AdminHistorysResponse> getHistorys(
@GetMapping("/admin/histories")
ResponseEntity<AdminHistoriesResponse> getHistories(
@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer limit,
@RequestParam(required = false) String requestMethod,
@RequestParam(required = false) String domainName,
@RequestParam(required = false) HttpMethodType requestMethod,
@RequestParam(required = false) DomainType domainName,
@RequestParam(required = false) Integer domainId,
@RequestParam(required = false) Sort sort,
@Auth(permit = {ADMIN}) Integer adminId
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package in.koreatech.koin.admin.history.controller;

import static in.koreatech.koin.domain.user.model.UserType.ADMIN;
import static in.koreatech.koin.global.model.Criteria.Sort;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -9,8 +10,10 @@
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.admin.history.dto.AdminHistoryResponse;
import in.koreatech.koin.admin.history.dto.AdminHistorysCondition;
import in.koreatech.koin.admin.history.dto.AdminHistorysResponse;
import in.koreatech.koin.admin.history.dto.AdminHistoriesCondition;
import in.koreatech.koin.admin.history.dto.AdminHistoriesResponse;
import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import in.koreatech.koin.admin.history.service.HistoryService;
import in.koreatech.koin.global.auth.Auth;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,19 +24,20 @@ public class HistoryController implements HistoryApi {

private final HistoryService historyService;

@GetMapping("/admin/historys")
public ResponseEntity<AdminHistorysResponse> getHistorys(
@GetMapping("/admin/histories")
public ResponseEntity<AdminHistoriesResponse> getHistories(
@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer limit,
@RequestParam(required = false) String requestMethod,
@RequestParam(required = false) String domainName,
@RequestParam(required = false) HttpMethodType requestMethod,
@RequestParam(required = false) DomainType domainName,
@RequestParam(required = false) Integer domainId,
@RequestParam(required = false) Sort sort,
@Auth(permit = {ADMIN}) Integer adminId
) {
AdminHistorysCondition adminHistorysCondition = new AdminHistorysCondition(page, limit, requestMethod,
domainName, domainId);
AdminHistorysResponse historys = historyService.getHistorys(adminHistorysCondition);
return ResponseEntity.ok(historys);
AdminHistoriesCondition adminHistoriesCondition = new AdminHistoriesCondition(page, limit, requestMethod,
domainName, domainId, sort);
AdminHistoriesResponse histories = historyService.getHistories(adminHistoriesCondition);
return ResponseEntity.ok(histories);
}

@GetMapping("/admin/history/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
package in.koreatech.koin.admin.history.dto;

import static com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import static in.koreatech.koin.global.model.Criteria.*;
import static in.koreatech.koin.global.model.Criteria.Sort.CREATED_AT_ASC;
import static in.koreatech.koin.global.model.Criteria.Sort.CREATED_AT_DESC;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;
import static org.springframework.data.domain.Sort.Direction;

import java.util.Objects;

import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.global.model.Criteria;
import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonNaming(value = SnakeCaseStrategy.class)
public record AdminHistorysCondition(
public record AdminHistoriesCondition(
@Schema(description = "페이지", example = "1", defaultValue = "1", requiredMode = NOT_REQUIRED)
Integer page,

@Schema(description = "페이지당 조회할 최대 개수", example = "10", defaultValue = "10", requiredMode = NOT_REQUIRED)
Integer limit,

@Schema(description = "HTTP 메소드", example = "POST", requiredMode = NOT_REQUIRED)
String requestMethod,
HttpMethodType requestMethod,

@Schema(description = "도메인 이름", example = "NOTICE", requiredMode = NOT_REQUIRED)
String domainName,
DomainType domainName,

@Schema(description = "특정 엔티티 id", requiredMode = NOT_REQUIRED)
Integer domainId
Integer domainId,

@Schema(description = "정렬 기준", requiredMode = NOT_REQUIRED)
Sort sort
) {
public AdminHistorysCondition {
public AdminHistoriesCondition {
if (Objects.isNull(page)) {
page = Criteria.DEFAULT_PAGE;
page = DEFAULT_PAGE;
}
if (Objects.isNull(limit)) {
limit = Criteria.DEFAULT_LIMIT;
limit = DEFAULT_LIMIT;
}
if (Objects.isNull(sort)) {
sort = CREATED_AT_DESC;
}
}

public Direction getSortDir() {
if (sort == CREATED_AT_ASC) {
return Direction.ASC;
}
return Direction.DESC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import in.koreatech.koin.admin.history.model.AdminActivityHistory;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonNaming(value = SnakeCaseStrategy.class)
public record AdminHistorysResponse(
public record AdminHistoriesResponse(
@Schema(description = "조건에 해당하는 히스토리 수", example = "10", requiredMode = REQUIRED)
Long totalCount,

Expand All @@ -32,10 +30,10 @@ public record AdminHistorysResponse(
Integer currentPage,

@Schema(description = "어드민 계정 리스트", requiredMode = REQUIRED)
List<InnerAdminHistorysResponse> historys
List<InnerAdminHistoriesResponse> histories
) {
@JsonNaming(value = SnakeCaseStrategy.class)
public record InnerAdminHistorysResponse(
public record InnerAdminHistoriesResponse(
@Schema(description = "고유 id", example = "1", requiredMode = REQUIRED)
Integer id,

Expand All @@ -61,31 +59,31 @@ public record InnerAdminHistorysResponse(
)
String requestMessage,

@Schema(description = "요청 시간", example = "2019-08-16-23-01-52", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd-HH-mm-ss")
@Schema(description = "요청 시간", example = "2019-08-16 23:01:52", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createdAt
) {
public static InnerAdminHistorysResponse from(AdminActivityHistory adminActivityHistory) {
return new InnerAdminHistorysResponse(
public static InnerAdminHistoriesResponse from(AdminActivityHistory adminActivityHistory) {
return new InnerAdminHistoriesResponse(
adminActivityHistory.getId(),
adminActivityHistory.getDomainId(),
adminActivityHistory.getAdmin().getUser().getName(),
DomainType.valueOf(adminActivityHistory.getDomainName()).getDescription(),
HttpMethodType.valueOf(adminActivityHistory.getRequestMethod()).getValue(),
adminActivityHistory.getDomainName().getDescription(),
adminActivityHistory.getRequestMethod().getValue(),
adminActivityHistory.getRequestMessage(),
adminActivityHistory.getCreatedAt()
);
}
}

public static AdminHistorysResponse of(Page<AdminActivityHistory> adminActivityHistoryPage) {
return new AdminHistorysResponse(
public static AdminHistoriesResponse from(Page<AdminActivityHistory> adminActivityHistoryPage) {
return new AdminHistoriesResponse(
adminActivityHistoryPage.getTotalElements(),
adminActivityHistoryPage.getContent().size(),
adminActivityHistoryPage.getTotalPages(),
adminActivityHistoryPage.getNumber() + 1,
adminActivityHistoryPage.getContent().stream()
.map(InnerAdminHistorysResponse::from)
.map(InnerAdminHistoriesResponse::from)
.collect(Collectors.toList())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ public record AdminHistoryResponse(
)
String requestMessage,

@Schema(description = "요청 시간", example = "2019-08-16-23-01-52", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd-HH-mm-ss")
@Schema(description = "요청 시간", example = "2019-08-16 23:01:52", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createdAt
) {
public static AdminHistoryResponse from(AdminActivityHistory adminActivityHistory) {
return new AdminHistoryResponse(
adminActivityHistory.getId(),
adminActivityHistory.getDomainId(),
adminActivityHistory.getAdmin().getUser().getName(),
DomainType.valueOf(adminActivityHistory.getDomainName()).getDescription(),
HttpMethodType.valueOf(adminActivityHistory.getRequestMethod()).getValue(),
adminActivityHistory.getDomainName().getDescription(),
adminActivityHistory.getRequestMethod().getValue(),
adminActivityHistory.getRequestMessage(),
adminActivityHistory.getCreatedAt()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import in.koreatech.koin.admin.history.enums.DomainType;
import in.koreatech.koin.admin.history.enums.HttpMethodType;
import in.koreatech.koin.admin.user.model.Admin;
import in.koreatech.koin.global.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -32,14 +35,14 @@ public class AdminActivityHistory extends BaseEntity {
private Integer domainId;

@NotNull
@Size(max = 10)
@Enumerated(value = EnumType.STRING)
@Column(name = "request_method", nullable = false, length = 10)
private String requestMethod;
private HttpMethodType requestMethod;

@NotNull
@Size(max = 20)
@Enumerated(value = EnumType.STRING)
@Column(name = "domain_name", nullable = false, length = 20)
private String domainName;
private DomainType domainName;

@Column(name = "request_message", columnDefinition = "TEXT")
private String requestMessage;
Expand All @@ -49,8 +52,13 @@ public class AdminActivityHistory extends BaseEntity {
private Admin admin;

@Builder
public AdminActivityHistory(Integer domainId, String requestMethod, String domainName, String requestMessage,
Admin admin) {
public AdminActivityHistory(
Integer domainId,
HttpMethodType requestMethod,
DomainType domainName,
String requestMessage,
Admin admin
) {
this.domainId = domainId;
this.requestMethod = requestMethod;
this.domainName = domainName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import in.koreatech.koin.admin.history.dto.AdminHistorysCondition;
import in.koreatech.koin.admin.history.dto.AdminHistoriesCondition;
import in.koreatech.koin.admin.history.exception.AdminActivityHistoryNotFoundException;
import in.koreatech.koin.admin.history.model.AdminActivityHistory;

public interface AdminActivityHistoryRepository extends Repository<AdminActivityHistory, Integer> {

AdminActivityHistory save(AdminActivityHistory adminActivityHistory);

Optional<AdminActivityHistory> findById(Integer id);
Expand All @@ -27,10 +28,10 @@ default AdminActivityHistory getById(Integer id) {

@Query("""
SELECT a FROM AdminActivityHistory a WHERE
(:#{#condition.requestMethod} IS NULL OR a.requestMethod = :#{#condition.requestMethod}) AND
(:#{#condition.domainName} IS NULL OR a.domainName = :#{#condition.domainName}) AND
(:#{#condition.requestMethod?.name()} IS NULL OR a.requestMethod = :#{#condition.requestMethod}) AND
(:#{#condition.domainName?.name()} IS NULL OR a.domainName = :#{#condition.domainName}) AND
(:#{#condition.domainId} IS NULL OR a.domainId = :#{#condition.domainId})
""")
Page<AdminActivityHistory> findByConditions(@Param("condition") AdminHistorysCondition adminsCondition,
Page<AdminActivityHistory> findByConditions(@Param("condition") AdminHistoriesCondition adminsCondition,
Pageable pageable);
}
Loading

0 comments on commit 885875d

Please sign in to comment.