-
Notifications
You must be signed in to change notification settings - Fork 2
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: 공지 CRUD API 구현 #323
Merged
feat: 공지 CRUD API 구현 #323
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4e3af74
test: 전체 테스트가 실패하는 문제 해결
kdkdhoho 8d5ece2
test: 요청 주제(토픽) 초기 생성 시 isExposed를 항상 true로 변경함에 따른 테스트 코드 수정 (#312)
kdkdhoho 4c39059
feat: 공지 카테고리(타입) 조회 API 구현 (#312)
kdkdhoho 58e606e
feat: Notice 도메인 객체 생성 (#312)
kdkdhoho d59695a
feat: 공지 생성 API 구현 (#312)
kdkdhoho 3de2eed
feat: 사용자용, 관리자용 공지 전체 조회 및 공지 상세 조회 API 구현 (#312)
kdkdhoho 2063ce6
feat: 공지 수정 API 구현 (#312)
kdkdhoho 1c6b03e
feat: 공지 삭제 API 구현 (#312)
kdkdhoho 8989a84
fix: 관리자 존재 체크 로직 제거 (#312)
kdkdhoho 6a7d204
refactor: 빠진 @Nullable 추가 (#312)
kdkdhoho dffe121
test: NoticeCreateRequest 생성 부분 리팩터링 (#312)
kdkdhoho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/listywave/notice/application/service/NoticeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.listywave.notice.application.service; | ||
|
||
import com.listywave.notice.application.domain.Notice; | ||
import com.listywave.notice.application.domain.NoticeContent; | ||
import com.listywave.notice.application.service.dto.NoticeCreateRequest; | ||
import com.listywave.notice.repository.NoticeRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class NoticeService { | ||
|
||
private final NoticeRepository noticeRepository; | ||
|
||
public Long create(NoticeCreateRequest request) { | ||
Notice notice = request.toNotice(); | ||
List<NoticeContent> noticeContents = request.toNoticeContents(notice); | ||
notice.addContents(noticeContents); | ||
return noticeRepository.save(notice).getId(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/listywave/notice/application/service/dto/NoticeCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.listywave.notice.application.service.dto; | ||
|
||
import com.listywave.notice.application.domain.ContentType; | ||
import com.listywave.notice.application.domain.Notice; | ||
import com.listywave.notice.application.domain.NoticeContent; | ||
import com.listywave.notice.application.domain.NoticeDescription; | ||
import com.listywave.notice.application.domain.NoticeTitle; | ||
import com.listywave.notice.application.domain.NoticeType; | ||
import jakarta.annotation.Nullable; | ||
import java.util.List; | ||
|
||
public record NoticeCreateRequest( | ||
int categoryCode, | ||
String title, | ||
String description, | ||
List<ContentDto> contents | ||
) { | ||
|
||
public record ContentDto( | ||
int order, | ||
String type, | ||
@Nullable String description, | ||
@Nullable String imageUrl, | ||
@Nullable String buttonName, | ||
@Nullable String buttonLink | ||
) { | ||
} | ||
|
||
public Notice toNotice() { | ||
return new Notice(NoticeType.codeOf(categoryCode), new NoticeTitle(title), new NoticeDescription(description)); | ||
} | ||
|
||
public List<NoticeContent> toNoticeContents(Notice notice) { | ||
return contents.stream() | ||
.map(it -> NoticeContent.create(notice, | ||
it.order, | ||
ContentType.valueOf(it.type.toUpperCase()), | ||
it.description, | ||
it.imageUrl, | ||
it.buttonName, | ||
it.buttonLink) | ||
).toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/listywave/notice/repository/NoticeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.listywave.notice.repository; | ||
|
||
import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND; | ||
|
||
import com.listywave.common.exception.CustomException; | ||
import com.listywave.notice.application.domain.Notice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long> { | ||
|
||
default Notice getById(Long id) { | ||
return findById(id).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, "존재하지 않는 공지입니다.")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/com/listywave/notice/application/service/NoticeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.listywave.notice.application.service; | ||
|
||
import static com.listywave.notice.application.domain.NoticeType.NEWS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import com.listywave.common.IntegrationTest; | ||
import com.listywave.notice.application.domain.Notice; | ||
import com.listywave.notice.application.service.dto.NoticeCreateRequest; | ||
import com.listywave.notice.application.service.dto.NoticeCreateRequest.ContentDto; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class NoticeServiceTest extends IntegrationTest { | ||
|
||
@Test | ||
void 공지를_생성한다() { | ||
// given | ||
NoticeCreateRequest request = new NoticeCreateRequest( | ||
1, | ||
"공지입니다", | ||
"공지에요", | ||
List.of( | ||
new ContentDto(1, "subtitle", "소제목입니다", null, null, null), | ||
new ContentDto(2, "body", "본문입니다", null, null, null), | ||
new ContentDto(3, "image", "이미지입니다", "https://image.com", null, null), | ||
new ContentDto(4, "button", "버튼입니다", null, "버튼 이름", "https://buttonLink.com"), | ||
new ContentDto(5, "note", "유의사항입니다", null, null, null) | ||
) | ||
); | ||
|
||
// when | ||
noticeService.create(request); | ||
|
||
// then | ||
Notice result = noticeRepository.getById(1L); | ||
assertAll( | ||
() -> assertThat(result.getType()).isEqualTo(NEWS), | ||
() -> assertThat(result.getTitle().getValue()).isEqualTo("공지입니다"), | ||
() -> assertThat(result.getDescription().getValue()).isEqualTo("공지에요") | ||
// () -> assertThat(result.getContents().size()).isEqualTo(5) | ||
// () -> { | ||
// List<NoticeContent> contents = result.getContents(); | ||
// | ||
// ; | ||
// assertAll( | ||
// () -> assertThat(contents.get(0).getOrder()).isEqualTo(1), | ||
// () -> assertThat(contents.get(0).getType()).isEqualTo(SUBTITLE), | ||
// () -> assertThat(contents.get(0).getDescription()).isEqualTo("소제목입니다"), | ||
// () -> assertThat(contents.get(0).getImageUrl()) | ||
// .isEqualTo(contents.get(0).getButtonName()) | ||
// .isEqualTo(contents.get(0).getButtonLink()) | ||
// .isNull() | ||
// ); | ||
// } | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 이게 그 RestFul API 설계 원칙에 HATEOAS 방식을 사용한 건가요? 응답에 uri 넣어주는
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이는 HTTP - 201 Created 상태 코드의 명세에 따른 것입니다~!