Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add category to notice #150

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading