Skip to content

Commit

Permalink
Merge pull request #15 from Mojacknong/feature_6/farmclub-기본-api
Browse files Browse the repository at this point in the history
Feature 6/farmclub 기본 api
  • Loading branch information
MinchoGreenT authored May 9, 2024
2 parents d8b1e67 + ad24b03 commit 61ba1d1
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateFarmClubRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostCommentRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostLikeRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.helper.FarmClubHelper;
import com.modernfarmer.farmusspring.domain.farmclub.service.FarmClubService;
Expand Down Expand Up @@ -76,4 +78,39 @@ public BaseResponseDto<?> createMissionPost(
) {
return BaseResponseDto.of(SuccessCode.CREATED, missionPostService.createMissionPost(requestDto, image));
}

@PostMapping("/mission/comment")
public BaseResponseDto<?> createMissionPostComment(
@AuthenticationPrincipal CustomUser user,
@RequestBody CreateMissionPostCommentRequestDto requestDto
) {
return BaseResponseDto.of(SuccessCode.CREATED, missionPostService.createMissionPostComment(user.getUserId(), requestDto));
}

@PostMapping("/mission/like")
public BaseResponseDto<?> createMissionPostLike(
@AuthenticationPrincipal CustomUser user,
@RequestBody CreateMissionPostLikeRequestDto requestDto
) {
return BaseResponseDto.of(SuccessCode.CREATED, missionPostService.createMissionPostLike(user.getUserId(), requestDto.missionPostId()));
}

@GetMapping("/{id}/mission")
public BaseResponseDto<?> getMissionPostList(
@PathVariable Long id
) {
return BaseResponseDto.of(SuccessCode.SUCCESS, missionPostService.getMissionPostList(id));
}

@GetMapping("/mission/{id}")
public BaseResponseDto<?> getMissionPostComments(
@PathVariable Long id
) {
return BaseResponseDto.of(SuccessCode.SUCCESS, missionPostService.getMissionPostComment(id));
}

@GetMapping("/help")
public BaseResponseDto<?> getFarmClubHelp() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.req;

