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

[feat] 모임 수정, 탈퇴 API 구현 #101

Merged
merged 2 commits into from
Aug 22, 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 @@ -70,4 +70,24 @@ public ResponseEntity<MembersDto> getMembers(
return ResponseEntity.ok(meetingService.getMembers(meetingId, exclude, userId));
}

@IsMemberByMeetingId(meetingIdParamIndex = 0)
@PatchMapping("/v1/meetings/{meetingId}")
public ResponseEntity<Void> updateMeeting(
@PathVariable final Long meetingId,
@Valid @RequestBody final MeetingCreateDto meetingCreateDto
) {
meetingService.updateMeeting(meetingId, meetingCreateDto);
return ResponseEntity.ok().build();
}

@IsMemberByMeetingId(meetingIdParamIndex = 1)
@DeleteMapping("/v1/meetings/{meetingId}")
public ResponseEntity<Void> leaveMeeting(
@UserId final Long userId,
@PathVariable final Long meetingId
) {
meetingService.leaveMeeting(userId, meetingId);
return ResponseEntity.ok().build();
}

}
4 changes: 4 additions & 0 deletions src/main/java/org/kkumulkkum/server/domain/Meeting.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public Meeting(String name, String invitationCode) {
this.name = name;
this.invitationCode = invitationCode;
}

public void updateMeetingName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ public interface PromiseRepository extends JpaRepository<Promise, Long> {
WHERE pt.promise.id = :promiseId
AND m.user.id = :userId""")
Promise findByUserIdAndPromiseId(Long userId, Long promiseId);

void deleteByMeetingId(Long meetingId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kkumulkkum.server.service.meeting;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Meeting;
import org.kkumulkkum.server.dto.meeting.request.MeetingCreateDto;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MeetingEditor {

public void updateMeetingName(
final Meeting meeting,
final MeetingCreateDto meetingCreateDto
) {
meeting.updateMeetingName(meetingCreateDto.name());
}
Comment on lines +12 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DTO 문제는 아까 말했듯이 9월 되자마자 같이 살펴봐요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹 알겠습니다!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.kkumulkkum.server.service.meeting;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.repository.MeetingRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MeetingRemover {

private final MeetingRepository meetingRepository;

public void deleteById(final Long meetingId) {
meetingRepository.deleteById(meetingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import org.kkumulkkum.server.dto.member.response.MembersDto;
import org.kkumulkkum.server.exception.MeetingException;
import org.kkumulkkum.server.exception.code.MeetingErrorCode;
import org.kkumulkkum.server.service.member.MemberRemover;
import org.kkumulkkum.server.service.member.MemberRetreiver;
import org.kkumulkkum.server.service.member.MemberSaver;
import org.kkumulkkum.server.service.participant.ParticipantRemover;
import org.kkumulkkum.server.service.promise.PromiseRemover;
import org.kkumulkkum.server.service.user.UserRetriever;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -33,6 +36,11 @@ public class MeetingService {
private final UserRetriever userRetriever;
private final MemberSaver memberSaver;
private final MemberRetreiver memberRetreiver;
private final MeetingEditor meetingEditor;
private final ParticipantRemover participantRemover;
private final MemberRemover memberRemover;
private final PromiseRemover promiseRemover;
private final MeetingRemover meetingRemover;

@Transactional
public CreatedMeetingDto createMeeting(
Expand Down Expand Up @@ -95,6 +103,32 @@ public MembersDto getMembers(final Long meetingId, final String exclude, final L
return MembersDto.from(members);
}

@Transactional
public void updateMeeting(
final Long meetingId,
final MeetingCreateDto meetingCreateDto
) {
Meeting meeting = meetingRetriever.findById(meetingId);
meetingEditor.updateMeetingName(meeting, meetingCreateDto);
}

@Transactional
public void leaveMeeting(
final Long userId,
final Long meetingId) {
//모임 나가면 참여한 약속도 다 나가기
Member member = memberRetreiver.findByMeetingIdAndUserId(meetingId, userId);
participantRemover.deleteByMemberId(member.getId());
memberRemover.deleteById(member.getId());

// 모임 내 참여 인원이 전부 탈퇴 or 나가기로 없을 경우(모임 사라지면) 약속도 다 삭제하기
List<MemberDto> remainingMembers = memberRetreiver.findAllByMeetingId(meetingId);
if(remainingMembers.isEmpty()) {
promiseRemover.deleteByMeetingId(meetingId);
meetingRemover.deleteById(meetingId);
}
}

private String generateInvitationCode() {
String invitationCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public class MemberRemover {
public void deleteAll(final List<Member> members) {
memberRepository.deleteAll(members);
}

public void deleteById(final Long memberId) {
memberRepository.deleteById(memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public class PromiseRemover {
public void deleteById(final Long promiseId) {
promiseRepository.deleteById(promiseId);
}

public void deleteByMeetingId(final Long meetingId) {
promiseRepository.deleteByMeetingId(meetingId);
}
}
Loading