diff --git a/src/main/java/side/onetime/controller/MemberController.java b/src/main/java/side/onetime/controller/MemberController.java index 819763d..0901ad7 100644 --- a/src/main/java/side/onetime/controller/MemberController.java +++ b/src/main/java/side/onetime/controller/MemberController.java @@ -14,6 +14,15 @@ public class MemberController { private final MemberService memberService; + // 멤버 등록 API + @PostMapping("/action-register") + public ResponseEntity> registerMember( + @RequestBody MemberDto.RegisterMemberRequest registerMemberRequest) { + + MemberDto.RegisterMemberResponse registerMemberResponse = memberService.registerMember(registerMemberRequest); + return ApiResponse.onSuccess(SuccessStatus._REGISTER_MEMBER, registerMemberResponse); + } + // 멤버 로그인 API @PostMapping("/action-login") public ResponseEntity> loginMember( diff --git a/src/main/java/side/onetime/dto/MemberDto.java b/src/main/java/side/onetime/dto/MemberDto.java index 8a63086..8cf6145 100644 --- a/src/main/java/side/onetime/dto/MemberDto.java +++ b/src/main/java/side/onetime/dto/MemberDto.java @@ -10,6 +10,7 @@ import side.onetime.domain.Event; import side.onetime.domain.Member; +import java.util.List; import java.util.UUID; public class MemberDto { @@ -19,10 +20,11 @@ public class MemberDto { @AllArgsConstructor @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) @JsonInclude(JsonInclude.Include.NON_NULL) - public static class LoginMemberRequest { + public static class RegisterMemberRequest { private String eventId; private String name; private String pin; + private List schedules; public Member to(Event event) { return Member.builder() @@ -34,6 +36,47 @@ public Member to(Event event) { } } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class RegisterMemberResponse { + private String memberId; + private String category; + + public static MemberDto.RegisterMemberResponse of(Member member, Event event) { + return RegisterMemberResponse.builder() + .memberId(String.valueOf(member.getMemberId())) + .category(event.getCategory().name()) + .build(); + } + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class Schedule { + private String timePoint; + private List times; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class LoginMemberRequest { + private String eventId; + private String name; + private String pin; + } + @Builder @Getter @NoArgsConstructor diff --git a/src/main/java/side/onetime/exception/MemberErrorResult.java b/src/main/java/side/onetime/exception/MemberErrorResult.java index d4c5936..711c226 100644 --- a/src/main/java/side/onetime/exception/MemberErrorResult.java +++ b/src/main/java/side/onetime/exception/MemberErrorResult.java @@ -11,6 +11,7 @@ public enum MemberErrorResult implements BaseErrorCode { _NOT_FOUND_MEMBER(HttpStatus.NOT_FOUND, "404", "멤버를 찾을 수 없습니다."), _IS_EXISTED_NAME(HttpStatus.CONFLICT, "409", "이미 존재하는 이름입니다."), + _IS_ALREADY_REGISTERED(HttpStatus.CONFLICT, "409", "이미 등록된 멤버입니다."), _NOT_FOUND_MEMBERS(HttpStatus.NOT_FOUND, "404", "멤버 전체 목록을 가져오는 데 실패했습니다.") ; diff --git a/src/main/java/side/onetime/global/common/constant/SuccessStatus.java b/src/main/java/side/onetime/global/common/constant/SuccessStatus.java index b41377c..7802948 100644 --- a/src/main/java/side/onetime/global/common/constant/SuccessStatus.java +++ b/src/main/java/side/onetime/global/common/constant/SuccessStatus.java @@ -16,6 +16,7 @@ public enum SuccessStatus implements BaseCode { _CREATED_EVENT(HttpStatus.CREATED, "201", "이벤트 생성에 성공했습니다."), _GET_EVENT(HttpStatus.OK, "200", "이벤트 조회에 성공했습니다."), // Member + _REGISTER_MEMBER(HttpStatus.CREATED, "201", "멤버 등록에 성공했습니다."), _LOGIN_MEMBER(HttpStatus.OK, "200", "멤버 로그인에 성공했습니다."), _IS_POSSIBLE_NAME(HttpStatus.OK, "200", "멤버 이름 중복 확인에 성공했습니다."), // Schedule diff --git a/src/main/java/side/onetime/repository/MemberRepository.java b/src/main/java/side/onetime/repository/MemberRepository.java index 97915af..f473b55 100644 --- a/src/main/java/side/onetime/repository/MemberRepository.java +++ b/src/main/java/side/onetime/repository/MemberRepository.java @@ -11,7 +11,6 @@ import java.util.UUID; public interface MemberRepository extends JpaRepository { - Boolean existsByEventAndNameAndPin(Event event, String name, String pin); Boolean existsByEventAndName(Event event, String name); Optional findByEventAndNameAndPin(Event event, String name, String pin); Optional findByMemberId(UUID memberId); diff --git a/src/main/java/side/onetime/service/MemberService.java b/src/main/java/side/onetime/service/MemberService.java index f2bc8ed..0af5104 100644 --- a/src/main/java/side/onetime/service/MemberService.java +++ b/src/main/java/side/onetime/service/MemberService.java @@ -5,14 +5,18 @@ import org.springframework.stereotype.Service; import side.onetime.domain.Event; import side.onetime.domain.Member; +import side.onetime.domain.Schedule; +import side.onetime.domain.Selection; import side.onetime.dto.MemberDto; -import side.onetime.exception.EventErrorResult; -import side.onetime.exception.EventException; -import side.onetime.exception.MemberErrorResult; -import side.onetime.exception.MemberException; +import side.onetime.exception.*; +import side.onetime.global.common.constant.Category; import side.onetime.repository.EventRepository; import side.onetime.repository.MemberRepository; +import side.onetime.repository.ScheduleRepository; +import side.onetime.repository.SelectionRepository; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; @Service @@ -20,23 +24,85 @@ public class MemberService { private final EventRepository eventRepository; private final MemberRepository memberRepository; + private final SelectionRepository selectionRepository; + private final ScheduleRepository scheduleRepository; - // 멤버 로그인 메서드 + // 멤버 등록 메서드 @Transactional - public MemberDto.LoginMemberResponse loginMember(MemberDto.LoginMemberRequest loginMemberRequest) { - UUID eventId = UUID.fromString(loginMemberRequest.getEventId()); + public MemberDto.RegisterMemberResponse registerMember(MemberDto.RegisterMemberRequest registerMemberRequest) { + UUID eventId = UUID.fromString(registerMemberRequest.getEventId()); Event event = eventRepository.findByEventId(eventId) .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); - // 이미 있는 유저인 경우 반환, 없을 경우 생성 - Member member; - if (memberRepository.existsByEventAndNameAndPin(event, loginMemberRequest.getName(), loginMemberRequest.getPin())) { - member = memberRepository.findByEventAndNameAndPin(event, loginMemberRequest.getName(), loginMemberRequest.getPin()) - .orElseThrow(() -> new MemberException(MemberErrorResult._NOT_FOUND_MEMBER)); + if (memberRepository.existsByEventAndName(event, registerMemberRequest.getName())) { + throw new MemberException(MemberErrorResult._IS_ALREADY_REGISTERED); + } + + Member member = registerMemberRequest.to(event); + memberRepository.save(member); + + List selections; + if (event.getCategory().equals(Category.DAY)) { + selections = createMembersDaySelections(event, member, registerMemberRequest); } else { - member = loginMemberRequest.to(event); - memberRepository.save(member); + selections = createMembersDateSelections(event, member, registerMemberRequest); + } + selectionRepository.saveAll(selections); + + return MemberDto.RegisterMemberResponse.of(member, event); + } + + private List createMembersDaySelections(Event event, Member member, MemberDto.RegisterMemberRequest registerMemberRequest) { + List schedules = registerMemberRequest.getSchedules(); + List selections = new ArrayList<>(); + for (MemberDto.Schedule schedule : schedules) { + String day = schedule.getTimePoint(); + List times = schedule.getTimes(); + List selectedSchedules = scheduleRepository.findAllByEventAndDay(event, day) + .orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DAY_SCHEDULES)); + + for (Schedule selectedSchedule : selectedSchedules) { + if (times.contains(selectedSchedule.getTime())) { + selections.add(Selection.builder() + .member(member) + .schedule(selectedSchedule) + .build()); + } + } + } + return selections; + } + + private List createMembersDateSelections(Event event, Member member, MemberDto.RegisterMemberRequest registerMemberRequest) { + List schedules = registerMemberRequest.getSchedules(); + List selections = new ArrayList<>(); + for (MemberDto.Schedule schedule : schedules) { + String date = schedule.getTimePoint(); + List times = schedule.getTimes(); + List selectedSchedules = scheduleRepository.findAllByEventAndDate(event, date) + .orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DATE_SCHEDULES)); + + for (Schedule selectedSchedule : selectedSchedules) { + if (times.contains(selectedSchedule.getTime())) { + selections.add(Selection.builder() + .member(member) + .schedule(selectedSchedule) + .build()); + } + } } + return selections; + } + + // 멤버 로그인 메서드 + public MemberDto.LoginMemberResponse loginMember(MemberDto.LoginMemberRequest loginMemberRequest) { + UUID eventId = UUID.fromString(loginMemberRequest.getEventId()); + Event event = eventRepository.findByEventId(eventId) + .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); + + Member member = memberRepository.findByEventAndNameAndPin(event, loginMemberRequest.getName(), loginMemberRequest.getPin()) + .orElseThrow(() -> new MemberException(MemberErrorResult._NOT_FOUND_MEMBER)); + return MemberDto.LoginMemberResponse.of(member, event); }