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 구현 #99

Merged
merged 4 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 @@ -73,6 +73,16 @@ public ResponseEntity<MainPromisesDto> getUpcomingPromise(
return ResponseEntity.ok().body(promiseService.getUpcomingPromises(userId));
}

@IsParticipant(promiseIdParamIndex = 1)
@PutMapping("/v1/promises/{promiseId}")
public ResponseEntity<PromiseAddDto> updatePromise(
@UserId final Long userId,
@PathVariable final Long promiseId,
@Valid @RequestBody final PromiseCreateDto updatePromiseDto
) {
return ResponseEntity.ok(promiseService.updatePromise(userId, promiseId, updatePromiseDto));
}

@IsParticipant(promiseIdParamIndex = 0)
@DeleteMapping("/v1/promises/{promiseId}")
public ResponseEntity<Void> deletePromise(
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/kkumulkkum/server/domain/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,22 @@ public void complete() {
this.isCompleted = true;
}

public void updatePromise(
String name,
String placeName,
Double x,
Double y,
String address,
String roadAddress,
DressUpLevel dressUpLevel,
String penalty) {
this.name = name;
this.placeName = placeName;
this.x = x;
this.y = y;
this.address = address;
this.roadAddress = roadAddress;
this.dressUpLevel = dressUpLevel;
this.penalty = penalty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public enum MemberErrorCode implements DefaultErrorCode {
// 403 FORBIDDEN
NOT_JOINED_MEMBER(HttpStatus.FORBIDDEN, 40340, "모임에 참여하지 않은 회원입니다."),
// 404 NOT_FOUND
NOT_FOUND_MEMBER(HttpStatus.NOT_FOUND, 40440, "멤버를 찾을 수 없습니다.")
;

private HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ SELECT CASE WHEN EXISTS (
Member findByMeetingIdAndUserId(Long meetingId, Long userId);

List<Member> findByUserId(Long userId);

@Query("""
SELECT m FROM Member m
JOIN m.meeting mt
JOIN m.user u
JOIN Promise p ON p.meeting.id = mt.id
WHERE p.id = :promiseId AND u.id = :userId""")
Member findByUserIdAndPromiseId(Long userId, Long promiseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ SELECT COUNT(p) FROM Participant p
List<String> findFcmTokenByPromiseId(Long promiseId, Long userId);

void deleteByMemberId(Long memberId);

Participant findByMemberIdAndPromiseId(Long memberId, Long promiseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Member;
import org.kkumulkkum.server.dto.member.response.MemberDto;
import org.kkumulkkum.server.exception.MemberException;
import org.kkumulkkum.server.exception.code.MemberErrorCode;
import org.kkumulkkum.server.repository.MemberRepository;
import org.springframework.stereotype.Component;

Expand All @@ -14,6 +16,11 @@ public class MemberRetreiver {

private final MemberRepository memberRepository;

public Member findById(final Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(MemberErrorCode.NOT_FOUND_MEMBER));
}

public boolean existsByMeetingIdAndUserId(
final Long meetingId,
final Long userId
Expand All @@ -39,4 +46,11 @@ public Member findByMeetingIdAndUserId(Long meetingId, Long userId) {
public List<Member> findByUserId(final Long userId) {
return memberRepository.findByUserId(userId);
}

public Member findByUserIdAndPromiseId(
final Long userId,
final Long promiseId
) {
return memberRepository.findByUserIdAndPromiseId(userId, promiseId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ public List<String> findFcmTokenByPromiseId(final Long promiseId, final Long use
return participantRepository.findFcmTokenByPromiseId(promiseId, userId);
}

public Participant findByMemberIdAndPromiseId(final Long memberId, final Long promiseId) {
return participantRepository.findByMemberIdAndPromiseId(memberId, promiseId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kkumulkkum.server.service.promise;

import org.kkumulkkum.server.domain.Promise;
import org.kkumulkkum.server.dto.promise.PromiseCreateDto;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -9,4 +10,20 @@ public class PromiseEditor {
public void completePromise(final Promise promise) {
promise.complete();
}

public void updatePromise(
final Promise promise,
final PromiseCreateDto updatePromiseDto
) {
promise.updatePromise(
updatePromiseDto.name(),
updatePromiseDto.placeName(),
updatePromiseDto.x(),
updatePromiseDto.y(),
updatePromiseDto.address(),
updatePromiseDto.roadAddress(),
updatePromiseDto.dressUpLevel(),
updatePromiseDto.penalty()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ public MainPromisesDto getUpcomingPromises(final Long userId) {
return MainPromisesDto.from(promiseRetriever.findUpcomingPromises(userId, 4));
}

@Transactional
public PromiseAddDto updatePromise(
final Long userId,
final Long promiseId,
final PromiseCreateDto updatePromiseDto
) {
// 약속 정보 수정
Promise promise = promiseRetriever.findById(promiseId);
promiseEditor.updatePromise(promise, updatePromiseDto);

// 참여자 목록 수정
Member member = memberRetreiver.findByUserIdAndPromiseId(userId, promiseId);
updatePromiseDto.participants().add(member.getId());

List<Long> currentMembers = participantRetriever.findAllByPromiseId(promiseId).stream()
.map(participant -> participant.getMember().getId())
.toList();
List<Long> newMembers = updatePromiseDto.participants();

saveNewParticipants(promise, newMembers, currentMembers);
removeOldParticipants(promiseId, newMembers, currentMembers);

return PromiseAddDto.from(promise);
}

@Transactional
public void deletePromise(final Long promiseId) {
Promise promise = promiseRetriever.findById(promiseId);
Expand All @@ -173,4 +198,37 @@ private void updateUserInfo(
}
}

private void saveNewParticipants(
final Promise promise,
final List<Long> newMembers,
final List<Long> currentMembers
) {
participantSaver.saveAll(
newMembers.stream()
.filter(memberId -> !currentMembers.contains(memberId))
.map(memberId -> {
Member member = memberRetreiver.findById(memberId);
return Participant.builder()
.promise(promise)
.member(member)
.build();
}).toList()
);
}

private void removeOldParticipants(
final Long promiseId,
final List<Long> newMembers,
final List<Long> currentMembers
) {
participantRemover.deleteAll(
currentMembers.stream()
.filter(memberId -> !newMembers.contains(memberId))
.map(memberId -> {
memberRetreiver.findById(memberId);
return participantRetriever.findByMemberIdAndPromiseId(memberId, promiseId);
}).toList()
);
}

}
Loading