[Practice] 사용자 관련 API 만들기 (8)

2021. 4. 19. 13:15Spring/Practice

1. 문제

  • AOP의 Around를 이용하여 게시판 상세 조회에 대한 히스토리 기록하는 기능 생성

 

 

 

2. 풀이

- BoardService.java

package com.example.jpa.sample.board.service;

import com.example.jpa.sample.board.entity.Board;
import com.example.jpa.sample.board.entity.BoardBadReport;
import com.example.jpa.sample.board.entity.BoardComment;
import com.example.jpa.sample.board.entity.BoardType;
import com.example.jpa.sample.board.model.*;

import java.util.List;

public interface BoardService {

    ...
    
    Board detail(Long id);
}

- BoardServiceImpl.java

package com.example.jpa.sample.board.service;

import com.example.jpa.sample.board.entity.*;
import com.example.jpa.sample.board.model.*;
import com.example.jpa.sample.board.repository.*;
import com.example.jpa.sample.common.exception.BizException;
import com.example.jpa.sample.user.entity.User;
import com.example.jpa.sample.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class BoardServiceImpl implements BoardService {

    ...
    
    @Override
    public Board detail(Long id) {

        Optional<Board> optionalBoard = boardRepository.findById(id);
        if(!optionalBoard.isPresent()) {
            throw new BizException("게시글이 존재하지 않습니다.");
        }
        Board board = optionalBoard.get();

        return board;
    }
}

- BoardLogger.java

package com.example.jpa.sample.common.aop;

import com.example.jpa.sample.logs.service.LogService;
import com.example.jpa.sample.user.entity.User;
import com.example.jpa.sample.user.model.UserLogin;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j // 로그 찍어보는 용도로 사용
@RequiredArgsConstructor
@Aspect
@Component
public class BoardLogger {

    private final LogService logService;

    @Around("execution(* com.example.jpa.sample..*.*Controller.detail(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {

        log.info("#################################");
        log.info("컨트롤러 Detail 서비스 호출 전");

        Object result = joinPoint.proceed();

        if(joinPoint.getSignature().getDeclaringTypeName().contains("ApiBoardController")
            && "detail".equals(joinPoint.getSignature().getName())) {

            StringBuilder sb = new StringBuilder();
            sb.append("파라미터: ");

            Object[] args = joinPoint.getArgs();
            for(Object o : args) {
                sb.append(o.toString());
            }

            sb.append("결과: ");
            sb.append(result.toString());
            log.info(sb.toString());

            logService.add(sb.toString());
        }


        log.info("#################################");
        log.info("컨트롤러 Detail 호출 후");

        return result;
    }


}
728x90