Skip to content

Commit

Permalink
feat/notification-delete-1: /id 로 알림 삭제 엔드포인트를 만들자 (#180)
Browse files Browse the repository at this point in the history
* fix/community-1: 게시글 조회 응답 DTO에 댓글 갯수를 표시한다.
- 단일 게시글 조회 시 / 게시글 그룹별 조회 시 댓글 갯수는 응답 DTO에 포함된다.

* fix/community-2: 댓글 조회시 본인이 종속되어 있는 게시글의 정보가 응답 DTO에 포함된다.
- 8/10 이후에 응답 DTO 초기값 정할 지 여부 결정 예정

* fix/member-1: 회원정보 각각의 필드값에 따른 수정을 가능하게 하자

- Set형태였던 hobbies, languages들을 null 값이 아닐 때에만 수정이 들어가며 null일 때에는 기존 값들이 들어가도록 함
- 기존의 /id API로 들어갔던 수정 메서드를 토큰 인증을 이용한 API로 수정함

* feat/notification-delete-1: /id 로 알림 삭제 엔드포인트를 만들자

- NotificationController, NotificationService:
- 알림 "읽었음"의 처리는 알림 "삭제" 와 동일함
- 따라서 좋아요 취소 때와 동일하게 @Deletemapping으로 구현

- 알림을 갖고 있는 회원과 삭제하려는 회원이 동일해야 하며 위배 시 401 에러를 발생시킴
- 이때 검사는 Notification이 종속되어 있는 NotificationToken 레포지토리에서 진행함
- 삭제 자격 통과 시 ID 값을 비교한 후 삭제 진행

* feat/notification-delete-2: JWT 로그인 실패는 JWT Filter에서 처리해주고 있으므로 외의 모든 에러는 403 에러를 내뱉게 수정한다.
  • Loading branch information
KooSuYeon authored Aug 8, 2024
1 parent ec2c186 commit bde1994
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/main/java/com/dife/api/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(MemberException.class)
public ResponseEntity<ExceptionResonse> handleMemberException(MemberException exception) {
return ResponseEntity.status(UNAUTHORIZED.value())
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}

Expand All @@ -29,7 +29,7 @@ public ResponseEntity<ExceptionResonse> handleDuplicateException(DuplicateExcept
@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity<ExceptionResonse> handleBadCredentialsException(
BadCredentialsException exception) {
return ResponseEntity.status(UNAUTHORIZED.value())
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}

Expand All @@ -48,14 +48,14 @@ public ResponseEntity<ExceptionResonse> handleRegisterException(RegisterExceptio
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ExceptionResonse> handleIllegalArgumentException(
IllegalArgumentException exception) {
return ResponseEntity.status(UNAUTHORIZED.value())
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}

@ExceptionHandler(MemberNullException.class)
public ResponseEntity<ExceptionResonse> handleNullPointerException(
NullPointerException exception) {
return ResponseEntity.status(UNAUTHORIZED.value())
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}

Expand All @@ -69,7 +69,7 @@ public ResponseEntity<ExceptionResonse> handleConnectException(
@ExceptionHandler(PostNotFoundException.class)
public ResponseEntity<ExceptionResonse> handlePostNotFoundException(
PostNotFoundException exception) {
return ResponseEntity.status(UNAUTHORIZED.value())
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}

Expand All @@ -83,7 +83,7 @@ public ResponseEntity<ExceptionResonse> handleConnectException(
@ExceptionHandler(ConnectUnauthorizedException.class)
public ResponseEntity<ExceptionResonse> handleConnectException(
ConnectUnauthorizedException exception) {
return ResponseEntity.status(UNAUTHORIZED)
return ResponseEntity.status(FORBIDDEN)
.body(new ExceptionResonse(false, exception.getMessage()));
}

Expand Down Expand Up @@ -120,4 +120,10 @@ public ResponseEntity<ExceptionResonse> handleChatroomNotFoundException(
return ResponseEntity.status(BAD_REQUEST)
.body(new ExceptionResonse(false, exception.getMessage()));
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ExceptionResonse> handleRuntimeException(RuntimeException exception) {
return ResponseEntity.status(FORBIDDEN.value())
.body(new ExceptionResonse(false, exception.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dife.api.controller;

import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

import com.dife.api.model.dto.NotificationResponseDto;
import com.dife.api.model.dto.NotificationTokenRequestDto;
Expand Down Expand Up @@ -34,6 +35,12 @@ public ResponseEntity<List<NotificationResponseDto>> getNotifications(Authentica
return ResponseEntity.ok(responseDtos);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteNotification(@PathVariable("id") Long id, Authentication auth) {
notificationService.deleteNotification(id, auth.getName());
return new ResponseEntity<>(OK);
}

@PostMapping("/push")
public ResponseEntity<NotificationTokenResponseDto> createNotificationToken(
@RequestBody NotificationTokenRequestDto requestDto, Authentication auth) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dife.api.exception;

public class NotificationAuthorizationException extends RuntimeException {

public NotificationAuthorizationException() {
super("알림 편집 자격이 없습니다!");
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/dife/api/repository/NotificationRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dife.api.repository;

import com.dife.api.model.Notification;
import com.dife.api.model.NotificationToken;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {

boolean existsByNotificationToken(NotificationToken notificationToken);
}
21 changes: 21 additions & 0 deletions src/main/java/com/dife/api/service/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.stream.Collectors.toList;

import com.dife.api.exception.MemberNotFoundException;
import com.dife.api.exception.NotificationAuthorizationException;
import com.dife.api.exception.NotificationException;
import com.dife.api.model.Member;
import com.dife.api.model.Notification;
Expand All @@ -12,6 +13,7 @@
import com.dife.api.model.dto.NotificationTokenRequestDto;
import com.dife.api.model.dto.NotificationTokenResponseDto;
import com.dife.api.repository.MemberRepository;
import com.dife.api.repository.NotificationRepository;
import com.dife.api.repository.NotificationTokenRepository;
import java.util.HashMap;
import java.util.List;
Expand All @@ -35,6 +37,7 @@ public class NotificationService {

private final MemberRepository memberRepository;
private final NotificationTokenRepository notificationTokenRepository;
private final NotificationRepository notificationRepository;

public NotificationTokenResponseDto sendNotificationToken(
String memberEmail, NotificationTokenRequestDto requestDto) {
Expand Down Expand Up @@ -113,4 +116,22 @@ public void addNotifications(
}
}
}

public void deleteNotification(Long id, String memberEmail) {
Member member =
memberRepository.findByEmail(memberEmail).orElseThrow(MemberNotFoundException::new);
List<NotificationToken> notifications = notificationTokenRepository.findAllByMember(member);

for (NotificationToken notificationToken : notifications) {
if (!notificationRepository.existsByNotificationToken(notificationToken))
throw new NotificationAuthorizationException();
for (Notification notification : notificationToken.getNotifications()) {
if (Objects.equals(notification.getId(), id)) {
notificationToken.getNotifications().remove(notification);
notificationRepository.delete(notification);
break;
}
}
}
}
}

0 comments on commit bde1994

Please sign in to comment.