Skip to content

Commit

Permalink
refactor : 커스텀 어노테이션을 통한 AoP 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youKeon committed Oct 4, 2023
1 parent 4258950 commit 75dc1e5
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import com.psq.backend.problem.application.ProblemService;
import com.psq.backend.problem.domain.Problem;
import com.psq.backend.problem.dto.response.ProblemListResponse;
import com.psq.backend.problem.exception.NoSuchProblemException;
import com.psq.backend.problem.persistence.ProblemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -25,11 +22,9 @@
@RequiredArgsConstructor
public class BookmarkService {
private final BookmarkRepository bookmarkRepository;
private final MemberService memberService;
private final ProblemService problemService;

public void save(HttpServletRequest request, BookmarkSaveRequest saveRequest) {
Member member = memberService.getMember(request);
public void save(Member member, BookmarkSaveRequest saveRequest) {
Problem problem = problemService.getProblem(saveRequest.getProblemId());

if (bookmarkRepository.isExistedBookmark(member.getId(), problem.getId()))
Expand All @@ -40,15 +35,13 @@ public void save(HttpServletRequest request, BookmarkSaveRequest saveRequest) {

public void delete(Long id) {
Bookmark bookmark = bookmarkRepository.findById(id).orElseThrow(
() -> new NoSuchBookmarkException()
NoSuchBookmarkException::new
);
bookmarkRepository.delete(bookmark);
}

public List<ProblemListResponse> getBookmarkList(HttpServletRequest request) {
Long memberId = memberService.getMember(request).getId();

List<Bookmark> bookmarkList = bookmarkRepository.findBookmarkByFetchJoin(memberId);
public List<ProblemListResponse> getBookmarkList(Member member) {
List<Bookmark> bookmarkList = bookmarkRepository.findBookmarkByFetchJoin(member.getId());

if (bookmarkList.isEmpty()) throw new NoSuchBookmarkException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.psq.backend.bookmark.application.BookmarkService;
import com.psq.backend.bookmark.dto.request.BookmarkSaveRequest;
import com.psq.backend.member.domain.CurrentUser;
import com.psq.backend.member.domain.Member;
import com.psq.backend.problem.dto.response.ProblemListResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

Expand All @@ -21,9 +22,9 @@ public class BookmarkController {

@Operation(summary = "북마크 등록")
@PostMapping
public ResponseEntity<Void> save(@RequestBody @Valid BookmarkSaveRequest saveRequest,
HttpServletRequest request) {
bookmarkService.save(request, saveRequest);
public ResponseEntity<Void> save(@CurrentUser Member member,
@RequestBody @Valid BookmarkSaveRequest saveRequest) {
bookmarkService.save(member, saveRequest);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@Operation(summary = "북마크 삭제")
Expand All @@ -34,8 +35,8 @@ public ResponseEntity<Void> delete(@PathVariable Long id) {
}
@Operation(summary = "북마크 리스트 조회")
@GetMapping
public ResponseEntity<List<ProblemListResponse>> getBookmarkList(HttpServletRequest request) {
List<ProblemListResponse> response = bookmarkService.getBookmarkList(request);
public ResponseEntity<List<ProblemListResponse>> getBookmarkList(@CurrentUser Member member) {
List<ProblemListResponse> response = bookmarkService.getBookmarkList(member);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.psq.backend.global.auth;
import com.psq.backend.member.domain.CurrentUser;
import com.psq.backend.member.domain.Member;
import com.psq.backend.member.domain.SessionInfo;
import com.psq.backend.member.exception.NoSuchMemberException;
import com.psq.backend.member.persistence.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpSession;
@Slf4j
@Component
@RequiredArgsConstructor
public class CurrentUserResolver implements HandlerMethodArgumentResolver {

private final HttpSession httpSession;
private final MemberRepository memberRepository;

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(CurrentUser.class)
&& parameter.getParameterType().equals(Member.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
SessionInfo sessionInfo = (SessionInfo) httpSession.getAttribute("sessionInfo");
if (sessionInfo == null) throw new NoSuchMemberException("잘못된 세션 정보입니다.");
Long memberId = sessionInfo.getId();
return memberRepository.findById(memberId).orElseThrow(
NoSuchMemberException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.psq.backend.global.config;


import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down
16 changes: 16 additions & 0 deletions backend/src/main/java/com/psq/backend/global/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package com.psq.backend.global.config;


import com.psq.backend.global.auth.CurrentUserResolver;
import com.psq.backend.member.domain.CurrentUser;
import com.psq.backend.member.persistence.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {
private final CurrentUserResolver currentUserResolver;

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedOrigins("*")
.allowedMethods("*");
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(currentUserResolver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void signup(MemberSignUpRequest request) {
memberRepository.save(request.toEntity(encodedPassword, salt));
}

public String signin(MemberSignInRequest request, HttpSession session) {
public void signin(MemberSignInRequest request, HttpSession session) {
Member member = memberRepository.findByEmail(request.getEmail()).orElseThrow(
() -> new NoSuchMemberException("로그인에 실패했습니다.")
);
Expand All @@ -46,17 +46,15 @@ public String signin(MemberSignInRequest request, HttpSession session) {

SessionInfo sessionInfo = new SessionInfo(member.getId(), member.getEmail());
session.setAttribute("sessionInfo", sessionInfo);
return session.getId();
}

public Member getMember(HttpServletRequest request) {
SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo");
if (sessionInfo == null) throw new NoSuchMemberException("잘못된 세션 정보입니다.");

Long memberId = sessionInfo.getId();
Member member = memberRepository.findById(memberId).orElseThrow(
() -> new NoSuchMemberException()
return memberRepository.findById(memberId).orElseThrow(
NoSuchMemberException::new
);
return member;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.psq.backend.member.domain;

import java.lang.annotation.*;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface CurrentUser {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public ResponseEntity<Void> signup(@RequestBody @Valid MemberSignUpRequest reque

@Operation(summary = "로그인")
@PostMapping("/signin")
public ResponseEntity<String> signin(@RequestBody @Valid MemberSignInRequest request, HttpSession session) {
String key = memberService.signin(request, session);
return ResponseEntity.ok(key);
public ResponseEntity<Void> signin(@RequestBody @Valid MemberSignInRequest request, HttpSession session) {
memberService.signin(request, session);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,21 @@ public class ProblemService {
private final ProblemRepository problemRepository;
private final MemberService memberService;

public void save(HttpServletRequest servletRequest,
public void save(Member member,
ProblemSaveRequest request) {

Member member = memberService.getMember(servletRequest);
Problem problem = request.toEntity(member);
problemRepository.save(problem);
}

public List<ProblemListResponse> getProblemList(HttpServletRequest request,
public List<ProblemListResponse> getProblemList(Member member,
Integer level,
Category category,
Boolean isSolved,
Pageable pageable) {


Long memberId = memberService.getMember(request).getId();

List<ProblemListResponse> problemList = problemRepository.findAllProblem(memberId, level, category, isSolved, pageable);
List<ProblemListResponse> problemList = problemRepository.findAllProblem(member.getId(), level, category, isSolved, pageable);

if (problemList.isEmpty()) throw new NoSuchProblemException("문제가 존재하지 않습니다.");
return problemList;
Expand All @@ -59,9 +56,8 @@ public ProblemResponse getProblemInfo(Long id) {
return problemRepository.findProblem(id).orElseThrow(NoSuchProblemException::new);
}

public ProblemResponse pollProblem(HttpServletRequest request) {
Long memberId = memberService.getMember(request).getId();
return problemRepository.pollProblem(memberId).orElseThrow(NoSuchProblemException::new);
public ProblemResponse pollProblem(Member member) {
return problemRepository.pollProblem(member.getId()).orElseThrow(NoSuchProblemException::new);
}

public void update(Long id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.psq.backend.problem.presentation;

import com.psq.backend.member.domain.CurrentUser;
import com.psq.backend.member.domain.Member;
import com.psq.backend.problem.application.ProblemService;
import com.psq.backend.problem.domain.Category;
import com.psq.backend.problem.dto.request.ProblemSaveRequest;
Expand All @@ -13,7 +15,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

Expand All @@ -32,27 +33,27 @@ public ResponseEntity<ProblemResponse> getProblem(@PathVariable Long id) {

@Operation(summary = "문제 조회")
@GetMapping
public ResponseEntity<List<ProblemListResponse>> getProblemList(HttpServletRequest request,
public ResponseEntity<List<ProblemListResponse>> getProblemList(@CurrentUser Member member,
@RequestParam(required = false) Integer level,
@RequestParam(required = false) Category category,
@RequestParam(required = false) Boolean isSolved,
Pageable pageable) {
List<ProblemListResponse> response = problemService.getProblemList(request, level, category, isSolved, pageable);
List<ProblemListResponse> response = problemService.getProblemList(member, level, category, isSolved, pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "가장 먼저 등록한 문제 조회")
@GetMapping("/poll")
public ResponseEntity<ProblemResponse> pollProblem(HttpServletRequest request) {
ProblemResponse response = problemService.pollProblem(request);
public ResponseEntity<ProblemResponse> pollProblem(@CurrentUser Member member) {
ProblemResponse response = problemService.pollProblem(member);
return ResponseEntity.ok(response);
}

@Operation(summary = "문제 등록")
@PostMapping
public ResponseEntity<Void> save(HttpServletRequest servletRequest,
public ResponseEntity<Void> save(@CurrentUser Member member,
@RequestBody @Valid ProblemSaveRequest request) {
problemService.save(servletRequest, request);
problemService.save(member, request);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

Expand Down
Loading

0 comments on commit 75dc1e5

Please sign in to comment.