Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 어드민 히스토리 기능 수정 #1131

Merged
merged 19 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

static import는 좋은데, 위는 CREATED_AT_DESC는 static import를 사용하고, 아래의 Sort.CREATED_AT_ASC를 사용한다면 조금 모호하게 의미파악이 되지 않을까 생각이 드네요👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

밑에도 static import로 변경하겠습니다 !

}
}

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

훨씬 깔끔해진 거 같네요

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
Loading