Skip to content

Commit

Permalink
feat: 9주차 미션 수행
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdcheol committed Nov 29, 2024
1 parent 1f26d08 commit de0def3
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package javalab.umc7th_mission.domain.membermission.controller;

import javalab.umc7th_mission.config.annotation.CheckPage;
import javalab.umc7th_mission.config.apipayload.ApiResponse;
import javalab.umc7th_mission.domain.membermission.dto.MemberMissionResponseDTO;
import javalab.umc7th_mission.domain.membermission.service.MemberMissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/missions")
public class MemberMissionController {

private final MemberMissionService memberMissionService;
private static final Integer PAGE_SIZE = 10;

@GetMapping("/member/{memberId}/ongoing")
public ApiResponse<Page<MemberMissionResponseDTO>> getOngoingMissionsByMember(
@PathVariable Long memberId,
@RequestParam @CheckPage Integer page
) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
return ApiResponse.onSuccess(
memberMissionService.getOngoingMissionsByMember(memberId, pageable));
}

@PatchMapping("/member/{memberId}/complete/{missionId}")
public ResponseEntity<String> completeMission(
@PathVariable Long memberId,
@PathVariable Long missionId
) {
memberMissionService.completeMission(memberId, missionId);
return ResponseEntity.ok("Mission marked as complete");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package javalab.umc7th_mission.domain.membermission.dto;

public record MemberMissionResponseDTO(
Long missionId,
String missionSpec,
Integer reward,
String status,
String storeName
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package javalab.umc7th_mission.domain.membermission.repository;

import java.util.Optional;
import javalab.umc7th_mission.domain.membermission.MemberMission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberMissionRepository extends JpaRepository<MemberMission, Long> {

Page<MemberMission> findByMemberIdAndStatus(Long memberId, String status, Pageable pageable);

Optional<MemberMission> findByMemberIdAndMissionId(Long memberId, Long missionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package javalab.umc7th_mission.domain.membermission.service;

import javalab.umc7th_mission.config.apipayload.code.status.ErrorStatus;
import javalab.umc7th_mission.config.apipayload.exception.GeneralException;
import javalab.umc7th_mission.domain.membermission.MemberMission;
import javalab.umc7th_mission.domain.membermission.dto.MemberMissionResponseDTO;
import javalab.umc7th_mission.domain.membermission.repository.MemberMissionRepository;
import javalab.umc7th_mission.domain.mission.MissionStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final MemberMissionRepository memberMissionRepository;

public Page<MemberMissionResponseDTO> getOngoingMissionsByMember(Long memberId,
Pageable pageable) {
String status = MissionStatus.CHALLENGING.name();

return memberMissionRepository.findByMemberIdAndStatus(memberId, status, pageable)
.map(memberMission -> new MemberMissionResponseDTO(
memberMission.getMission().getId(),
memberMission.getMission().getMissionSpec(),
memberMission.getMission().getReward(),
memberMission.getStatus().name(),
memberMission.getMission().getStore().getName()
));
}

@Transactional
public void completeMission(Long memberId, Long missionId) {
MemberMission memberMission = memberMissionRepository.findByMemberIdAndMissionId(memberId,
missionId)
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));
if (!memberMission.getStatus().equals(MissionStatus.CHALLENGING)) {
throw new GeneralException(ErrorStatus._BAD_REQUEST);
}
memberMission.complete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package javalab.umc7th_mission.domain.mission.controller;

import javalab.umc7th_mission.config.annotation.CheckPage;
import javalab.umc7th_mission.config.apipayload.ApiResponse;
import javalab.umc7th_mission.domain.mission.dto.MissionResponseDto;
import javalab.umc7th_mission.domain.mission.service.MissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/missions")
public class MissionController {

private static final Integer PAGE_SIZE = 10;
private final MissionService service;

@GetMapping("/store/{storeId}")
public ApiResponse<Page<MissionResponseDto>> getMissionsByStore(
@PathVariable Long storeId,
@RequestParam @CheckPage Integer page
) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
Page<MissionResponseDto> missions = service.getMissionsByStore(storeId, pageable);
return ApiResponse.onSuccess(missions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package javalab.umc7th_mission.domain.mission.dto;

public record MissionResponseDto(
Long missionId,
Integer reward,
String missionSpec,
String storeName
) {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package javalab.umc7th_mission.domain.mission.repository;

import javalab.umc7th_mission.domain.mission.Mission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MissionRepository extends JpaRepository<Mission, Long> {

Page<Mission> findByStoreId(Long storeId, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package javalab.umc7th_mission.domain.mission.service;

import javalab.umc7th_mission.domain.mission.dto.MissionResponseDto;
import javalab.umc7th_mission.domain.mission.repository.MissionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final MissionRepository repository;

public Page<MissionResponseDto> getMissionsByStore(Long storeId, Pageable pageable) {
return repository.findByStoreId(storeId, pageable)
.map(mission -> new MissionResponseDto(
mission.getId(),
mission.getReward(),
mission.getMissionSpec(),
mission.getStore().getName()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package javalab.umc7th_mission.domain.review.controller;

import javalab.umc7th_mission.config.apipayload.ApiResponse;
import javalab.umc7th_mission.domain.review.dto.ReviewResponse.ReviewsDTO;
import javalab.umc7th_mission.domain.review.service.ReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class ReviewController {

private final ReviewService reviewService;

private static final Integer PAGE_SIZE = 10;

@GetMapping("/member")
public ApiResponse<ReviewsDTO> getReviewsByMember(
Long memberId, // 로그인 되어있다고 가정
@RequestParam(defaultValue = "0") int page
) {
memberId = 0L; // 추후 argumentResolver 이용
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
ReviewsDTO result = reviewService.getReviewsByMember(memberId, pageable);
return ApiResponse.onSuccess(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package javalab.umc7th_mission.domain.review.dto;

public class ReviewRequest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package javalab.umc7th_mission.domain.review.dto;

import java.time.LocalDate;
import java.util.List;

public final class ReviewResponse {

private ReviewResponse() {
}

public record ReviewsDTO(
List<ReviewDTO> reviews
) {

}

public record ReviewDTO(
String nickname,
Double score,
String body,
LocalDate createdAt
) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package javalab.umc7th_mission.domain.review.repository;

import javalab.umc7th_mission.domain.review.Review;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewRepository extends JpaRepository<Review, Long> {

Page<Review> findByMemberId(Long memberId, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package javalab.umc7th_mission.domain.review.service;

import java.util.ArrayList;
import java.util.List;
import javalab.umc7th_mission.domain.review.Review;
import javalab.umc7th_mission.domain.review.dto.ReviewResponse;
import javalab.umc7th_mission.domain.review.dto.ReviewResponse.ReviewDTO;
import javalab.umc7th_mission.domain.review.repository.ReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ReviewService {

private final ReviewRepository reviewRepository;

public ReviewResponse.ReviewsDTO getReviewsByMember(Long memberId, Pageable pageable) {
Page<Review> reviews = reviewRepository.findByMemberId(memberId, pageable);

List<ReviewDTO> reviewDTOs = new ArrayList<>();
for (Review review : reviews) {
ReviewDTO reviewDTO = new ReviewDTO(
review.getMember().getName(),
review.getScore(),
review.getBody(),
review.getCreatedAt().toLocalDate()
);
reviewDTOs.add(reviewDTO);
}

return new ReviewResponse.ReviewsDTO(reviewDTOs);

}
}

0 comments on commit de0def3

Please sign in to comment.