public record CreateMissionPostCommentRequestDto(
Long missionPostId,
String content
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.req;

public record CreateMissionPostLikeRequestDto(
Long missionPostId,
Long userId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.res;


import lombok.Builder;

@Builder
public record CreateMissionPostCommentResponseDto(
Long missionPostCommentId
) {

public static CreateMissionPostCommentResponseDto of(Long missionPostCommentId) {
return CreateMissionPostCommentResponseDto.builder()
.missionPostCommentId(missionPostCommentId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.res;

import lombok.Builder;

@Builder
public record CreateMissionPostLikeResponseDto(
Long missionPostLikeId
) {

public static CreateMissionPostLikeResponseDto of(Long missionPostLikeId) {
return CreateMissionPostLikeResponseDto.builder()
.missionPostLikeId(missionPostLikeId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.res;

import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostCommentVo;
import lombok.Builder;

import java.util.List;

@Builder
public record GetMissionPostCommentResponseDto(
List<MissionPostCommentVo> comments
) {
public static GetMissionPostCommentResponseDto of(List<MissionPostCommentVo> comments) {
return GetMissionPostCommentResponseDto.builder()
.comments(comments)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.modernfarmer.farmusspring.domain.farmclub.dto.res;

import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostVo;
import lombok.Builder;

import java.util.List;

@Builder
public record GetMissionPostListResponseDto(
List<MissionPostVo> missionPosts
) {
public static GetMissionPostListResponseDto of(List<MissionPostVo> missionPosts) {
return GetMissionPostListResponseDto.builder()
.missionPosts(missionPosts)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
public enum FarmClubErrorCode implements ResponseCode {

FARM_CLUB_NOT_FOUND(3001, "해당 팜클럽을 찾을 수 없습니다."),
USER_FARM_CLUB_NOT_FOUND(3002, "해당 등록 정보를 찾을 수 없습니다.");
USER_FARM_CLUB_NOT_FOUND(3002, "해당 등록 정보를 찾을 수 없습니다."),
MISSION_POST_NOT_FOUND(3003, "해당 미션 포스트를 찾을 수 없습니다.");

private final int code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.modernfarmer.farmusspring.domain.farmclub.exception;

import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubBaseException;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubEntityNotFoundException;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -13,7 +13,7 @@
public class FarmClubExceptionHandler {

@ExceptionHandler(FarmClubBaseException.class)
public BaseResponseDto<?> handleFarmClubNotFoundException(EntityNotFoundException e, HttpServletRequest request) {
public BaseResponseDto<?> handleFarmClubNotFoundException(FarmClubEntityNotFoundException e, HttpServletRequest request) {
log.error("FarmClubNotFoundException : {} {} errMessage={}\n",
request.getMethod(),
request.getRequestURI(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode;

public class EntityNotFoundException extends FarmClubBaseException {
public class FarmClubEntityNotFoundException extends FarmClubBaseException {

public EntityNotFoundException(String message, FarmClubErrorCode errorCode) {
public FarmClubEntityNotFoundException(String message, FarmClubErrorCode errorCode) {
super(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.modernfarmer.farmusspring.domain.farmclub.helper;

import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost;
import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubEntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MissionPostHelper {

private final MissionPostRepository missionPostRepository;

public MissionPost getMissionPost(Long missionPostId) {
return missionPostRepository.findById(missionPostId)
.orElseThrow(() -> new FarmClubEntityNotFoundException("존재하지 않는 미션 게시글입니다.", FarmClubErrorCode.MISSION_POST_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubEntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.repository.UserFarmClubRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -17,6 +17,6 @@ public class UserFarmClubHelper {

public UserFarmClub getUserFarmClubEntity(Long id) {
return userFarmClubRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("존재하지 않는 등록 정보입니다.", FarmClubErrorCode.USER_FARM_CLUB_NOT_FOUND));
.orElseThrow(() -> new FarmClubEntityNotFoundException("존재하지 않는 등록 정보입니다.", FarmClubErrorCode.USER_FARM_CLUB_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.modernfarmer.farmusspring.domain.farmclub.dto.res.*;
import com.modernfarmer.farmusspring.domain.farmclub.entity.QFarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.exception.FarmClubErrorCode;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.EntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.exception.custom.FarmClubEntityNotFoundException;
import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.QGetMyFarmClubVo_BaseInfo;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down Expand Up @@ -75,7 +75,7 @@ public GetMyFarmClubVo findMyFarmClub(Long farmClubId, Long userId) {

return GetMyFarmClubVo.of(baseInfo, userFarmClubCount, daySinceStart);
} else {
throw new EntityNotFoundException("내 팜클럽을 불러오는 도중 에러가 발생했습니다.", FarmClubErrorCode.FARM_CLUB_NOT_FOUND);
throw new FarmClubEntityNotFoundException("내 팜클럽을 불러오는 도중 에러가 발생했습니다.", FarmClubErrorCode.FARM_CLUB_NOT_FOUND);
}
}

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


import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostCommentVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostVo;

import java.util.List;

public interface MissionPostRepositoryCustom {

List<GetMissionPostListWithStepCountsAndImagesVo> getMissionPostStepNumAndImage(Long farmClubId);
List<MissionPostCommentVo> getMissionPostComment(Long missionPostId);
List<MissionPostVo> getMissionPostList(Long missionPostId);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.modernfarmer.farmusspring.domain.farmclub.repository;


import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.QGetMissionPostListWithStepCountsAndImagesVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.*;
import com.querydsl.core.group.GroupBy;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand All @@ -12,7 +12,11 @@

import static com.modernfarmer.farmusspring.domain.farmclub.entity.QFarmClub.farmClub;
import static com.modernfarmer.farmusspring.domain.farmclub.entity.QMissionPost.missionPost;
import static com.modernfarmer.farmusspring.domain.farmclub.entity.QMissionPostComment.missionPostComment;
import static com.modernfarmer.farmusspring.domain.farmclub.entity.QMissionPostLike.missionPostLike;
import static com.modernfarmer.farmusspring.domain.farmclub.entity.QUserFarmClub.userFarmClub;
import static com.modernfarmer.farmusspring.domain.myveggiegarden.entity.QMyVeggie.myVeggie;
import static com.modernfarmer.farmusspring.domain.user.entity.QUser.user;

public class MissionPostRepositoryImpl implements MissionPostRepositoryCustom {

Expand All @@ -36,4 +40,41 @@ public List<GetMissionPostListWithStepCountsAndImagesVo> getMissionPostStepNumAn
))
);
}

@Override
public List<MissionPostCommentVo> getMissionPostComment(Long missionPostId) {
JPAQueryFactory queryFactory = new JPAQueryFactory(em);

return queryFactory
.select(new QMissionPostCommentVo(missionPost, user))
.from(missionPost)
.join(missionPost.userFarmClub, userFarmClub)
.join(userFarmClub.myVeggie, myVeggie)
.join(myVeggie.user, user)
.where(missionPost.id.eq(missionPostId))
.fetch();
}

@Override
public List<MissionPostVo> getMissionPostList(Long farmClubId) {
JPAQueryFactory queryFactory = new JPAQueryFactory(em);

return queryFactory
.select(new QMissionPostVo(
missionPost,
user,
JPAExpressions.select(missionPostLike.count())
.from(missionPostLike)
.where(missionPostLike.missionPost.eq(missionPost)),
JPAExpressions.select(missionPostComment.count())
.from(missionPostComment)
.where(missionPostComment.missionPost.eq(missionPost))))
.from(missionPost)
.join(missionPost.userFarmClub, userFarmClub)
.join(userFarmClub.myVeggie, myVeggie)
.join(myVeggie.user, user)
.where(farmClub.id.eq(farmClubId))
.orderBy(missionPost.createdDate.desc())
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.modernfarmer.farmusspring.domain.farmclub.service;

import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostCommentRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.req.CreateMissionPostRequestDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.res.CreateMissionPostResponseDto;
import com.modernfarmer.farmusspring.domain.farmclub.dto.res.*;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPost;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPostComment;
import com.modernfarmer.farmusspring.domain.farmclub.entity.MissionPostLike;
import com.modernfarmer.farmusspring.domain.farmclub.entity.UserFarmClub;
import com.modernfarmer.farmusspring.domain.farmclub.helper.MissionPostHelper;
import com.modernfarmer.farmusspring.domain.farmclub.helper.UserFarmClubHelper;
import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository;
import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostCommentVo;
import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostVo;
import com.modernfarmer.farmusspring.domain.user.entity.User;
import com.modernfarmer.farmusspring.domain.user.helper.UserHelper;
import com.modernfarmer.farmusspring.infra.s3.S3Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
Expand All @@ -20,6 +30,8 @@ public class MissionPostService {
private final MissionPostRepository missionPostRepository;

private final UserFarmClubHelper userFarmClubHelper;
private final MissionPostHelper missionPostHelper;
private final UserHelper userHelper;

private final S3Service s3Service;

Expand All @@ -30,6 +42,30 @@ public CreateMissionPostResponseDto createMissionPost(CreateMissionPostRequestDt
return CreateMissionPostResponseDto.of(missionPostId);
}

public CreateMissionPostCommentResponseDto createMissionPostComment(Long userId, CreateMissionPostCommentRequestDto requestDto) {
MissionPost missionPost = missionPostHelper.getMissionPost(requestDto.missionPostId());
User user = userHelper.getUserEntity(userId);
MissionPostComment.createMissionPostComment(requestDto.content(), missionPost, user);
return CreateMissionPostCommentResponseDto.of(missionPost.getId());
}

public CreateMissionPostLikeResponseDto createMissionPostLike(Long userId, Long missionPostId) {
MissionPost missionPost = missionPostHelper.getMissionPost(missionPostId);
User user = userHelper.getUserEntity(userId);
MissionPostLike.createMissionPostLike(missionPost, user);
return CreateMissionPostLikeResponseDto.of(missionPost.getId());
}

public GetMissionPostListResponseDto getMissionPostList(Long farmClubId) {
List<MissionPostVo> missionPosts = missionPostRepository.getMissionPostList(farmClubId);
return GetMissionPostListResponseDto.of(missionPosts);
}

public GetMissionPostCommentResponseDto getMissionPostComment(Long missionPostId) {
List<MissionPostCommentVo> comments = missionPostRepository.getMissionPostComment(missionPostId);
return GetMissionPostCommentResponseDto.of(comments);
}

private MissionPost saveMissionPost(MissionPost missionPost) {
return missionPostRepository.save(missionPost);
}
Expand Down
Loading

0 comments on commit 61ba1d1

Please sign in to comment.