Skip to content

Commit

Permalink
Merge pull request #16 from onetime-with-members/feature/#15/member-r…
Browse files Browse the repository at this point in the history
…efactoring

[Refactor] : 멤버 등록 관련 리팩토링 완료
  • Loading branch information
bbbang105 authored Aug 19, 2024
2 parents 23dd087 + deb7c1d commit 0fd0cba
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/main/java/side/onetime/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
public class MemberController {
private final MemberService memberService;

// 멤버 등록 API
@PostMapping("/action-register")
public ResponseEntity<ApiResponse<MemberDto.RegisterMemberResponse>> registerMember(
@RequestBody MemberDto.RegisterMemberRequest registerMemberRequest) {

MemberDto.RegisterMemberResponse registerMemberResponse = memberService.registerMember(registerMemberRequest);
return ApiResponse.onSuccess(SuccessStatus._REGISTER_MEMBER, registerMemberResponse);
}

// 멤버 로그인 API
@PostMapping("/action-login")
public ResponseEntity<ApiResponse<MemberDto.LoginMemberResponse>> loginMember(
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/side/onetime/dto/MemberDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<MemberDto.Schedule> schedules;

public Member to(Event event) {
return Member.builder()
Expand All @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "멤버 전체 목록을 가져오는 데 실패했습니다.")
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.UUID;

public interface MemberRepository extends JpaRepository<Member, Long> {
Boolean existsByEventAndNameAndPin(Event event, String name, String pin);
Boolean existsByEventAndName(Event event, String name);
Optional<Member> findByEventAndNameAndPin(Event event, String name, String pin);
Optional<Member> findByMemberId(UUID memberId);
Expand Down
94 changes: 80 additions & 14 deletions src/main/java/side/onetime/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,104 @@
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
@RequiredArgsConstructor
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<Selection> 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<Selection> createMembersDaySelections(Event event, Member member, MemberDto.RegisterMemberRequest registerMemberRequest) {
List<MemberDto.Schedule> schedules = registerMemberRequest.getSchedules();
List<Selection> selections = new ArrayList<>();
for (MemberDto.Schedule schedule : schedules) {
String day = schedule.getTimePoint();
List<String> times = schedule.getTimes();
List<Schedule> 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<Selection> createMembersDateSelections(Event event, Member member, MemberDto.RegisterMemberRequest registerMemberRequest) {
List<MemberDto.Schedule> schedules = registerMemberRequest.getSchedules();
List<Selection> selections = new ArrayList<>();
for (MemberDto.Schedule schedule : schedules) {
String date = schedule.getTimePoint();
List<String> times = schedule.getTimes();
List<Schedule> 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);
}

Expand Down

0 comments on commit 0fd0cba

Please sign in to comment.