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 일부 생성 #25

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -4,9 +4,11 @@
import org.kkumulkkum.server.exception.AuthException;
import org.kkumulkkum.server.exception.BusinessException;
import org.kkumulkkum.server.exception.MeetingException;
import org.kkumulkkum.server.exception.ParticipantException;
import org.kkumulkkum.server.exception.code.AuthErrorCode;
import org.kkumulkkum.server.exception.code.BusinessErrorCode;
import org.kkumulkkum.server.exception.code.MeetingErrorCode;
import org.kkumulkkum.server.exception.code.ParticipantErrorCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -34,6 +36,14 @@ public ResponseEntity<AuthErrorCode> handleAuthException(AuthException e) {
.body(e.getErrorCode());
}

@ExceptionHandler(value = {ParticipantException.class})
public ResponseEntity<ParticipantErrorCode> handleParticipantException(ParticipantException e) {
log.error("GlobalExceptionHandler catch ParticipantException : {}", e.getErrorCode().getMessage());
return ResponseEntity
.status(e.getErrorCode().getHttpStatus())
.body(e.getErrorCode());
}

// 도메인 관련된 에러가 아닐 경우
@ExceptionHandler(value = {BusinessException.class})
public ResponseEntity<BusinessErrorCode> handleBusinessException(BusinessException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.kkumulkkum.server.controller;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.annotation.UserId;
import org.kkumulkkum.server.service.participant.ParticipantService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class ParticipantController {

private final ParticipantService participantService;

@PatchMapping("/promises/{promiseId}/preperation")
public ResponseEntity<Void> preparePromise(
@UserId Long userId,
@PathVariable Long promiseId
) {
participantService.preparePromise(userId, promiseId);
return ResponseEntity.ok().build();
}

@PatchMapping("/promises/{promiseId}/departure")
public ResponseEntity<Void> departurePromise(
@UserId Long userId,
@PathVariable Long promiseId
) {
participantService.departurePromise(userId, promiseId);
return ResponseEntity.ok().build();
}

@PatchMapping("/promises/{promiseId}/arrival")
public ResponseEntity<Void> arrivalPromise(
@UserId Long userId,
@PathVariable Long promiseId
) {
participantService.arrivalPromise(userId, promiseId);
return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.kkumulkkum.server.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class PromiseController {

}
12 changes: 12 additions & 0 deletions src/main/java/org/kkumulkkum/server/domain/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ public Participant(LocalDateTime preparationStartAt,
this.promise = promise;
this.member = member;
}

public void preparePromise() {
this.preparationStartAt = LocalDateTime.now();
}

public void departurePromise() {
this.departureAt = LocalDateTime.now();
}

public void arrivalPromise() {
this.arrivalAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.kkumulkkum.server.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.exception.code.ParticipantErrorCode;

@Getter
@RequiredArgsConstructor
public class ParticipantException extends RuntimeException {
private final ParticipantErrorCode errorCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kkumulkkum.server.exception.code;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum ParticipantErrorCode implements DefaultErrorCode{
// 400 BAD_REQUEST
NOT_JOINED_PROMISE(HttpStatus.BAD_REQUEST, 40040, "참여하지 않은 약속입니다."),
INVALID_STATE(HttpStatus.BAD_REQUEST, 40041, "유효하지 않은 상태 변경입니다."),
;

private HttpStatus httpStatus;
private int code;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

import java.util.Optional;

public interface ParticipantRepository extends JpaRepository<Participant, Long> {

@Query("SELECT p FROM Participant p " +
"JOIN Member m ON p.member.id = m.id " +
"WHERE p.promise.id = :promiseId AND m.user.id = :userId")
Optional<Participant> findByPromiseIdAndUserId(Long promiseId, Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.kkumulkkum.server.service.participant;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Participant;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ParticipantEditor {
Copy link
Member

Choose a reason for hiding this comment

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

우와 ParticipantEditor를 따로 빼서 만들어주신 이유가 있나요??
혹시 이것도 saver, retriever, remover 와 비슷한 역할을 하는 건지 궁금합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

엇 맞습니다. Editor 또한 같은 역할을 의도하고 작성한 것이 맞습니다.


public void preparePromise(final Participant participant) {
participant.preparePromise();
}

public void departurePromise(final Participant participant) {
participant.departurePromise();
}

public void arrivalPromise(final Participant participant) {
participant.arrivalPromise();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.kkumulkkum.server.service.participant;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Participant;
import org.kkumulkkum.server.exception.ParticipantException;
import org.kkumulkkum.server.exception.code.ParticipantErrorCode;
import org.kkumulkkum.server.repository.ParticipantRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ParticipantRetriever {

private final ParticipantRepository participantRepository;

public Participant findByPromiseIdAndUserId(Long promiseId, Long userId) {
return participantRepository.findByPromiseIdAndUserId(promiseId, userId)
.orElseThrow(() -> new ParticipantException(ParticipantErrorCode.NOT_JOINED_PROMISE));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
package org.kkumulkkum.server.service.participant;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kkumulkkum.server.domain.Participant;
import org.kkumulkkum.server.exception.ParticipantException;
import org.kkumulkkum.server.exception.code.ParticipantErrorCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class ParticipantService {

private final ParticipantRetriever participantRetriever;
private final ParticipantEditor participantEditor;

@Transactional
public void preparePromise(final Long userId, final Long promiseId) {
Participant participant = participantRetriever.findByPromiseIdAndUserId(promiseId, userId);
if (!validateState(participant, "preperation")) {
throw new ParticipantException(ParticipantErrorCode.INVALID_STATE);
}
participantEditor.preparePromise(participant);
}

@Transactional
public void departurePromise(final Long userId, final Long promiseId) {
Participant participant = participantRetriever.findByPromiseIdAndUserId(promiseId, userId);
if (!validateState(participant, "departure")) {
throw new ParticipantException(ParticipantErrorCode.INVALID_STATE);
}
participantEditor.departurePromise(participant);
}

@Transactional
public void arrivalPromise(final Long userId, final Long promiseId) {
Participant participant = participantRetriever.findByPromiseIdAndUserId(promiseId, userId);
if (!validateState(participant, "arrival")) {
throw new ParticipantException(ParticipantErrorCode.INVALID_STATE);
}
participantEditor.arrivalPromise(participant);
}

private boolean validateState(final Participant participant, final String status) {
switch (status) {
case "preperation":
return isNull(participant.getPreparationStartAt())
&& isNull(participant.getDepartureAt())
&& isNull(participant.getArrivalAt());
case "departure":
return isNotNull(participant.getPreparationStartAt())
&& isNull(participant.getDepartureAt())
&& isNull(participant.getArrivalAt());
case "arrival":
return isNotNull(participant.getPreparationStartAt())
&& isNotNull(participant.getDepartureAt())
&& isNull(participant.getArrivalAt());
default:
throw new IllegalArgumentException("Unknown status");
}
}

private boolean isNull(LocalDateTime time) {
return time == null;
}

private boolean isNotNull(LocalDateTime time) {
return time != null;
}

}
Loading