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

[8주차/공공] 워크북 제출합니다. #35

Merged
merged 1 commit into from
Nov 24, 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 @@ -17,6 +17,7 @@ public enum ErrorStatus implements BaseErrorCode {
NICKNAME_NOT_EXIST(HttpStatus.BAD_REQUEST, "MEMBER4002", "닉네임은 필수 입니다."),
ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "ARTICLE4001", "게시글이 없습니다."),
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트");
FOOD_CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, "FOOD4001", "존재하지 않는 음식 카테고리입니다.");
private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.spring.apiPayload.exception;
package javalab.umc7th_mission.apiPayload.exception;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.spring.apiPayload.exception;
package javalab.umc7th_mission.apiPayload.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package javalab.umc7th_mission.apiPayload.exception.handler;

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

public class FoodCategoryHandler extends GeneralException {
public FoodCategoryHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.spring.apiPayload.exception.handler;
package javalab.umc7th_mission.apiPayload.exception.handler;

import umc.spring.apiPayload.code.BaseErrorCode;
import umc.spring.apiPayload.exception.GeneralException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package javalab.umc7th_mission.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import umc.spring.apiPayload.ApiResponse;
import umc.spring.apiPayload.exception.GeneralException;
import umc.spring.converter.MemberConverter;
import umc.spring.domain.Member;
import umc.spring.dto.MemberRequestDTO;
import umc.spring.dto.MemberResponseDTO;
import umc.spring.service.MemberService.MemberCommandService;
import umc.spring.service.MissionService.MissionService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/members")
public class MemberRestController {

private final MemberCommandService memberCommandService;
private final MissionService missionService;

@PostMapping("/")
public ApiResponse<MemberResponseDTO.JoinResultDTO> join(@RequestBody @Valid MemberRequestDTO.JoinDto request){
Member member = memberCommandService.joinMember(request);
return ApiResponse.onSuccess(MemberConverter.toJoinResultDTO(member));
}

// 미션 도전 시작 API
@PostMapping("/{memberId}/missions/{missionId}/start")
public ResponseEntity<ApiResponse<String>> startMission(
@PathVariable Long memberId, @PathVariable Long missionId) {

try {
missionService.startMission(memberId, missionId); // MissionService에서 도전 시작 처리
// 미션 도전 성공 시 응답
return ResponseEntity.ok(ApiResponse.onSuccess("미션 도전이 시작되었습니다."));
} catch (GeneralException e) {
// 이미 도전 중인 미션인 경우
ApiResponse<String> errorResponse = ApiResponse.onFailure("BAD_REQUEST", "이미 도전 중인 미션입니다.", null);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
} catch (Exception e) {
// 기타 서버 오류 발생 시
ApiResponse<String> errorResponse = ApiResponse.onFailure("INTERNAL_SERVER_ERROR", "서버 오류: " + e.getMessage(), null);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package javalab.umc7th_mission.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import umc.spring.dto.MissionRequestDTO;
import umc.spring.dto.MissionResponseDTO;
import umc.spring.service.MissionService.MissionService;

import jakarta.validation.Valid;

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

private final MissionService missionService;

@PostMapping
public ResponseEntity<?> addMission(@RequestBody @Valid MissionRequestDTO request) {
try {
// 성공적으로 미션을 추가한 경우
MissionResponseDTO response = missionService.addMission(request);
return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
// 클라이언트 요청 오류
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (Exception e) {
// 서버 내부 오류
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("서버 에러: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package javalab.umc7th_mission.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import umc.spring.dto.ReviewRequestDTO;
import umc.spring.dto.ReviewResponseDTO;
import umc.spring.service.ReviewService.ReviewService;

@RestController
@RequestMapping("/review")
@RequiredArgsConstructor
public class ReviewController {

private final ReviewService reviewService;

// 리뷰 추가 API
@PostMapping
public ResponseEntity<ReviewResponseDTO> addReview(@RequestBody ReviewRequestDTO request) {
ReviewResponseDTO response = reviewService.addReview(request);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package javalab.umc7th_mission.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import umc.spring.apiPayload.ApiResponse;
import umc.spring.converter.StoreConverter;
import umc.spring.domain.Store;
import umc.spring.dto.StoreRequestDTO;
import umc.spring.dto.StoreResponseDTO;
import umc.spring.service.StoreService.StoreService;

@RestController
@RequestMapping("/stores")
@RequiredArgsConstructor
public class StoreController {

private final StoreService storeService;

@PostMapping("/")
public ApiResponse<StoreResponseDTO> addStore(@RequestBody @Valid StoreRequestDTO request) {
Store store = storeService.addStore(request);
return ApiResponse.onSuccess(StoreConverter.toStoreResponseDTO(store)); // 응답 DTO로 변환하여 반환
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.spring.controller;
package javalab.umc7th_mission.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package umc.spring.converter;

import umc.spring.domain.Member;
import umc.spring.domain.enums.Gender;
import umc.spring.dto.MemberRequestDTO;
import umc.spring.dto.MemberResponseDTO;

import java.time.LocalDateTime;
import java.util.ArrayList;

public class MemberConverter {

public static MemberResponseDTO.JoinResultDTO toJoinResultDTO(Member member){
return MemberResponseDTO.JoinResultDTO.builder()
.memberId(member.getId())
.createdAt(LocalDateTime.now())
.build();
}

public static Member toMember(MemberRequestDTO.JoinDto request){

Gender gender = null;

switch (request.getGender()){
case 1:
gender = Gender.MALE;
break;
case 2:
gender = Gender.FEMALE;
break;
case 3:
gender = Gender.NONE;
break;
}

return Member.builder()
.address(request.getAddress())
.specAddress(request.getSpecAddress())
.gender(gender)
.name(request.getName())
.memberPreferList(new ArrayList<>())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package umc.spring.converter;

import umc.spring.domain.foodCategory;
import umc.spring.domain.mapping.MemberPrefer;

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

public class MemberPreferConverter {

public static List<MemberPrefer> toMemberPreferList(List<foodCategory> foodCategoryList){

return foodCategoryList.stream()
.map(foodCategory ->
MemberPrefer.builder()
.foodCategory(foodCategory)
.build()
).collect(Collectors.toList());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package umc.spring.converter;

import umc.spring.domain.Mission;
import umc.spring.dto.MissionResponseDTO;

public class MissionConverter {
public static MissionResponseDTO toMissionResponseDTO(Mission mission) {
return MissionResponseDTO.builder()
.id(mission.getId())
.missionSpec(mission.getMissionSpec())
.reward(mission.getReward())
.deadline(mission.getDeadline())
.storeName(mission.getStore().getName())
.build();
}
}
26 changes: 26 additions & 0 deletions src/main/java/javalab/umc7th_mission/converter/StoreConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package umc.spring.converter;

import umc.spring.domain.Region;
import umc.spring.domain.Store;
import umc.spring.dto.StoreRequestDTO;
import umc.spring.dto.StoreResponseDTO;

public class StoreConverter {
public static Store toStore(StoreRequestDTO request, Region region) {
return Store.builder()
.name(request.getName())
.address(request.getAddress())
.region(region)
.score(request.getScore()) // score 값 추가
.build();
}

public static StoreResponseDTO toStoreResponseDTO(Store store) {
return StoreResponseDTO.builder()
.id(store.getId())
.name(store.getName())
.address(store.getAddress())
.regionName(store.getRegion().getName()) // 지역 이름도 포함
.build();
}
}
32 changes: 32 additions & 0 deletions src/main/java/javalab/umc7th_mission/dto/MemberRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package umc.spring.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import umc.spring.validation.annotation.ExistCategories;

import java.util.List;

public class MemberRequestDTO {

@Getter
public static class JoinDto{
@NotBlank
String name;
@NotNull
Integer gender;
@NotNull
Integer birthYear;
@NotNull
Integer birthMonth;
@NotNull
Integer birthDay;
@Size(min = 5, max = 12)
String address;
@Size(min = 5, max = 12)
String specAddress;
@ExistCategories
List<Long> preferCategory;
}
}
20 changes: 20 additions & 0 deletions src/main/java/javalab/umc7th_mission/dto/MemberResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package umc.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

public class MemberResponseDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinResultDTO{
Long memberId;
LocalDateTime createdAt;
}
}
15 changes: 15 additions & 0 deletions src/main/java/javalab/umc7th_mission/dto/MissionRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package umc.spring.dto;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
public class MissionRequestDTO {
private String missionSpec;
private int reward;
private LocalDate deadline;
private Long storeId;
}
18 changes: 18 additions & 0 deletions src/main/java/javalab/umc7th_mission/dto/MissionResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package umc.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;

@Getter
@AllArgsConstructor
@Builder
public class MissionResponseDTO {
private Long id;
private String missionSpec;
private int reward;
private LocalDate deadline;
private String storeName;
}
Loading