Skip to content

Commit 4335e86

Browse files
authored
Merge pull request #30 from 9roomMoa/feat/#26
Feat/#26: 공지 수정, 공지 삭제 api
2 parents 6fe763f + 07d4621 commit 4335e86

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

src/main/java/com/groommoa/aether_back_notification/domain/notices/controller/NoticeController.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import com.groommoa.aether_back_notification.domain.notices.dto.CreateNoticeRequestDto;
44
import com.groommoa.aether_back_notification.domain.notices.dto.CreateNoticeResponseDto;
55
import com.groommoa.aether_back_notification.domain.notices.dto.NoticeDto;
6+
import com.groommoa.aether_back_notification.domain.notices.dto.UpdateNoticeRequestDto;
67
import com.groommoa.aether_back_notification.domain.notices.entity.Notice;
78
import com.groommoa.aether_back_notification.domain.notices.service.NoticeService;
89
import com.groommoa.aether_back_notification.global.common.constants.HttpStatus;
910
import com.groommoa.aether_back_notification.global.common.response.CommonResponse;
1011
import com.groommoa.aether_back_notification.global.common.utils.DtoUtils;
1112
import io.jsonwebtoken.Claims;
13+
import jakarta.validation.Valid;
1214
import jakarta.validation.constraints.Max;
1315
import jakarta.validation.constraints.Min;
1416
import lombok.RequiredArgsConstructor;
@@ -53,4 +55,34 @@ public ResponseEntity<CommonResponse> getNotices(
5355
return ResponseEntity.ok(response);
5456
}
5557

