-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82ce59e
[feat] #24 create Participant Error & handle in exception handler
tkdwns414 ceb53fb
[feat] #24 create updating preperation time API
tkdwns414 00b44bd
[feat] #24 create updating departure time API
tkdwns414 9762aef
[feat] #24 create updating arrival time API
tkdwns414 55b6425
[feat] #24 mv APIs to appropriate controller
tkdwns414 76806f1
Merge branch 'develop' into feat/24
tkdwns414 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/org/kkumulkkum/server/controller/ParticipantController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/kkumulkkum/server/controller/PromiseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/org/kkumulkkum/server/exception/ParticipantException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/kkumulkkum/server/exception/code/ParticipantErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/kkumulkkum/server/service/participant/ParticipantEditor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
public void preparePromise(final Participant participant) { | ||
participant.preparePromise(); | ||
} | ||
|
||
public void departurePromise(final Participant participant) { | ||
participant.departurePromise(); | ||
} | ||
|
||
public void arrivalPromise(final Participant participant) { | ||
participant.arrivalPromise(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/kkumulkkum/server/service/participant/ParticipantRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/kkumulkkum/server/service/participant/ParticipantService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우와 ParticipantEditor를 따로 빼서 만들어주신 이유가 있나요??
혹시 이것도 saver, retriever, remover 와 비슷한 역할을 하는 건지 궁금합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엇 맞습니다. Editor 또한 같은 역할을 의도하고 작성한 것이 맞습니다.