Skip to content

Commit

Permalink
🔀 :: (#265) 친구 삭제 Api
Browse files Browse the repository at this point in the history
  • Loading branch information
leeseojune53 authored Dec 31, 2022
2 parents 7802032 + b66e91b commit b5f153a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ Optional<Relation> findRelationBySendUserIdAndReceiveUserId(
Long sendUserId, Long receiveUserId);

boolean isFriend(Long currentUserId, Long userId);

Long getRelationIdByUserId(Long currentUserId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public boolean isFriend(Long currentUserId, Long userId) {
.fetchFirst();
}

@Override
public Long getRelationIdByUserId(Long currentUserId, Long userId) {
return queryFactory
.select(relation.id)
.from(relation)
.where(
friendPredicated(currentUserId, userId)
.or(friendPredicated(userId, currentUserId)))
.fetchFirst();
}

private BooleanExpression friendPredicated(Long senderUserId, Long receiveUserId) {
return relation.sendUser.id.eq(senderUserId).and(relation.receiveUser.id.eq(receiveUserId));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.depromeet.knockknockbackend.domain.relation.exception;


import io.github.depromeet.knockknockbackend.global.error.exception.ErrorCode;
import io.github.depromeet.knockknockbackend.global.error.exception.KnockException;

public class NotFriendRelationException extends KnockException {

public static final KnockException EXCEPTION = new NotFriendRelationException();

private NotFriendRelationException() {
super(ErrorCode.NOT_FRIEND_RELATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public ResponseEntity<Void> sendUserRequest(@RequestBody @Valid FriendRequest re
return new ResponseEntity<>(relationService.sendFriendRequest(request));
}

@Operation(summary = "친구를 삭제하는 Api입니다. - 친구목록")
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping
public void deleteRelation(@RequestBody @Valid FriendRequest request) {
relationService.deleteRelation(request);
}

@Operation(summary = "친구 요청을 수락하는 Api입니다. - 메인 알림")
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/requests")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.depromeet.knockknockbackend.domain.relation.exception.AlreadyFriendException;
import io.github.depromeet.knockknockbackend.domain.relation.exception.AlreadySendRequestException;
import io.github.depromeet.knockknockbackend.domain.relation.exception.FriendRequestNotFoundException;
import io.github.depromeet.knockknockbackend.domain.relation.exception.NotFriendRelationException;
import io.github.depromeet.knockknockbackend.domain.relation.presentation.dto.request.FriendRequest;
import io.github.depromeet.knockknockbackend.domain.relation.presentation.dto.response.QueryFriendListResponse;
import io.github.depromeet.knockknockbackend.domain.relation.presentation.dto.response.QueryFriendListResponseElement;
Expand Down Expand Up @@ -77,6 +78,14 @@ public HttpStatus sendFriendRequest(FriendRequest request) {
return HttpStatus.CREATED;
}

public void deleteRelation(FriendRequest request) {
if (!getIsFriend(request.getUserId())) {
throw NotFriendRelationException.EXCEPTION;
}

relationRepository.deleteById(getRelationId(request.getUserId()));
}

public void acceptRequest(FriendRequest request) {
updateIsFriendWithValidate(request, true);
}
Expand All @@ -89,6 +98,10 @@ public boolean getIsFriend(Long userId) {
return relationRepository.isFriend(SecurityUtils.getCurrentUserId(), userId);
}

private Long getRelationId(Long userId) {
return relationRepository.getRelationIdByUserId(SecurityUtils.getCurrentUserId(), userId);
}

private void updateIsFriendWithValidate(FriendRequest request, boolean isFriend) {
if (!getIsFriend(request.getUserId())) {
throw AlreadyFriendException.EXCEPTION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum ErrorCode {
ALREADY_SEND_REQUEST(400, "RELATION-400-1", "Already Send Request."),
ALREADY_FRIEND_REQUEST(400, "RELATION-400-1", "Already Friend Request."),
FRIEND_REQUEST_NOT_FOUND(404, "RELATION-404-1", "Friend Request Not Found."),
NOT_FRIEND_RELATION(404, "RELATION-404-2", "Not Friend Relation."),

OPTION_NOT_FOUND(404, "OPTION-404-1", "Option Not Found."),

Expand Down

0 comments on commit b5f153a

Please sign in to comment.