58+
@PatchMapping("/{noticeId}")
59+
public ResponseEntity<CommonResponse> updateNotice(
60+
@AuthenticationPrincipal Claims claims,
61+
@PathVariable String noticeId,
62+
@Valid @RequestBody UpdateNoticeRequestDto request
63+
) {
64+
String requesterId = claims.getSubject();
65+
String content = request.getContent();
66+
NoticeDto updatedNotice = noticeService.updateNotice(requesterId, noticeId, content);
67+
68+
CommonResponse response = new CommonResponse(
69+
HttpStatus.OK, "공지 수정에 성공했습니다.", DtoUtils.toMap(updatedNotice));
70+
71+
return ResponseEntity.ok(response);
72+
}
73+
74+
@DeleteMapping("/{noticeId}")
75+
public ResponseEntity<CommonResponse> deleteNotice(
76+
@AuthenticationPrincipal Claims claims,
77+
@PathVariable String noticeId
78+
) {
79+
String requesterId = claims.getSubject();
80+
noticeService.deleteNotice(requesterId, noticeId);
81+
82+
CommonResponse response = new CommonResponse(
83+
HttpStatus.OK, "공지 삭제에 성공했습니다.", null);
84+
85+
return ResponseEntity.ok(response);
86+
}
87+
5688
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.groommoa.aether_back_notification.domain.notices.dto;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@Builder
8+
public class UpdateNoticeRequestDto {
9+
private final String content;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.groommoa.aether_back_notification.domain.notices.exception;
2+
3+
import com.groommoa.aether_back_notification.global.common.exception.CustomException;
4+
import com.groommoa.aether_back_notification.global.common.exception.ErrorCode;
5+
6+
public class NoticeException extends CustomException {
7+
public NoticeException(ErrorCode errorCode) {
8+
super(errorCode);
9+
}
10+
}

src/main/java/com/groommoa/aether_back_notification/domain/notices/service/NoticeService.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.groommoa.aether_back_notification.domain.notices.dto.CreateNoticeRequestDto;
44
import com.groommoa.aether_back_notification.domain.notices.dto.NoticeDto;
55
import com.groommoa.aether_back_notification.domain.notices.entity.Notice;
6+
import com.groommoa.aether_back_notification.domain.notices.exception.NoticeException;
67
import com.groommoa.aether_back_notification.domain.notices.repository.NoticeRepository;
8+
import com.groommoa.aether_back_notification.global.common.exception.ErrorCode;
79
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
811
import org.bson.types.ObjectId;
912
import org.springframework.data.domain.PageRequest;
1013
import org.springframework.data.domain.Pageable;
@@ -13,7 +16,9 @@
1316

1417
import java.time.Instant;
1518
import java.util.List;
19+
import java.util.Optional;
1620

21+
@Slf4j
1722
@RequiredArgsConstructor
1823
@Service
1924
public class NoticeService {
@@ -40,4 +45,31 @@ public List<NoticeDto> getNotices(int limit) {
4045
.map(NoticeDto::from)
4146
.toList();
4247
}
48+
49+
public NoticeDto updateNotice(String requesterId, String noticeId, String content) {
50+
Notice notice = getValidatedNotice(requesterId, noticeId);
51+
52+
notice.setContent(content);
53+
noticeRepository.save(notice);
54+
55+
return NoticeDto.from(notice);
56+
}
57+
58+
public void deleteNotice(String requesterId, String noticeId) {
59+
Notice notice = getValidatedNotice(requesterId, noticeId);
60+
noticeRepository.delete(notice);
61+
}
62+
63+
private Notice getValidatedNotice(String requesterId, String noticeId) {
64+
Optional<Notice> optionalNotice = noticeRepository.findById(noticeId);
65+
if (optionalNotice.isEmpty()) {
66+
throw new NoticeException(ErrorCode.NOTICE_NOT_FOUND);
67+
}
68+
Notice notice = optionalNotice.get();
69+
String createdBy = notice.getCreatedBy().toHexString();
70+
if (requesterId != null && !requesterId.equals(createdBy)) {
71+
throw new NoticeException(ErrorCode.NOTICE_ACCESS_DENIED);
72+
}
73+
return notice;
74+
}
4375
}

src/main/java/com/groommoa/aether_back_notification/global/common/exception/ErrorCode.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public enum ErrorCode {
1717

1818
// notification
1919
NOTIFICATION_NOT_FOUNT(NOT_FOUND, "요청한 알림을 찾을 수 없습니다."),
20-
NOTIFICATION_ACCESS_DENIED(FORBIDDEN, "요청한 알림에 접근할 권한이 없습니다.");
20+
NOTIFICATION_ACCESS_DENIED(FORBIDDEN, "요청한 알림에 접근할 권한이 없습니다."),
21+
22+
// notice
23+
NOTICE_NOT_FOUND(NOT_FOUND, "요청한 공지를 찾을 수 없습니다."),
24+
NOTICE_ACCESS_DENIED(FORBIDDEN, "요청한 공지에 접근할 권한이 없습니다.");
2125

2226
private final HttpStatus httpStatus;
2327
private final String message;

src/main/java/com/groommoa/aether_back_notification/global/common/handler/GlobalExceptionHandler.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.JsonParseException;
44
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
55
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
6+
import com.groommoa.aether_back_notification.domain.notices.exception.NoticeException;
67
import com.groommoa.aether_back_notification.domain.notifications.exception.NotificationException;
78
import com.groommoa.aether_back_notification.global.common.exception.ErrorCode;
89
import com.groommoa.aether_back_notification.global.common.response.ErrorResponse;
@@ -223,6 +224,18 @@ public ResponseEntity<ErrorResponse> handleNotificationException(NotificationExc
223224
return ResponseEntity.status(status).body(response);
224225
}
225226

227+
@ExceptionHandler(NoticeException.class)
228+
public ResponseEntity<ErrorResponse> handleNoticeException(NoticeException ex) {
229+
ex.printStackTrace();
230+
ErrorCode errorCode = ex.getErrorCode();
231+
HttpStatus status = errorCode.getHttpStatus();
232+
String message = errorCode.getMessage();
233+
234+
ErrorResponse response = new ErrorResponse(
235+
status.value(), message, null);
236+
return ResponseEntity.status(status).body(response);
237+
}
238+
226239
@ExceptionHandler(Exception.class)
227240
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
228241
ex.printStackTrace();

0 commit comments

Comments
 (0)