Skip to content

Commit

Permalink
[fix] 약속 완료 API 수정 (#45)
Browse files Browse the repository at this point in the history
* [fix] #41 add missing logic in Promise complete

* [fix] #41 fix tardySum's unit in UserDto

* [fix] #41 add missing implements

* [fix] #41 fix logic & typo
  • Loading branch information
tkdwns414 authored Jul 12, 2024
1 parent f60efcb commit 73fb3d5
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.annotation.IsMember;
import org.kkumulkkum.server.annotation.IsParticipant;
import org.kkumulkkum.server.annotation.UserId;
import org.kkumulkkum.server.dto.promise.PromiseCreateDto;
import org.kkumulkkum.server.dto.promise.response.PromiseDto;
Expand Down Expand Up @@ -31,12 +32,12 @@ public ResponseEntity<Void> createPromise(
return ResponseEntity.created(URI.create(promiseId.toString())).build();
}

@IsParticipant(promiseIdParamIndex = 0)
@PatchMapping("/promises/{promiseId}/completion")
public ResponseEntity<Void> completePromise(
@UserId Long userId,
@PathVariable Long promiseId
) {
promiseService.completePromise(userId, promiseId);
promiseService.completePromise(promiseId);
return ResponseEntity.ok().build();
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/kkumulkkum/server/domain/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,27 @@ public void deleteImage() {
public void updateName(String name) {
this.name = name;
}

public void addPromiseCount() {
this.promiseCount++;
}

public void addLateCount() {
this.tardyCount++;
}

public void addLateTime(long time) {
this.tardySum += time;
}

public void levelUp() {
int count = this.promiseCount - this.tardyCount;
if (count == 2) {
this.level = 2;
} else if (count == 5) {
this.level = 3;
} else if (count == 10) {
this.level = 4;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static UserDto from(UserInfo userInfo) {
userInfo.getLevel(),
userInfo.getPromiseCount(),
userInfo.getTardyCount(),
userInfo.getTardySum(),
userInfo.getTardySum()/60,
userInfo.getProfileImg()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

@Getter
@AllArgsConstructor
public enum PromiseErrorCode {
//404 Not Found
public enum PromiseErrorCode implements DefaultErrorCode {
// 400 Bad Request
NOT_PAST_DUE(HttpStatus.BAD_REQUEST, 40010, "약속 시간이 지나지 않았습니다."),
NOT_ARRIVED_PARTICIPANT_EXISTS(HttpStatus.BAD_REQUEST, 40020, "도착하지 않은 참여자가 있습니다."),
// 404 Not Found
NOT_FOUND_PROMISE(HttpStatus.NOT_FOUND, 40430, "약속을 찾을 수 없습니다."),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface ParticipantRepository extends JpaRepository<Participant, Long>
"JOIN Member m ON p.member.id = m.id " +
"JOIN UserInfo ui ON m.user.id = ui.user.id " +
"WHERE p.promise.id = :promiseId")
List<ParticipantStatusUserInfoDto> findAllByPromiseId(Long promiseId);
List<ParticipantStatusUserInfoDto> findAllByPromiseIdWithUserInfo(Long promiseId);

@Query("SELECT new org.kkumulkkum.server.dto.participant.response.LateComerDto " +
"(p.id, ui.name, ui.profileImg) " +
Expand All @@ -42,4 +42,6 @@ public interface ParticipantRepository extends JpaRepository<Participant, Long>
") THEN TRUE ELSE FALSE END " +
"FROM Participant p")
boolean existsByPromiseIdAndUserId(Long promiseId, Long userId);

List<Participant> findAllByPromiseId(Long promiseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import org.kkumulkkum.server.domain.Promise;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PromiseRepository extends JpaRepository<Promise, Long> {

List<Promise> findAllByMeetingId(Long meetingId);

@Query("SELECT CASE WHEN EXISTS " +
"(SELECT p FROM Participant p WHERE p.promise.id = :promiseId AND p.arrivalAt IS NULL) " +
"THEN TRUE ELSE FALSE END FROM Participant p")
boolean existsByArrivedAtIsNull(Long promiseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import org.kkumulkkum.server.domain.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface UserInfoRepository extends JpaRepository<UserInfo, Long> {

Optional<UserInfo> findByUserId(Long id);

@Query("SELECT ui FROM UserInfo ui " +
"JOIN Participant p ON ui.user.id = p.member.user.id " +
"WHERE p.id = :id")
Optional<UserInfo> findByParticipantId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public Participant findByPromiseIdAndUserId(Long promiseId, Long userId) {
.orElseThrow(() -> new ParticipantException(ParticipantErrorCode.NOT_JOINED_PROMISE));
}

public List<ParticipantStatusUserInfoDto> findAllByPromiseId(Long promiseId) {
public List<Participant> findAllByPromiseId(Long promiseId) {
return participantRepository.findAllByPromiseId(promiseId);
}

public List<ParticipantStatusUserInfoDto> findAllByPromiseIdWithUserInfo(Long promiseId) {
return participantRepository.findAllByPromiseIdWithUserInfo(promiseId);
}

public List<LateComerDto> findAllLateComersByPromiseId(Long promiseId) {
return participantRepository.findAllLateComersByPromiseId(promiseId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PreparationStatusDto getPreparation(final Long userId, final Long promise

@Transactional(readOnly = true)
public ParticipantsDto getParticipants(final Long promiseId) {
List<ParticipantStatusUserInfoDto> participants = participantRetriever.findAllByPromiseId(promiseId);
List<ParticipantStatusUserInfoDto> participants = participantRetriever.findAllByPromiseIdWithUserInfo(promiseId);
return ParticipantsDto.from(
participants.stream()
.map(this::createParticipantDto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public Promise findById(Long id) {
return promiseRepository.findById(id)
.orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE));
}

public boolean existsByArrivedAtIsNull(Long promiseId) {
return promiseRepository.existsByArrivedAtIsNull(promiseId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Meeting;
import org.kkumulkkum.server.domain.Member;
import org.kkumulkkum.server.domain.Participant;
import org.kkumulkkum.server.domain.Promise;
import org.kkumulkkum.server.domain.*;
import org.kkumulkkum.server.dto.promise.PromiseCreateDto;
import org.kkumulkkum.server.dto.promise.response.PromiseDto;
import org.kkumulkkum.server.dto.promise.response.PromisesDto;
import org.kkumulkkum.server.exception.PromiseException;
import org.kkumulkkum.server.exception.code.PromiseErrorCode;
import org.kkumulkkum.server.service.participant.ParticipantRetriever;
import org.kkumulkkum.server.service.participant.ParticipantSaver;
import org.kkumulkkum.server.service.member.MemberRetreiver;
import org.kkumulkkum.server.service.userInfo.UserInfoRetriever;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
Expand All @@ -24,6 +26,8 @@ public class PromiseService {
private final PromiseRetriever promiseRetriever;
private final PromiseEditor promiseEditor;
private final ParticipantSaver participantSaver;
private final ParticipantRetriever participantRetriever;
private final UserInfoRetriever userInfoRetriever;
private final EntityManager entityManager;

@Transactional
Expand Down Expand Up @@ -55,10 +59,19 @@ public Long createPromise(Long userId, Long meetingId, PromiseCreateDto createPr
}

@Transactional
public void completePromise(Long userId, Long promiseId) {
// TODO: PARTICIPANT 검증
public void completePromise(Long promiseId) {
Promise promise = promiseRetriever.findById(promiseId);
if (promise.getTime().isAfter(LocalDateTime.now())) {
throw new PromiseException(PromiseErrorCode.NOT_PAST_DUE);
}

if (promiseRetriever.existsByArrivedAtIsNull(promiseId)) {
throw new PromiseException(PromiseErrorCode.NOT_ARRIVED_PARTICIPANT_EXISTS);
}

promiseEditor.completePromise(promise);
List<Participant> participants = participantRetriever.findAllByPromiseId(promiseId);
participants.forEach(participant -> updateUserInfo(participant, promise.getTime()));
}

@Transactional(readOnly = true)
Expand All @@ -77,4 +90,16 @@ public PromiseDto getPromise(
Promise promise = promiseRetriever.findById(promiseId);
return PromiseDto.from(promise);
}

private void updateUserInfo(Participant participant, LocalDateTime promiseTime) {
UserInfo userInfo = userInfoRetriever.findByParticipantId(participant.getId());
userInfo.addPromiseCount();
if (promiseTime.isBefore(participant.getArrivalAt())) {
userInfo.addLateCount();
userInfo.addLateTime(Duration.between(promiseTime, participant.getArrivalAt()).getSeconds());
} else {
userInfo.levelUp();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public UserInfo findByUserId(final Long id) {
return userInfoRepository.findByUserId(id)
.orElseThrow(() -> new UserException(UserErrorCode.NOT_FOUND_USER));
}

public UserInfo findByParticipantId(final Long id) {
return userInfoRepository.findByParticipantId(id)
.orElseThrow(() -> new UserException(UserErrorCode.NOT_FOUND_USER));
}
}

0 comments on commit 73fb3d5

Please sign in to comment.