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

[9주차/피우] 워크북 제출합니다. #41

Merged
merged 1 commit into from
Dec 2, 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 @@ -39,8 +39,11 @@ public enum ErrorStatus implements BaseErrorCode {
FILE_EXCEPTION(HttpStatus.BAD_REQUEST, "FILE4001", "파일이 등록되지 않았습니다."),

// 미션 관련 에러
ALREADY_CHALLENGE(HttpStatus.BAD_REQUEST, "MISSION4001", "이미 수행 중인 미션입니다.");

ALREADY_CHALLENGE(HttpStatus.BAD_REQUEST, "MISSION4001", "이미 수행 중인 미션입니다."),

// 페이징 관련 에러
PAGE_NOT_VALID(HttpStatus.BAD_REQUEST, "PAGE4001", "페이징 번호가 유효하지 않습니다.");

private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import javalab.umc7th_mission.domain.mapping.MemberMission;
import javalab.umc7th_mission.web.dto.Mission.MissionRequestDTO;
import javalab.umc7th_mission.web.dto.Mission.MissionResponseDTO;
import org.springframework.data.domain.Page;

import java.util.List;

public class MissionConverter {

Expand Down Expand Up @@ -34,4 +37,60 @@ public static MemberMission toMemberMission(MissionRequestDTO.ChallengeDto reque
.status(request.getStatus())
.build();
}

public static MissionResponseDTO.MissionPreViewDTO toMissionPreviewDTO(Mission mission) {
return MissionResponseDTO.MissionPreViewDTO.builder()
.missionId(mission.getId())
.content(mission.getContent())
.point(mission.getPoint())
.deadline(mission.getDeadline())
.build();
}

public static MissionResponseDTO.MissionPreViewListDTO toMissionPreviewListDTO(Page<Mission> missionList) {
List<MissionResponseDTO.MissionPreViewDTO> missionPreViewDTOList = missionList.stream()
.map(mission -> toMissionPreviewDTO(mission))
.toList();

return MissionResponseDTO.MissionPreViewListDTO.builder()
.missionList(missionPreViewDTOList)
.listSize(missionPreViewDTOList.size())
.totalPage(missionList.getTotalPages())
.totalElements(missionList.getTotalElements())
.isFirst(missionList.isFirst())
.isLast(missionList.isLast())
.build();
}

public static MissionResponseDTO.MemberMissionDTO toMemberMissionDTO(MemberMission mission) {
return MissionResponseDTO.MemberMissionDTO.builder()
.missionId(mission.getMission().getId())
.restaurantId(mission.getId())
.content(mission.getMission().getContent())
.point(mission.getMission().getPoint())
.deadline(mission.getMission().getDeadline())
.build();
}

public static MissionResponseDTO.MemberMissionListDTO toMemberMissionListDTO(Page<MemberMission> missionList) {
List<MissionResponseDTO.MemberMissionDTO> memberMissionDTOList = missionList.stream()
.map(memberMission -> toMemberMissionDTO(memberMission))
.toList();

return MissionResponseDTO.MemberMissionListDTO.builder()
.missionList(memberMissionDTOList)
.listSize(memberMissionDTOList.size())
.totalPage(missionList.getTotalPages())
.totalElements(missionList.getTotalElements())
.isFirst(missionList.isFirst())
.isLast(missionList.isLast())
.build();
}

public static MissionResponseDTO.CompleteResultDto toCompleteResultDto(MemberMission request) {
return MissionResponseDTO.CompleteResultDto.builder()
.memberMissionId(request.getId())
.status(request.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import javalab.umc7th_mission.domain.*;
import javalab.umc7th_mission.web.dto.Review.ReviewRequestDTO;
import javalab.umc7th_mission.web.dto.Review.ReviewResponseDTO;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

public class ReviewConverter {

Expand All @@ -19,4 +23,34 @@ public static Review toReview(ReviewRequestDTO.JoinDto request){
.score(request.getScore())
.build();
}

public static ReviewResponseDTO.ReviewPreViewDTO reviewPreViewDTO(Review review){

List<String> imageUrl = review.getReviewImageList().stream()
.map(reviewImage -> reviewImage.getUrl())
.toList();

return ReviewResponseDTO.ReviewPreViewDTO.builder()
.reviewer(review.getMember().getName())
.score(review.getScore())
.content(review.getContent())
.imageUrl(imageUrl)
.createdAt(review.getCreatedAt())
.build();
}

public static ReviewResponseDTO.ReviewPreViewListDTO reviewPreViewListDTO(Page<Review> reviewList){
List<ReviewResponseDTO.ReviewPreViewDTO> reviewPreViewDTOList = reviewList.stream()
.map(review -> reviewPreViewDTO(review))
.toList();

return ReviewResponseDTO.ReviewPreViewListDTO.builder()
.isLast(reviewList.isLast())
.isFirst(reviewList.isFirst())
.totalPage(reviewList.getTotalPages())
.totalElements(reviewList.getTotalElements())
.listSize(reviewPreViewDTOList.size())
.reviewList(reviewPreViewDTOList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public void setMember(Member member) {
public void setMission(Mission mission) {
this.mission = mission;
}

public void setStatus(MissionStatus status) { this.status = status; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package javalab.umc7th_mission.repository.MemberMissionRepository;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberMissionRepository extends JpaRepository<MemberMission, Long>, MemberMissionRepositoryCustom {
boolean existsByMemberIdAndMissionId(Long memberId, Long missionId);
Page<MemberMission> findAllByMemberAndStatus(Member member, MissionStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package javalab.umc7th_mission.repository.MissionRepository;

import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.Restaurant;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MissionRepository extends JpaRepository<Mission, Long>, MissionRepositoryCustom {
@Query("""
SELECT m
FROM Mission m
WHERE m.restaurant.id = :restaurantId
AND m.id NOT IN (
SELECT mm.mission.id
FROM MemberMission mm
WHERE mm.member.id = :memberId
)
""")
Page<Mission> findAllNotInMemberMission(@Param("restaurantId") Long restaurantId,
@Param("memberId") Long memberId,
Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package javalab.umc7th_mission.repository.ReviewRepository;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.Review;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewRepository extends JpaRepository<Review, Long>, ReviewRepositoryCustom {
Page<Review> findAllByRestaurantAndMember(Restaurant restaurant, Member member, PageRequest pageRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface MissionCommandService {
Mission joinMission(MissionRequestDTO.JoinDto request);
MemberMission challengeMission(MissionRequestDTO.ChallengeDto request);
MemberMission completeMission(MissionRequestDTO.CompleteDto request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import javalab.umc7th_mission.repository.MemberMissionRepository.MemberMissionRepository;
import javalab.umc7th_mission.repository.MemberRepository.MemberRepository;
Expand Down Expand Up @@ -49,4 +50,15 @@ public MemberMission challengeMission(MissionRequestDTO.ChallengeDto request) {

return memberMissionRepository.save(newMemberMission);
}

@Override
@Transactional
public MemberMission completeMission(MissionRequestDTO.CompleteDto request) {
MemberMission memberMission = memberMissionRepository.findById(request.getMemberMissionId()).get();
memberMission.setStatus(MissionStatus.COMPLETE);

return memberMission;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package javalab.umc7th_mission.service.MissionService;

import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import org.springframework.data.domain.Page;

public interface MissionQueryService {

Page<Mission> getRestaurantMissionList(Long restaurantId, Long memberId, Integer page);
Page<MemberMission> getMemberMissionList(Long memberId, MissionStatus status, Integer page);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package javalab.umc7th_mission.service.MissionService;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import javalab.umc7th_mission.repository.MemberMissionRepository.MemberMissionRepository;
import javalab.umc7th_mission.repository.MemberRepository.MemberRepository;
import javalab.umc7th_mission.repository.MissionRepository.MissionRepository;
import javalab.umc7th_mission.repository.RestaurantRepository.RestaurantRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class MissionQueryServiceImpl implements MissionQueryService {

private final MemberRepository memberRepository;
private final MissionRepository missionRepository;
private final MemberMissionRepository memberMissionRepository;

@Override
@Transactional
public Page<Mission> getRestaurantMissionList(Long restaurantId, Long memberId, Integer page) {
return missionRepository.findAllNotInMemberMission(restaurantId, memberId, PageRequest.of(page, 10));
}

@Override
@Transactional
public Page<MemberMission> getMemberMissionList(Long memberId, MissionStatus status, Integer page) {
Member member = memberRepository.findById(memberId).get();

return memberMissionRepository.findAllByMemberAndStatus(member, status, PageRequest.of(page, 10));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package javalab.umc7th_mission.service.ReviewService;

import javalab.umc7th_mission.domain.Review;
import org.springframework.data.domain.Page;

public interface ReviewQueryService {
Page<Review> getMemberReviewList(Long restaurantId, Long memberId, Integer page);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package javalab.umc7th_mission.service.ReviewService;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.Review;
import javalab.umc7th_mission.repository.MemberRepository.MemberRepository;
import javalab.umc7th_mission.repository.RestaurantRepository.RestaurantRepository;
import javalab.umc7th_mission.repository.ReviewRepository.ReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ReviewQueryServiceImpl implements ReviewQueryService {

private final RestaurantRepository restaurantRepository;
private final MemberRepository memberRepository;
private final ReviewRepository reviewRepository;

@Override
@Transactional
public Page<Review> getMemberReviewList(Long restaurantId, Long memberId, Integer page) {
Restaurant restaurant = restaurantRepository.findById(restaurantId).get();
Member member = memberRepository.findById(memberId).get();

return reviewRepository.findAllByRestaurantAndMember(restaurant, member, PageRequest.of(page, 10));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package javalab.umc7th_mission.validation.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import javalab.umc7th_mission.validation.validator.CheckPageValidator;
import javalab.umc7th_mission.validation.validator.MemberExistValidator;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = CheckPageValidator.class)
@Target( { ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckPage {

String message() default "페이지가 유효하지 않습니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package javalab.umc7th_mission.validation.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import javalab.umc7th_mission.apiPayload.code.status.ErrorStatus;
import javalab.umc7th_mission.validation.annotation.CheckPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CheckPageValidator implements ConstraintValidator<CheckPage, Integer> {

@Override
public boolean isValid(Integer page, ConstraintValidatorContext context) {
boolean isValid = (page != null && page >= 1);

if (!isValid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(ErrorStatus.PAGE_NOT_VALID.toString()).addConstraintViolation();
}

return isValid;
}
}
Loading