Skip to content

Commit

Permalink
feat: Add category to notice (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwangjokim authored Nov 9, 2024
1 parent acbcac7 commit 6825729
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Notice {
private String title;
@Column(columnDefinition = "TEXT")
private String content;
private String category;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

Expand All @@ -43,17 +44,20 @@ public class Notice {
private StudyGroup studyGroup;

@Builder
public Notice(User author, StudyGroup studyGroup, String title, String content, LocalDateTime createdAt) {
public Notice(User author, StudyGroup studyGroup, String title, String content, String category,
LocalDateTime createdAt) {
this.author = author;
this.title = title;
this.studyGroup = studyGroup;
this.content = content;
this.category = category;
this.createdAt = createdAt;
}

public void updateNotice(String title, String content) {
public void updateNotice(String title, String content, String category) {
this.title = title;
this.content = content;
this.category = category;
this.updatedAt = LocalDateTime.now();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record CreateNoticeRequest(@NotNull(message = "그룹id 는 필수입니다") Long studyGroupId,
public record CreateNoticeRequest(@NotNull(message = "그룹 id 는 필수입니다") Long studyGroupId,
@NotBlank(message = "제목은 필수 입력입니다") String title,
@NotBlank(message = "본문은 필수 입력입니다") String content
@NotBlank(message = "본문은 필수 입력입니다") String content,
@NotBlank(message = "카테고리는 필수 입력입니다") String category
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
@Builder
public record GetNoticeResponse(String author,
Long noticeId,
String noticeContent,
String noticeTitle,
String content,
String title,
String category,
String createAt) {

public static GetNoticeResponse toDTO(Notice notice) {
return GetNoticeResponse.builder()
.author(notice.getAuthor().getNickname())
.noticeId(notice.getId())
.noticeTitle(notice.getTitle())
.noticeContent(notice.getContent())
.title(notice.getTitle())
.content(notice.getContent())
.category(notice.getCategory())
.createAt(DateFormatUtil.formatDate(notice.getCreatedAt().toLocalDate()))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

public record UpdateNoticeRequest(@NotNull(message = "게시글 id는 필수 입니다.") Long noticeId,
@NotBlank(message = "제목은 필수 입력입니다") String title,
@NotBlank(message = "본문은 필수 입력입니다") String content) {
@NotBlank(message = "본문은 필수 입력입니다") String content,
@NotBlank(message = "카테고리는 필수 입력입니다") String category
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void createNotice(@AuthedUser User user, CreateNoticeRequest request) {
.studyGroup(studyGroup)
.title(request.title())
.content(request.content())
.category(request.category())
.createdAt(LocalDateTime.now())
.build());
log.info("success to create notice");
Expand All @@ -71,8 +72,9 @@ public GetNoticeResponse getNotice(@AuthedUser User user, Long noticeId) {
return GetNoticeResponse.builder()
.author(notice.getAuthor().getNickname())
.noticeId(notice.getId())
.noticeTitle(notice.getTitle())
.noticeContent(notice.getContent())
.title(notice.getTitle())
.content(notice.getContent())
.category(notice.getCategory())
.createAt(DateFormatUtil.formatDate(notice.getCreatedAt().toLocalDate()))
.build();
}
Expand All @@ -98,7 +100,7 @@ public void updateNotice(User user, UpdateNoticeRequest request) {
if (!user.getId().equals(notice.getAuthor().getId()))
throw new UserValidationException("공지를 수정할 수 있는 권한이 없습니다");

notice.updateNotice(request.title(), request.content());
notice.updateNotice(request.title(), request.content(), request.category());
}

@Transactional
Expand Down
39 changes: 25 additions & 14 deletions src/test/java/com/gamzabat/algohub/service/NoticeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
.createdAt(LocalDateTime.now())
.title("title")
.content("content")
.category("category")
.author(user)
.build();

Expand Down Expand Up @@ -107,7 +108,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
@DisplayName("공지 작성 성공")
void createNoticeSuccess_1() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content");
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user2, studyGroup)).thenReturn(
Optional.ofNullable(groupMember2));
Expand All @@ -119,6 +120,7 @@ void createNoticeSuccess_1() {
assertThat(result.getAuthor()).isEqualTo(user2);
assertThat(result.getContent()).isEqualTo("content");
assertThat(result.getTitle()).isEqualTo("title");
assertThat(result.getCategory()).isEqualTo("category");
assertThat(result.getStudyGroup()).isEqualTo(studyGroup);

}
Expand All @@ -127,7 +129,7 @@ void createNoticeSuccess_1() {
@DisplayName("공지 작성 실패 그룹장or부방장이 아님")
void createNoticeFail_1() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content");
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user3, studyGroup)).thenReturn(
Optional.ofNullable(groupMember3));
Expand All @@ -142,7 +144,7 @@ void createNoticeFail_1() {
@DisplayName("공지 작성 실패 존재하지 않는 그룹")
void createNoticeFail_2() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(31L, "title", "content");
CreateNoticeRequest request = new CreateNoticeRequest(31L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.createNotice(user, request))
Expand All @@ -155,7 +157,7 @@ void createNoticeFail_2() {
@DisplayName("공지 작성 실패 존재하지 않는 멤버")
void createNoticeFail_3() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content");
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user4, studyGroup)).thenReturn(Optional.empty());
//when,then
Expand All @@ -176,8 +178,9 @@ void getNoticeSuccess_1() {
GetNoticeResponse response = noticeService.getNotice(user2, 1000L);
//then
assertThat(response.author()).isEqualTo("nickname1");
assertThat(response.noticeContent()).isEqualTo("content");
assertThat(response.noticeTitle()).isEqualTo("title");
assertThat(response.content()).isEqualTo("content");
assertThat(response.title()).isEqualTo("title");
assertThat(response.category()).isEqualTo("category");
assertThat(response.createAt()).isEqualTo(DateFormatUtil.formatDate(LocalDateTime.now().toLocalDate()));
assertThat(response.noticeId()).isEqualTo(1000L);
}
Expand Down Expand Up @@ -215,19 +218,21 @@ void getNoticeListSuccess_1() {
List<Notice> noticeList = new ArrayList<>(10);
for (int i = 0; i < 10; i++)
noticeList.add(
notice.builder()
Notice.builder()
.author(user)
.content("content" + i)
.title("title" + i)
.category("category" + i)
.createdAt(LocalDateTime.now())
.studyGroup(studyGroup)
.build());
for (int i = 10; i < 20; i++)
noticeList.add(
notice.builder()
Notice.builder()
.author(user2)
.content("content" + i)
.title("title" + i)
.category("category" + i)
.createdAt(LocalDateTime.now())
.studyGroup(studyGroup)
.build());
Expand All @@ -239,8 +244,9 @@ void getNoticeListSuccess_1() {
//then
assertThat(result.size()).isEqualTo(20);
for (int i = 0; i < 20; i++) {
assertThat(result.get(i).noticeContent()).isEqualTo("content" + i);
assertThat(result.get(i).noticeTitle()).isEqualTo("title" + i);
assertThat(result.get(i).content()).isEqualTo("content" + i);
assertThat(result.get(i).title()).isEqualTo("title" + i);
assertThat(result.get(i).category()).isEqualTo("category" + i);
}
}

Expand Down Expand Up @@ -273,21 +279,24 @@ void getNoticeListFailed_2() {
@DisplayName("공지 수정 성공")
void updateNoticeSuccess() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent");
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
//when
noticeService.updateNotice(user, updateNoticeRequest);
//then
assertThat(notice.getContent()).isEqualTo("updateContent");
assertThat(notice.getTitle()).isEqualTo("updateTitle");
assertThat(notice.getCategory()).isEqualTo("updateCategory");
}

@Test
@DisplayName("공지 수정 실패(존재하지 않는 게시글)")
void updateNoticeFailed_1() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1001L, "updateTitle", "updateContent");
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1001L, "updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1001L)).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.updateNotice(user, updateNoticeRequest))
Expand All @@ -299,7 +308,8 @@ void updateNoticeFailed_1() {
@DisplayName("공시 수정 실패(존재하지 않는 스터디 그룹)")
void updateNoticeFailed_2() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent");
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.empty());
//when,then
Expand All @@ -314,7 +324,8 @@ void updateNoticeFailed_2() {
@DisplayName("공지 수정 실패(게시글 작성자가 아님)")
void updateNoticeFailed_3() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent");
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
//when, then
Expand Down

0 comments on commit 6825729

Please sign in to comment.