Skip to content

Commit

Permalink
feat: Implement week 9 API. Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jjun522 committed Nov 29, 2024
1 parent 7dcdf1c commit 5b997cf
Show file tree
Hide file tree
Showing 37 changed files with 664 additions and 55 deletions.
17 changes: 17 additions & 0 deletions src/main/java/umc/spring/Vaildator/annotation/CheckPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package umc.spring.Vaildator.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import umc.spring.Vaildator.vaildator.CheckPageValidator;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = CheckPageValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckPage {
String message() default "페이지 번호는 1 이상의 값이어야 합니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package umc.spring.Vaildator.vaildator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import umc.spring.Vaildator.annotation.CheckPage;
import umc.spring.apiPayload.code.status.ErrorStatus;

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


@Override
public void initialize(CheckPage constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

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

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

return isValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import umc.spring.apiPayload.code.status.ErrorStatus;
import umc.spring.domain.emums.MissionStatus;
import umc.spring.repository.MissionRepository.MissionRepository;
import umc.spring.repository.UserMissionRepository.UerMissionRepository;
import umc.spring.repository.UserMissionRepository.UserMissionRepository;

@Component
@RequiredArgsConstructor
public class MissionExistValidator implements ConstraintValidator<ExistMission, Long> {

private final MissionRepository missionRepository;
private final UerMissionRepository userMissionRepository;
private final UserMissionRepository userMissionRepository;

@Override
public void initialize(ExistMission constraintAnnotation) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/umc/spring/apiPayload/code/ReasonDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Getter
@Builder
public class ReasonDTO {

private HttpStatus httpstatus;

private final boolean isSuccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public enum ErrorStatus implements BaseErrorCode {
_FOOD_CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND,"FOOD4001","잘못된 요청입니다"),
Store_region_NOT_FOUND(HttpStatus.NOT_FOUND,"STORE40001","잘못된 요청입니다"),

PAGE_Error(HttpStatus.BAD_REQUEST,"Page4001","페이지 요청이 잘못된 요청입니다"),


Mission_STORE_NOT_FOUND(HttpStatus.NOT_FOUND,"MISSION40001","가게를 찾을 수 없습니다"),

User_NOT_FOUND(HttpStatus.NOT_FOUND,"USER40001","이용자를 찾을 수 없습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.spring.apiPayload.exception;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class ExceptionAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler
public ResponseEntity<Object> validation(ConstraintViolationException e, WebRequest request) {
String errorMessage = e.getConstraintViolations().stream()
.map(constraintViolation -> constraintViolation.getMessage())
.map(ConstraintViolation::getMessage)
.findFirst()
.orElseThrow(() -> new RuntimeException("ConstraintViolationException 추출 도중 에러 발생"));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package umc.spring.apiPayload.exception.handler;

import umc.spring.apiPayload.code.BaseErrorCode;
import umc.spring.apiPayload.exception.GeneralException;

public class PageHandler extends GeneralException {
public PageHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
14 changes: 12 additions & 2 deletions src/main/java/umc/spring/converter/MissionConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import umc.spring.domain.Store;
import umc.spring.domain.mapping.Mission;
import umc.spring.web.controller.dto.addMissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.addMissionDTO.MissionResponseDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionResponseDTO;


import java.time.LocalDateTime;

Expand All @@ -29,4 +30,13 @@ public static Mission ToMission(MissionRequestDTO.JoinAddMissionDTO request, Sto
.point(request.getPoint())
.build();
}

public static MissionResponseDTO.MissionDTO toMissionPageDTO(Mission mission) {
return MissionResponseDTO.MissionDTO.builder()
.missionId(mission.getId())
.title(mission.getTitle())
.description(mission.getDescription())
.point(mission.getPoint())
.build();
}
}
28 changes: 28 additions & 0 deletions src/main/java/umc/spring/converter/MyReviewConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package umc.spring.converter;

import org.springframework.data.domain.Page;
import umc.spring.domain.Review;
import umc.spring.web.controller.dto.MyReviewDTO.MyReviewResponseDTO;

import java.util.stream.Collectors;

public class MyReviewConverter {

public static MyReviewResponseDTO.PagedReviewDTO toPagedReviewDTO(Page<Review> reviewPage) {
return MyReviewResponseDTO.PagedReviewDTO.builder()
.reviews(
reviewPage.getContent().stream()
.map(review -> MyReviewResponseDTO.ReviewDTO.builder()
.reviewId(review.getId())
.comment(review.getComment())
.rating(review.getRating())
.storeId(review.getStore().getId())
.createdAt(review.getCreatedAt())
.build()
).collect(Collectors.toList())
)
.currentPage(reviewPage.getNumber() + 1) // 0 기반을 1 기반으로 변환
.totalPages(reviewPage.getTotalPages())
.build();
}
}
14 changes: 14 additions & 0 deletions src/main/java/umc/spring/converter/ReviewConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import umc.spring.domain.User;
import umc.spring.web.controller.dto.ReviewDTO.ReviewRequestDTO;
import umc.spring.web.controller.dto.ReviewDTO.ReviewResponseDTO;
import umc.spring.web.controller.dto.StoreDTO.StoreResponseDTO;

import java.time.LocalDateTime;

Expand Down Expand Up @@ -35,4 +36,17 @@ public static Review ToReview(ReviewRequestDTO.JoinReviewDTO request, User user,
.build();

}



public static StoreResponseDTO.ReviewDTO toReviewPageDTO(Review review) {
return StoreResponseDTO.ReviewDTO.builder()
.reviewId(review.getId())
.comment(review.getComment())
.rating(review.getRating())
.createdAt(review.getCreatedAt())
.build();
}


}
39 changes: 39 additions & 0 deletions src/main/java/umc/spring/converter/UserMissionConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package umc.spring.converter;

import umc.spring.domain.UserMission;
import umc.spring.domain.emums.MissionStatus;
import umc.spring.web.controller.dto.MissionDTO.UserMissionResponseDTO;
import umc.spring.web.controller.dto.UserMissionDTO.UserMissionDTO;

import java.time.LocalDateTime;

public class UserMissionConverter {

public static UserMissionResponseDTO.MissionDTO toUserMissionDTO(UserMission userMission) {
return UserMissionResponseDTO.MissionDTO.builder()
.missionId(userMission.getMission().getId())
.title(userMission.getMission().getTitle())
.description(userMission.getMission().getDescription())
.point(userMission.getMission().getPoint())
.storeName(userMission.getMission().getStore().getName())
.build();
}

public static UserMissionDTO.MissionUpdateResponseDTO toMissionUpdateDTO(UserMission userMission) {
return UserMissionDTO.MissionUpdateResponseDTO.builder()
.userMissionId(userMission.getId())
.missionStatus(userMission.getMissionStatus().name())
.completedDate(userMission.getCompletedDate())
.build();
}
// 빌더를 활용하여 상태 변경
public static UserMission updateMissionStatus(UserMission userMission, MissionStatus missionStatus, LocalDateTime completedDate) {
return UserMission.builder()
.id(userMission.getId()) // 기존 ID 유지
.user(userMission.getUser()) // 기존 User 유지
.mission(userMission.getMission()) // 기존 Mission 유지
.missionStatus(missionStatus) // 변경할 Status
.completedDate(completedDate) // 변경할 Completed Date
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package umc.spring.repository.MissionRepository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import umc.spring.domain.Store;
import umc.spring.domain.mapping.Mission;

@Repository
public interface MissionRepository extends JpaRepository<Mission, Long> {

Page<Mission> findByStore(Store store, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package umc.spring.repository.ReviewRepository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import umc.spring.domain.Review;
Expand All @@ -8,4 +10,6 @@

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> ,ReviewRepositoryCustom{
Page<Review> findByStoreId(Long storeId, Pageable pageable);
Page<Review> findByUserId(Long userId, Pageable pageable);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package umc.spring.repository.UserMissionRepository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import umc.spring.domain.User;
import umc.spring.domain.UserMission;
import umc.spring.domain.emums.MissionStatus;

@Repository
public interface UerMissionRepository extends JpaRepository<UserMission ,Long> {
public interface UserMissionRepository extends JpaRepository<UserMission ,Long> {
boolean existsByMissionIdAndMissionStatus(Long missionId, MissionStatus missionStatus);
Page<UserMission> findByUserAndMissionStatus(User user, MissionStatus missionStatus, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import umc.spring.web.controller.dto.MissionChallenge.MissionChallengeRequestDTO;
import umc.spring.web.controller.dto.MissionChallenge.MissionChallengeResponseDTO;
import umc.spring.web.controller.dto.addMissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.addMissionDTO.MissionResponseDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionResponseDTO;

public interface MissionQueryService {
MissionResponseDTO.JoinResultDTO JoinDTO(MissionRequestDTO.JoinAddMissionDTO request);
MissionChallengeResponseDTO.JoinChallengeResultDTO JoinChallengeDTD(MissionChallengeRequestDTO.JoinAddMissionChallengeDTO result , Long userId);

MissionResponseDTO.PagedMissionDTO getMissionsByStore(Long storeId, int page);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package umc.spring.service.MissionService;

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 umc.spring.apiPayload.exception.handler.MissionHandler;
Expand All @@ -12,13 +14,12 @@
import umc.spring.domain.mapping.Mission;
import umc.spring.repository.MissionRepository.MissionRepository;
import umc.spring.repository.StoreRepository.StoreRepository;
import umc.spring.repository.UserMissionRepository.UerMissionRepository;
import umc.spring.repository.UserMissionRepository.UserMissionRepository;
import umc.spring.repository.UserRepository.UserRepository;
import umc.spring.web.controller.MissionChallengeController;
import umc.spring.web.controller.dto.MissionChallenge.MissionChallengeRequestDTO;
import umc.spring.web.controller.dto.MissionChallenge.MissionChallengeResponseDTO;
import umc.spring.web.controller.dto.addMissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.addMissionDTO.MissionResponseDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionRequestDTO;
import umc.spring.web.controller.dto.MissionDTO.MissionResponseDTO;

import static umc.spring.apiPayload.code.status.ErrorStatus.*;

Expand All @@ -30,7 +31,7 @@ public class MissionQueryServiceImpl implements MissionQueryService {
private final MissionRepository missionRepository;
private final StoreRepository storeRepository;
private final UserRepository userRepository;
private final UerMissionRepository userMissionRepository;
private final UserMissionRepository userMissionRepository;

@Override
@Transactional
Expand Down Expand Up @@ -68,6 +69,22 @@ public MissionChallengeResponseDTO.JoinChallengeResultDTO JoinChallengeDTD(Missi
return MissionChallengeConverter.ToChallengeDTO(newUserMission);
}

public MissionResponseDTO.PagedMissionDTO getMissionsByStore(Long storeId, int page) {
Store store = storeRepository.findById(storeId)
.orElseThrow(() -> new IllegalArgumentException("해당 가게가 존재하지 않습니다."));

PageRequest pageRequest = PageRequest.of(page, 10); // 한 페이지당 10개
Page<MissionResponseDTO.MissionDTO> missions = missionRepository
.findByStore(store, pageRequest)
.map(MissionConverter::toMissionPageDTO);

return MissionResponseDTO.PagedMissionDTO.builder()
.missions(missions.getContent())
.currentPage(missions.getNumber() + 1) // 0-based -> 1-based
.totalPages(missions.getTotalPages())
.build();
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import umc.spring.domain.Store;
import umc.spring.domain.User;
import umc.spring.web.controller.dto.MyReviewDTO.MyReviewResponseDTO;
import umc.spring.web.controller.dto.ReviewDTO.ReviewRequestDTO;
import umc.spring.web.controller.dto.ReviewDTO.ReviewResponseDTO;

public interface ReviewQueryService {
ReviewResponseDTO.JoinResultDTO addReview(ReviewRequestDTO.JoinReviewDTO request);
MyReviewResponseDTO.PagedReviewDTO getMyReviews(Long userId, int page);
}
Loading

0 comments on commit 5b997cf

Please sign in to comment.