diff --git a/src/main/java/com/neighbors/tohero/application/baseResponse/BaseResponseMessage.java b/src/main/java/com/neighbors/tohero/application/baseResponse/BaseResponseMessage.java index 82aafbc..aaa7ad6 100644 --- a/src/main/java/com/neighbors/tohero/application/baseResponse/BaseResponseMessage.java +++ b/src/main/java/com/neighbors/tohero/application/baseResponse/BaseResponseMessage.java @@ -12,7 +12,9 @@ public enum BaseResponseMessage { 토큰_재발급이_성공했습니다("토큰 재발급이 성공했습니다"), //notice success + 공지_무한페이징_조회_성공했습니댜("공지 무한페이징 조회 성공했습니댜"), 공지_조회_성공했습니댜("공지 조회 성공했습니댜"), + 일치하는_공지가_없습니다("일치하는 공지가 없습니다"), //user 이미_존재하는_유저입니다("이미 존재하는 유저입니다"), diff --git a/src/main/java/com/neighbors/tohero/application/notice/dto/GetNoticeResponse.java b/src/main/java/com/neighbors/tohero/application/notice/dto/GetNoticeResponse.java index ae42e5d..af2e73a 100644 --- a/src/main/java/com/neighbors/tohero/application/notice/dto/GetNoticeResponse.java +++ b/src/main/java/com/neighbors/tohero/application/notice/dto/GetNoticeResponse.java @@ -7,12 +7,14 @@ public record GetNoticeResponse(List noticeList) { public record NoticeDTO( + long noticeId, String title, String content, String createdAT ) { public static NoticeDTO from(Notice notice) { return new GetNoticeResponse.NoticeDTO( + notice.getNoticeId(), notice.getNoticeTitle(), notice.getNoticeContent(), notice.getCreatedAt().toLocalDate().toString() diff --git a/src/main/java/com/neighbors/tohero/application/notice/service/NoticeService.java b/src/main/java/com/neighbors/tohero/application/notice/service/NoticeService.java index 20a1600..5d18dca 100644 --- a/src/main/java/com/neighbors/tohero/application/notice/service/NoticeService.java +++ b/src/main/java/com/neighbors/tohero/application/notice/service/NoticeService.java @@ -24,9 +24,19 @@ public BaseResponse getNotice(Pageable pageable){ return new BaseResponse<>( BaseResponseStatus.OK, - BaseResponseMessage.공지_조회_성공했습니댜.getMessage(), + BaseResponseMessage.공지_무한페이징_조회_성공했습니댜.getMessage(), GetNoticeResponse.createSuccessObjFrom(notices) ); } + public BaseResponse getNoticeDetail(long noticeId){ + Notice notice = getNotice.getNotice(noticeId); + + return new BaseResponse<>( + BaseResponseStatus.OK, + BaseResponseMessage.공지_조회_성공했습니댜.getMessage(), + GetNoticeResponse.NoticeDTO.from(notice) + ); + } + } diff --git a/src/main/java/com/neighbors/tohero/common/exception/notice/NoticeException.java b/src/main/java/com/neighbors/tohero/common/exception/notice/NoticeException.java new file mode 100644 index 0000000..c32cfdd --- /dev/null +++ b/src/main/java/com/neighbors/tohero/common/exception/notice/NoticeException.java @@ -0,0 +1,15 @@ +package com.neighbors.tohero.common.exception.notice; + +import com.neighbors.tohero.application.baseResponse.BaseResponseStatus; + +public class NoticeException extends RuntimeException { + + private final BaseResponseStatus status; + private final String message; + + public NoticeException(BaseResponseStatus status, String message) { + super(message); + this.status = status; + this.message = message; + } +} \ No newline at end of file diff --git a/src/main/java/com/neighbors/tohero/domain/domain/notice/service/GetNotice.java b/src/main/java/com/neighbors/tohero/domain/domain/notice/service/GetNotice.java index c4b121a..9b1792c 100644 --- a/src/main/java/com/neighbors/tohero/domain/domain/notice/service/GetNotice.java +++ b/src/main/java/com/neighbors/tohero/domain/domain/notice/service/GetNotice.java @@ -21,4 +21,8 @@ public List getPagedNotice(Pageable pageable) { public List getTopNotices(int exposeNoticeNumber){ return noticeRepository.getTopNotices(exposeNoticeNumber); } + + public Notice getNotice(long noticeId) { + return noticeRepository.getNoticeById(noticeId); + } } diff --git a/src/main/java/com/neighbors/tohero/domain/query/NoticeRepository.java b/src/main/java/com/neighbors/tohero/domain/query/NoticeRepository.java index 7a8b013..b5e6232 100644 --- a/src/main/java/com/neighbors/tohero/domain/query/NoticeRepository.java +++ b/src/main/java/com/neighbors/tohero/domain/query/NoticeRepository.java @@ -8,4 +8,5 @@ public interface NoticeRepository { List getPagedNotice(Pageable pageable); List getTopNotices(int exposeNoticeNumber); + Notice getNoticeById(long noticeId); } diff --git a/src/main/java/com/neighbors/tohero/infrastructure/query/impl/NoticeRepositoryImpl.java b/src/main/java/com/neighbors/tohero/infrastructure/query/impl/NoticeRepositoryImpl.java index 8a4b313..a5f3ee5 100644 --- a/src/main/java/com/neighbors/tohero/infrastructure/query/impl/NoticeRepositoryImpl.java +++ b/src/main/java/com/neighbors/tohero/infrastructure/query/impl/NoticeRepositoryImpl.java @@ -1,5 +1,8 @@ package com.neighbors.tohero.infrastructure.query.impl; +import com.neighbors.tohero.application.baseResponse.BaseResponseMessage; +import com.neighbors.tohero.application.baseResponse.BaseResponseStatus; +import com.neighbors.tohero.common.exception.notice.NoticeException; import com.neighbors.tohero.domain.domain.notice.model.Notice; import com.neighbors.tohero.domain.query.NoticeRepository; import com.neighbors.tohero.infrastructure.entity.NoticeEntity; @@ -10,6 +13,7 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository @RequiredArgsConstructor @@ -35,4 +39,15 @@ public List getTopNotices(int exposeNoticeNumber) { .map(noticeMapper::toDomain) .toList(); } + + @Override + public Notice getNoticeById(long noticeId) { + NoticeEntity noticeEntity = noticeEntityRepository.findById(noticeId) + .orElseThrow(() -> new NoticeException( + BaseResponseStatus.NO_RESULT, + BaseResponseMessage.일치하는_공지가_없습니다.getMessage() + )); + + return noticeMapper.toDomain(noticeEntity); + } } diff --git a/src/main/java/com/neighbors/tohero/presentation/controller/NoticeController.java b/src/main/java/com/neighbors/tohero/presentation/controller/NoticeController.java index aa191a1..82837f3 100644 --- a/src/main/java/com/neighbors/tohero/presentation/controller/NoticeController.java +++ b/src/main/java/com/neighbors/tohero/presentation/controller/NoticeController.java @@ -9,6 +9,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -24,4 +25,11 @@ public ResponseEntity getNotice(@ParameterObject Pageable pageable return ResponseEntity.ok() .body(noticeService.getNotice(pageable)); } + + @Operation(summary = "공지 API", description = "공지사항 상세보기 API입니다.") + @GetMapping("/notice/detail") + public ResponseEntity getNoticeDetail(@RequestParam long noticeId){ + return ResponseEntity.ok() + .body(noticeService.getNoticeDetail(noticeId)); + } } diff --git a/src/main/java/com/neighbors/tohero/presentation/exception_handler/DTOFieldExceptionControllerAdvice.java b/src/main/java/com/neighbors/tohero/presentation/exception_handler/DTOFieldExceptionControllerAdvice.java index 367370f..12dccf8 100644 --- a/src/main/java/com/neighbors/tohero/presentation/exception_handler/DTOFieldExceptionControllerAdvice.java +++ b/src/main/java/com/neighbors/tohero/presentation/exception_handler/DTOFieldExceptionControllerAdvice.java @@ -32,7 +32,7 @@ public DTOFieldExceptionControllerAdvice() { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity handle_UserValidationException(MethodArgumentNotValidException e) { + public ResponseEntity handle_fieldValidationException(MethodArgumentNotValidException e) { BaseResponse response = new BaseResponse( BaseResponseStatus.BAD_REQUEST, fieldErrorMapper.get(e.getBindingResult().getFieldError().getField()).getMessage() diff --git a/src/main/java/com/neighbors/tohero/presentation/exception_handler/NoticeExceptionControllerAdvice.java b/src/main/java/com/neighbors/tohero/presentation/exception_handler/NoticeExceptionControllerAdvice.java new file mode 100644 index 0000000..397c6e0 --- /dev/null +++ b/src/main/java/com/neighbors/tohero/presentation/exception_handler/NoticeExceptionControllerAdvice.java @@ -0,0 +1,26 @@ +package com.neighbors.tohero.presentation.exception_handler; + +import com.neighbors.tohero.application.baseResponse.BaseResponse; +import com.neighbors.tohero.application.baseResponse.BaseResponseStatus; +import com.neighbors.tohero.common.exception.notice.NoticeException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class NoticeExceptionControllerAdvice { + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(NoticeException.class) + public ResponseEntity handle_NoticeException(NoticeException e) { + BaseResponse response = new BaseResponse( + BaseResponseStatus.NO_RESULT, + e.getMessage() + ); + + return ResponseEntity.badRequest() + .body(response); + } +}