Skip to content

Commit

Permalink
[FIX] 추가된 NoticeCategory를 바탕으로 NoticeService 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
rinarina0429 committed Jan 9, 2025
1 parent 901b343 commit 207d003
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/org/websoso/WSSServer/service/NoticeService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -31,14 +37,15 @@ public void createNotice(User user, NoticePostRequest noticePostRequest) {
.noticeTitle(noticePostRequest.noticeTitle())
.noticeContent(noticePostRequest.noticeContent())
.userId(noticePostRequest.userId())
.noticeCategory(getNoticeCategoryOrException(noticePostRequest.noticeCategoryId()))
.build());
}

public void editNotice(User user, Long noticeId, NoticeEditRequest noticeEditRequest) {
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) {
Expand All @@ -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"));
}
}

0 comments on commit 207d003

Please sign in to comment.