diff --git a/src/main/java/org/websoso/WSSServer/service/NoticeService.java b/src/main/java/org/websoso/WSSServer/service/NoticeService.java index 55a01b9f..7d486579 100644 --- a/src/main/java/org/websoso/WSSServer/service/NoticeService.java +++ b/src/main/java/org/websoso/WSSServer/service/NoticeService.java @@ -1,6 +1,7 @@ package org.websoso.WSSServer.service; import static org.websoso.WSSServer.domain.common.Role.ADMIN; +import static org.websoso.WSSServer.exception.error.CustomNoticeCategoryError.NOTICE_CATEGORY_NOT_FOUND; import static org.websoso.WSSServer.exception.error.CustomNoticeError.NOTICE_FORBIDDEN; import static org.websoso.WSSServer.exception.error.CustomNoticeError.NOTICE_NOT_FOUND; @@ -9,12 +10,15 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.websoso.WSSServer.domain.Notice; +import org.websoso.WSSServer.domain.NoticeCategory; import org.websoso.WSSServer.domain.User; import org.websoso.WSSServer.domain.common.Role; import org.websoso.WSSServer.dto.notice.NoticeEditRequest; import org.websoso.WSSServer.dto.notice.NoticePostRequest; import org.websoso.WSSServer.dto.notice.NoticesGetResponse; +import org.websoso.WSSServer.exception.exception.CustomNoticeCategoryException; import org.websoso.WSSServer.exception.exception.CustomNoticeException; +import org.websoso.WSSServer.repository.NoticeCategoryRepository; import org.websoso.WSSServer.repository.NoticeRepository; @Service @@ -23,6 +27,8 @@ public class NoticeService { private final NoticeRepository noticeRepository; + private final NoticeCategoryRepository noticeCategoryRepository; + private static final Role ADMIN_ROLE = ADMIN; public void createNotice(User user, NoticePostRequest noticePostRequest) { @@ -31,6 +37,7 @@ public void createNotice(User user, NoticePostRequest noticePostRequest) { .noticeTitle(noticePostRequest.noticeTitle()) .noticeContent(noticePostRequest.noticeContent()) .userId(noticePostRequest.userId()) + .noticeCategory(getNoticeCategoryOrException(noticePostRequest.noticeCategoryId())) .build()); } @@ -38,7 +45,7 @@ public void editNotice(User user, Long noticeId, NoticeEditRequest noticeEditReq validateAuthorization(user); Notice notice = getNoticeOrException(noticeId); notice.updateNotice(noticeEditRequest.noticeTitle(), noticeEditRequest.noticeContent(), - noticeEditRequest.userId()); + noticeEditRequest.userId(), getNoticeCategoryOrException(noticeEditRequest.noticeCategoryId())); } private static void validateAuthorization(User user) { @@ -64,4 +71,10 @@ private Notice getNoticeOrException(Long noticeId) { return noticeRepository.findById(noticeId).orElseThrow(() -> new CustomNoticeException(NOTICE_NOT_FOUND, "notice with given noticeId was not found")); } + + private NoticeCategory getNoticeCategoryOrException(Byte noticeCategoryId) { + return noticeCategoryRepository.findById(noticeCategoryId).orElseThrow( + () -> new CustomNoticeCategoryException(NOTICE_CATEGORY_NOT_FOUND, + "notice category with given noticeCategoryId was not found")); + } }