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

골 참여자 방출 기능 구현 #99

Merged
merged 32 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d6df41a
refacotr: exception 클래스명 리네이밍
jhsseonn Mar 29, 2024
7440c2d
refacotr: exception 클래스명 리네이밍
jhsseonn Mar 29, 2024
ce65227
feat: 골 참여자 방출 기능 구현
jhsseonn Mar 29, 2024
8097b98
feat: 골 참여자 방출 컨트롤러 기능 구현
jhsseonn Mar 29, 2024
c69a1d4
feat: 방출한 사용자의 모든 스탬프 삭제 기능 구현
jhsseonn Mar 29, 2024
4ee2bd6
test: 골 팀 삭제했을 경우 삭제 상태가 거짓인지 반환하는 테스트 추가
jhsseonn Mar 29, 2024
04042d2
docs: api 문서 업데이트
jhsseonn Mar 29, 2024
0d8fe1d
refactor: 골 참여자 방출 api 형식 변경
jhsseonn Mar 29, 2024
3100d79
docs: api 문서 업데이트
jhsseonn Mar 29, 2024
5b4f9e0
docs: api 문서 업데이트
jhsseonn Mar 29, 2024
c3b55c5
docs: 논의사항 편집
jhsseonn Mar 29, 2024
157465a
refactor: 주석 제거
jhsseonn Apr 3, 2024
d070051
refactor: teams 팀원 제거 로직 수정
jhsseonn Apr 3, 2024
a3b8651
refactor: teams 팀원 제거 로직 수정
jhsseonn Apr 3, 2024
4996979
refactor: 골 참여자 방출 로직 수정
jhsseonn Apr 3, 2024
c8d2760
refactor: 골 참여자 방출 컨트롤러 로직 수정
jhsseonn Apr 3, 2024
1683ec2
refactor: 골 참여자 방출 메서드명 변경
jhsseonn Apr 3, 2024
4ca692c
refactor: 골 참여자 방출 메서드명 변경
jhsseonn Apr 3, 2024
07dc4bc
docs: api 문서 업데이트
jhsseonn Apr 3, 2024
c6517ad
refactor: #100 골 팀 골, 사용자 fk명 수정 (#101)
jhsseonn Mar 30, 2024
62ea675
ci: #89 Flyway 적용 (#95)
JJ503 Mar 31, 2024
42d3d58
docs: api 문서 업데이트
jhsseonn Mar 29, 2024
41c9d66
Merge branch 'develop' into feature/90
jhsseonn Apr 3, 2024
80fbb73
refactor: 골 참여자 방출시 hardDelete하도록 로직 변경
jhsseonn Apr 19, 2024
e3ff334
ci: 브랜치 최신화
jhsseonn Apr 19, 2024
13c9f2e
refactor: isDeletedFalse 삭제
jhsseonn May 3, 2024
560e6ca
refactor: 골 팀 hardDelete 로직 수정
jhsseonn May 6, 2024
80a9449
docs: api 문서 업데이트
jhsseonn May 6, 2024
f0daa20
docs: api 문서 업데이트
jhsseonn May 6, 2024
43a95f3
refactor: 테스트 필드명 변경
jhsseonn May 20, 2024
a8a8d1e
refactor: 객체 assertSotfly 외부에서 선언
jhsseonn May 20, 2024
4d35b0d
docs: api 문서 업데이트
jhsseonn May 20, 2024
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
6 changes: 6 additions & 0 deletions src/docs/asciidoc/goal.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ operation::goal-controller-test/요청한_내용으로_골을_수정한다[snipp
operation::goal-controller-test/골_초대를_수락한다[snippets='http-request,path-parameters,request-headers']
==== 응답
operation::goal-controller-test/골_초대를_수락한다[snippets='http-response']

=== 골 참여자 방출
==== 요청
operation::goal-controller-test/골_참여자를_방출한다[snippets='http-request,path-parameters,request-headers']
==== 응답
operation::goal-controller-test/골_참여자를_방출한다[snippets='http-response']
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum ExceptionMessage {
INVALID_GOAL_DAYS("골 날짜 수가 범위 밖입니다.(범위: 1~100)"),
INVALID_USERS_SIZE("골 참여자 수가 범위 밖입니다.(범위: 1~5명)"),
INVALID_USER_TO_PARTICIPATE("골에 참여할 수 없는 사용자입니다. 골에는 친구인 사용자만 초대할 수 있습니다."),
INVALID_USER_TO_DELETE("골 참여자가 아닌 사용자는 삭제할 수 없습니다."),
READ_GOAL_FORBIDDEN("골을 조회할 권한이 없습니다."),
INVALID_GOAL_ACCEPT("초대받은 골이 아닙니다."),
INVALID_GOAL_ACCEPT_MANAGER("골 관리자는 골 초대 수락을 할 수 없습니다."),
Expand All @@ -61,6 +62,8 @@ public enum ExceptionMessage {
MANAGER_GOAL_ACCEPT_INVALID("골 관리자는 골 수락을 할 수 없습니다."),
NOT_FOUND_MANAGER("골의 관리자를 찾을 수 없습니다."),
FORBIDDEN_USER_TO_READ_GOAL("골을 조회할 권한이 없습니다."),
FORBIDDEN_USER_TO_DELETE_USER("골 참여자를 삭제할 권한이 없습니다."),
NOT_FOUND_GOAL_TEAM("삭제할 골 팀이 존재하지 않습니다."),

// 콕 찌르기
SENDER_NOT_IN_GOAL_TEAM("콕 찌르기 요청자가 해당 골의 팀원이 아닙니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import com.backend.blooming.friend.application.exception.FriendAcceptanceForbiddenException;
import com.backend.blooming.friend.application.exception.FriendRequestNotAllowedException;
import com.backend.blooming.friend.application.exception.NotFoundFriendRequestException;
import com.backend.blooming.goal.application.exception.DeleteGoalForbiddenException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToDeleteException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToPokeException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToReadException;
import com.backend.blooming.goal.application.exception.InvalidGoalAcceptException;
import com.backend.blooming.goal.application.exception.InvalidGoalException;
import com.backend.blooming.goal.application.exception.NotFoundGoalException;
import com.backend.blooming.goal.application.exception.UpdateGoalForbiddenException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToUpdateException;
import com.backend.blooming.image.infrastructure.exception.UploadImageException;
import com.backend.blooming.notification.application.exception.NotFoundGoalManagerException;
import com.backend.blooming.report.application.exception.InvalidGoalReportException;
Expand Down Expand Up @@ -194,19 +194,19 @@ public ResponseEntity<ExceptionResponse> handleDeleteFriendForbiddenException(
.body(new ExceptionResponse(exception.getMessage()));
}

@ExceptionHandler(DeleteGoalForbiddenException.class)
@ExceptionHandler(ForbiddenGoalToDeleteException.class)
public ResponseEntity<ExceptionResponse> handleDeleteGoalForbiddenException(
final DeleteGoalForbiddenException exception, final HttpServletRequest request
final ForbiddenGoalToDeleteException exception, final HttpServletRequest request
) {
logWarn(exception, request);

return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new ExceptionResponse(exception.getMessage()));
}

@ExceptionHandler(UpdateGoalForbiddenException.class)
@ExceptionHandler(ForbiddenGoalToUpdateException.class)
public ResponseEntity<ExceptionResponse> handleUpdateGoalForbiddenException(
final UpdateGoalForbiddenException exception, final HttpServletRequest request
final ForbiddenGoalToUpdateException exception, final HttpServletRequest request
) {
logWarn(exception, request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import com.backend.blooming.goal.application.dto.ReadGoalDetailDto;
import com.backend.blooming.goal.application.dto.UpdateGoalDto;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToReadException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToUpdateException;
import com.backend.blooming.goal.application.exception.InvalidGoalException;
import com.backend.blooming.goal.application.exception.NotFoundGoalException;
import com.backend.blooming.goal.application.exception.UpdateGoalForbiddenException;
import com.backend.blooming.goal.application.exception.NotFoundGoalTeamException;
import com.backend.blooming.goal.domain.Goal;
import com.backend.blooming.goal.domain.GoalTeam;
import com.backend.blooming.goal.infrastructure.repository.GoalRepository;
import com.backend.blooming.goal.infrastructure.repository.GoalTeamRepository;
import com.backend.blooming.notification.application.NotificationService;
import com.backend.blooming.stamp.application.StampService;
import com.backend.blooming.stamp.domain.Stamp;
import com.backend.blooming.stamp.infrastructure.repository.StampRepository;
import com.backend.blooming.user.application.exception.NotFoundUserException;
Expand All @@ -35,7 +38,9 @@ public class GoalService {
private final UserRepository userRepository;
private final FriendRepository friendRepository;
private final StampRepository stampRepository;
private final GoalTeamRepository goalTeamRepository;
private final NotificationService notificationService;
private final StampService stampService;

public Long createGoal(final CreateGoalDto createGoalDto) {
final List<User> users = getUsers(createGoalDto.teamUserIds());
Expand Down Expand Up @@ -139,7 +144,7 @@ public ReadGoalDetailDto update(final Long userId, final Long goalId, final Upda

private void validateUserToUpdate(final Goal goal, final Long userId) {
if (!goal.isManager(userId)) {
throw new UpdateGoalForbiddenException.ForbiddenUserToUpdate();
throw new ForbiddenGoalToUpdateException.ForbiddenUserToUpdateGoalToUpdate();
}
}

Expand Down Expand Up @@ -171,4 +176,17 @@ public void acceptGoalRequest(final Long userId, final Long goalId) {
final Goal goal = getGoal(goalId);
goal.updateAccepted(user);
}

public ReadGoalDetailDto deleteUser(final Long deleteUserId, final Long goalId, final Long userId) {
Copy link
Member

Choose a reason for hiding this comment

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

당장 좋은 아이디어가 떠오르지 않지만, 추후 리팩터링 진행 시 어떻게 하면 쿼리를 줄일 수 있을지 함께 고민해 보면 좋을 것 같습니다.
(정말 줄일 수 없는 것이라면 그러한 결론도 괜찮고요!)
기록 겸 코멘트 달아둡니다.

final User user = getUser(deleteUserId);
final Goal goal = getGoal(goalId);
goal.deleteUser(user, userId);
final GoalTeam goalTeam = goalTeamRepository.findByUserIdAndGoalId(deleteUserId, goalId)
.orElseThrow(NotFoundGoalTeamException::new);
goalTeamRepository.delete(goalTeam);
stampService.deleteAllByGoalIdAndUserId(goalId, deleteUserId);
final List<Long> usersUploadedStamp = getUsersUploadedStamp(goal);

return ReadGoalDetailDto.of(goal, usersUploadedStamp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backend.blooming.goal.application.exception;

import com.backend.blooming.exception.BloomingException;
import com.backend.blooming.exception.ExceptionMessage;

public class ForbiddenGoalToDeleteException extends BloomingException {

private ForbiddenGoalToDeleteException(final ExceptionMessage exceptionMessage) {
super(exceptionMessage);
}

public static class ForbiddenUserToDeleteGoal extends ForbiddenGoalToDeleteException {

public ForbiddenUserToDeleteGoal() {
super(ExceptionMessage.DELETE_GOAL_FORBIDDEN);
}
}

public static class ForbiddenUserToDeleteUser extends ForbiddenGoalToDeleteException {

public ForbiddenUserToDeleteUser() {
super(ExceptionMessage.FORBIDDEN_USER_TO_DELETE_USER);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.backend.blooming.goal.application.exception;

import com.backend.blooming.exception.BloomingException;
import com.backend.blooming.exception.ExceptionMessage;

public class ForbiddenGoalToUpdateException extends BloomingException {

private ForbiddenGoalToUpdateException(final ExceptionMessage exceptionMessage) {
super(exceptionMessage);
}

public static class ForbiddenUserToUpdateGoalToUpdate extends ForbiddenGoalToUpdateException {

public ForbiddenUserToUpdateGoalToUpdate() {
super(ExceptionMessage.UPDATE_GOAL_FORBIDDEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ public InvalidInvalidUpdateEndDate() {
super(ExceptionMessage.INVALID_UPDATE_END_DATE);
}
}

public static class InvalidInvalidUserToDelete extends InvalidGoalException {

public InvalidInvalidUserToDelete() {
super(ExceptionMessage.INVALID_USER_TO_DELETE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.backend.blooming.exception.BloomingException;
import com.backend.blooming.exception.ExceptionMessage;

public class DeleteGoalForbiddenException extends BloomingException {
public class NotFoundGoalTeamException extends BloomingException {

public DeleteGoalForbiddenException() {
super(ExceptionMessage.DELETE_GOAL_FORBIDDEN);
public NotFoundGoalTeamException() {
super(ExceptionMessage.NOT_FOUND_GOAL_TEAM);
}
}

This file was deleted.

23 changes: 21 additions & 2 deletions src/main/java/com/backend/blooming/goal/domain/Goal.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.backend.blooming.goal.domain;

import com.backend.blooming.common.entity.BaseTimeEntity;
import com.backend.blooming.goal.application.exception.DeleteGoalForbiddenException;
import com.backend.blooming.goal.application.exception.ForbiddenGoalToDeleteException;
import com.backend.blooming.goal.application.exception.InvalidGoalAcceptException;
import com.backend.blooming.goal.application.exception.InvalidGoalException;
import com.backend.blooming.user.domain.User;
Expand Down Expand Up @@ -107,9 +107,10 @@ public void updateDeleted(final Long userId) {

private void validUserToDelete(final Long userId) {
if (!isManager(userId)) {
throw new DeleteGoalForbiddenException();
throw new ForbiddenGoalToDeleteException.ForbiddenUserToDeleteGoal();
}
}

public boolean isTeam(final User user) {
return teams.isTeam(user);
}
Expand Down Expand Up @@ -139,4 +140,22 @@ private void validateUserToAccept(final User user) {
public boolean isTeamAndAccepted(final User user) {
return teams.isTeamAndAccepted(user);
}

public void deleteUser(final User user, final Long managerId) {
validUserToDelete(user);
validUserToDeleteUser(managerId);
teams.deleteUser(user);
}

private void validUserToDelete(final User user) {
if (!this.isTeam(user)) {
throw new InvalidGoalException.InvalidInvalidUserToDelete();
}
}

private void validUserToDeleteUser(final Long userId) {
if (!isManager(userId)) {
throw new ForbiddenGoalToDeleteException.ForbiddenUserToDeleteUser();
}
}
Comment on lines +156 to +160
Copy link
Member

Choose a reason for hiding this comment

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

해당 부분은 생각하지 못했는데 좋네요! 👍

}
4 changes: 4 additions & 0 deletions src/main/java/com/backend/blooming/goal/domain/Teams.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public boolean isTeamAndAccepted(final User user) {
return goalTeams.stream()
.anyMatch(goalTeam -> goalTeam.getUser().equals(user) && goalTeam.isAccepted());
}

public void deleteUser(final User user) {
goalTeams.removeIf(goalTeam -> goalTeam.getUser().equals(user));
Copy link
Member

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,17 @@
package com.backend.blooming.goal.infrastructure.repository;

import com.backend.blooming.goal.domain.GoalTeam;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface GoalTeamRepository extends JpaRepository<GoalTeam, Long> {

@Query("""
SELECT gt
FROM GoalTeam gt
WHERE (gt.user.id = :userId AND gt.goal.id = :goalId)
""")
Optional<GoalTeam> findByUserIdAndGoalId(final Long userId, final Long goalId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,16 @@ public ResponseEntity<Void> acceptGoal(
return ResponseEntity.noContent()
.build();
}

@DeleteMapping(value = "/{goalId}/user/{userId}", headers = "X-API-VERSION=1")
public ResponseEntity<ReadGoalResponse> deleteUser(
@PathVariable("goalId") final Long goalId,
@PathVariable("userId") final Long userId,
@Authenticated AuthenticatedUser authenticatedUser
) {
final ReadGoalDetailDto readGoalDetailDto = goalService.deleteUser(userId, goalId, authenticatedUser.userId());
final ReadGoalResponse response = ReadGoalResponse.from(readGoalDetailDto);

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ private void validateUserToRead(final Goal goal, final User user) {
throw new ForbiddenStampToReadException();
}
}

public void deleteAllByGoalIdAndUserId(final Long goalId, final Long userId) {
final List<Stamp> stamps = stampRepository.findAllByGoalIdAndUserId(goalId, userId);
stamps.forEach(Stamp::delete);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/backend/blooming/stamp/domain/Stamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ private String processDefaultStampImageUrl(final String stampImageUrl) {

return stampImageUrl;
}

public void delete() {
this.deleted = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ SELECT EXISTS(
WHERE s.id = :stampId
""")
Optional<Stamp> findByIdAndFetchGoalAndUser(final Long stampId);

@Query("""
SELECT s
FROM Stamp s
WHERE s.goal.id = :goalId AND s.user.id = :userId
""")
List<Stamp> findAllByGoalIdAndUserId(final Long goalId, final Long userId);
Comment on lines +47 to +52
Copy link
Member

Choose a reason for hiding this comment

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

jpa 네이밍만으로 안 되기에 쿼리문을 사용한 것이 맞을까요?
궁금해서 여쭤봅니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

네 맞습니다! 네이밍만으로 조회가 불가하다고 판단해 쿼리문 사용했습니다!

}
Loading
Loading