Skip to content

Commit

Permalink
Merge pull request #91 from KTB16Team/feature/88-feat-implement-point
Browse files Browse the repository at this point in the history
Feature:feat implement point(#88)
  • Loading branch information
yooooonshine authored Dec 12, 2024
2 parents 8ce244a + c820e26 commit a4167bf
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package aimo.backend.common.scheduler;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@Configuration
public class SchedulingConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package aimo.backend.common.scheduler;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aimo.backend.domains.member.repository.MemberAttemptCountRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SchedulingService {

private final MemberAttemptCountRepository memberAttemptCountRepository;

@Transactional
@Async
@Scheduled(cron = "0 0 0 * * ?") //매일 자정에 초기화
public void deleteAllMemberPointLimitCount() {
memberAttemptCountRepository.deleteAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import aimo.backend.common.properties.SecurityProperties;
import aimo.backend.common.security.oAuth.OAuth2LoginFailureHandler;
import aimo.backend.domains.member.service.MemberPointService;
import aimo.backend.domains.member.service.MemberService;
import aimo.backend.common.security.filter.exceptionHandlingFilter.ExceptionHandlingFilter;
import aimo.backend.common.security.filter.jwtFilter.JwtAuthenticationFilter;
Expand All @@ -35,7 +36,7 @@
public class SecurityConfig {

private final MemberService memberService;

private final MemberPointService memberPointService;
private final UserDetailsService userDetailsService;
private final JwtTokenProvider jwtTokenProvider;
private final CustomOAuth2UserService customOAuth2UserService;
Expand Down Expand Up @@ -112,7 +113,7 @@ public LoginFilter loginFilter() {

@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter(jwtTokenProvider, securityProperties, pathMatcher);
return new JwtAuthenticationFilter(jwtTokenProvider, securityProperties, pathMatcher, memberPointService);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import aimo.backend.common.exception.ApiException;
import aimo.backend.common.properties.SecurityProperties;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.service.MemberPointService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -27,6 +29,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
private final JwtTokenProvider jwtTokenProvider;
private final SecurityProperties securityProperties;
private final AntPathMatcher pathMatcher;
private final MemberPointService memberPointService;

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
Expand Down Expand Up @@ -79,7 +82,12 @@ protected void doFilterInternal(
}

checkLogoutToken(accessToken);

log.info("access 토큰 인증 성공");

// 출석 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(jwtTokenProvider.extractMemberId(accessToken).get(), IncreasePoint.ATTENDANCE);

Authentication authentication = jwtTokenProvider.getAuthentication(accessToken);
saveAuthentication(authentication);
filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package aimo.backend.common.util.memberLoader;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import aimo.backend.domains.member.entity.Member;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class MemberLoader {

public static Long getMemberId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import aimo.backend.domains.comment.repository.ChildCommentRepository;
import aimo.backend.domains.comment.repository.ParentCommentRepository;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.repository.MemberRepository;
import aimo.backend.domains.member.service.MemberPointService;
import aimo.backend.domains.post.entity.Post;
import aimo.backend.domains.post.repository.PostRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,6 +31,7 @@ public class ChildCommentService {
private final PostRepository postRepository;
private final ParentCommentService parentCommentService;
private final ParentCommentRepository parentCommentRepository;
private final MemberPointService memberPointService;

//자식 댓글 권한 확인
private void validateChildCommentAuthority(Long memberId, Long childCommentId) {
Expand Down Expand Up @@ -56,6 +59,9 @@ public void saveChildComment(SaveChildCommentParameter parameter) {

ChildComment childComment = ChildComment.of(content, member, parentComment, post);
childCommentRepository.save(childComment);

// 멤버 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(member.getId(), IncreasePoint.COMMENT);
}

//자식 댓글 수정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import aimo.backend.domains.comment.entity.ParentComment;
import aimo.backend.domains.comment.repository.ParentCommentRepository;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.repository.MemberRepository;
import aimo.backend.domains.member.service.MemberPointService;
import aimo.backend.domains.post.entity.Post;
import aimo.backend.domains.post.repository.PostRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,6 +30,7 @@ public class ParentCommentService {
private final PostRepository postRepository;
private final ParentCommentRepository parentCommentRepository;
private final MemberRepository memberRepository;
private final MemberPointService memberPointService;

// 부모 댓글 권한 확인
private void validateParentCommentAuthority(Long memberId, Long commentId) {
Expand All @@ -51,8 +54,10 @@ public void saveParentComment(SaveParentCommentParameter parameter) {
.orElseThrow(() -> ApiException.from(POST_NOT_FOUND));

ParentComment parentComment = ParentComment.of(member, post, parameter.content());

parentCommentRepository.save(parentComment);

// 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(memberId, IncreasePoint.COMMENT);
}

// 부모 댓글 수정
Expand All @@ -79,7 +84,7 @@ public void validateAndDeleteParentComment(DeleteParentCommentParameter paramete

parentCommentRepository.findById(commentId)
.ifPresent((parentComment) -> {
if(!parentComment.getChildComments().isEmpty()){
if (!parentComment.getChildComments().isEmpty()) {
parentComment.deleteParentCommentSoftlyWithContent();
return;
}
Expand All @@ -88,13 +93,13 @@ public void validateAndDeleteParentComment(DeleteParentCommentParameter paramete
}

// 자식 댓글이 없고 삭제된 부모 댓글이면 삭제
public void deleteIfParentCommentIsDeletedAndChildrenIsEmpty(ParentComment parentComment){
public void deleteIfParentCommentIsDeletedAndChildrenIsEmpty(ParentComment parentComment) {
// 부모 댓글이 삭제되지 않은 상태면 return
if (!parentComment.getIsDeleted()) {
return;
}
// 자식 댓글이 존재하면 return
if(!parentComment.getChildComments().isEmpty()){
if (!parentComment.getChildComments().isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import aimo.backend.common.util.memberLoader.MemberLoader;
import aimo.backend.domains.like.dto.parameter.LikeChildCommentParameter;
import aimo.backend.domains.like.dto.parameter.LikeParentCommentParameter;
import aimo.backend.domains.like.dto.parameter.LikePostParameter;
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.service.ChildCommentLikeService;
import aimo.backend.domains.like.service.ParentCommentLikeService;
import aimo.backend.domains.like.service.PostLikeService;
import aimo.backend.domains.like.dto.parameter.LikePostParameter;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -28,7 +28,6 @@ public class LikeController {
private final ChildCommentLikeService childCommentLikeService;
private final ParentCommentLikeService parentCommentLikeService;


@PostMapping("/comments/child/{childCommentId}/likes")
public ResponseEntity<DataResponse<Void>> likeChildComment(
@PathVariable Long childCommentId,
Expand Down Expand Up @@ -61,7 +60,6 @@ public ResponseEntity<DataResponse<Void>> likeParentComment(
.body(DataResponse.created());
}


@PostMapping("/{postId}/likes")
public ResponseEntity<DataResponse<Void>> likePost(
@PathVariable Long postId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.repository.ChildCommentLikeRepository;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.repository.MemberRepository;
import aimo.backend.domains.member.service.MemberPointService;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -24,6 +26,7 @@ public class ChildCommentLikeService {
private final ChildCommentLikeRepository childCommentLikeRepository;
private final MemberRepository memberRepository;
private final ChildCommentRepository childCommentRepository;
private final MemberPointService memberPointService;

@Transactional(rollbackFor = ApiException.class)
public void likeChildComment(LikeChildCommentParameter parameter) {
Expand All @@ -35,14 +38,19 @@ public void likeChildComment(LikeChildCommentParameter parameter) {
ChildComment childComment = childCommentRepository.findById(childCommentId)
.orElseThrow(() -> ApiException.from(CHILD_COMMENT_NOT_FOUND));

// 좋아요 타입이 LIKE인 경우, 이미 좋아요 누르지 않았을 경우에만 좋아요 저장
if (parameter.likeType() == LikeType.LIKE) {
if (childCommentLikeRepository.existsByChildCommentIdAndMemberId(childCommentId, memberId))
return;

// 좋아요 저장
childCommentLikeRepository.save(ChildCommentLike.from(member, childComment));
// 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(memberId, IncreasePoint.LIKE);
return;
}

// 좋아요 타입이 DISLIKE인 경우, 좋아요 삭제
childCommentLikeRepository.deleteByMemberIdAndChildCommentId(member.getId(), childCommentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.repository.ParentCommentLikeRepository;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.repository.MemberRepository;
import aimo.backend.domains.member.service.MemberPointService;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -24,6 +26,7 @@ public class ParentCommentLikeService {
private final ParentCommentLikeRepository parentCommentLikeRepository;
private final ParentCommentRepository parentCommentRepository;
private final MemberRepository memberRepository;
private final MemberPointService memberPointService;

@Transactional(rollbackFor = ApiException.class)
public void likeParentComment(LikeParentCommentParameter parameter) {
Expand All @@ -37,14 +40,20 @@ public void likeParentComment(LikeParentCommentParameter parameter) {
ParentComment parentComment = parentCommentRepository.findById(parentCommentId)
.orElseThrow(() -> ApiException.from(PARENT_COMMENT_NOT_FOUND));

// 라이크 타입이 LIKE인 경우 저장, 이미 라이크가 존재하면 무시
if (likeType == LikeType.LIKE) {
// 라이크가 이미 존재하면 무시
if (parentCommentLikeRepository.existsByParentCommentIdAndMemberId(parentCommentId, memberId)) return;

// 라이크 저장
parentCommentLikeRepository.save(ParentCommentLike.from(member, parentComment));
// 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(memberId, IncreasePoint.LIKE);

return;
}

// 라이크 타입이 DISLIKE인 경우 삭제
parentCommentLikeRepository.deleteByMember_IdAndParentComment_Id(member.getId(), parentCommentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import org.springframework.transaction.annotation.Transactional;

import aimo.backend.common.exception.ApiException;
import aimo.backend.domains.like.dto.parameter.LikePostParameter;
import aimo.backend.domains.like.entity.PostLike;
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.repository.PostLikeRepository;
import aimo.backend.domains.member.model.IncreasePoint;
import aimo.backend.domains.member.repository.MemberRepository;
import aimo.backend.domains.like.dto.parameter.LikePostParameter;
import aimo.backend.domains.member.service.MemberPointService;
import aimo.backend.domains.post.entity.Post;
import aimo.backend.domains.post.repository.PostRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +25,7 @@ public class PostLikeService {
private final PostLikeRepository postLikeRepository;
private final PostRepository postRepository;
private final MemberRepository memberRepository;
private final MemberPointService memberPointService;

@Transactional(rollbackFor = Exception.class)
public void likePost(LikePostParameter parameter) {
Expand Down Expand Up @@ -51,7 +54,9 @@ public void likePost(LikePostParameter parameter) {

// 포스트 라이크 수 증가
post.increasePostLikesCount();
return ;
// 멤버 포인트 증가
memberPointService.checkAndIncreaseMemberPoint(memberId, IncreasePoint.LIKE);
return;
}

// 라이크가 이미 존재하면 삭제
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aimo.backend.domains.member.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import aimo.backend.common.dto.DataResponse;
import aimo.backend.common.util.memberLoader.MemberLoader;
import aimo.backend.domains.member.dto.response.FindMemberPointResponse;
import aimo.backend.domains.member.service.MemberPointService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/v1/members/points")
@RequiredArgsConstructor
public class MemberPointController {

private final MemberPointService memberPointService;

// 멤버 포인트 조회
@GetMapping
public ResponseEntity<DataResponse<FindMemberPointResponse>> findMemberPoint() {
Long memberId = MemberLoader.getMemberId();

FindMemberPointResponse response = memberPointService.findMemberPoint(memberId);

return ResponseEntity.ok(DataResponse.from(response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package aimo.backend.domains.member.dto.response;

public record FindMemberPointResponse(
Integer memberPoint
) {
}
Loading

0 comments on commit a4167bf

Please sign in to comment.