-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from 9uttery/feat/notice-#120
[Feature] 공지사항 API 구현
- Loading branch information
Showing
10 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/guttery/madii/domain/notice/application/dto/CreateNoticeRequest.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,15 @@ | ||
package com.guttery.madii.domain.notice.application.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
@Schema(description = "공지사항 생성 요청") | ||
public record CreateNoticeRequest( | ||
@NotBlank | ||
@Schema(description = "공지사항 제목", example = "공지사항 제목") | ||
String title, | ||
@NotBlank | ||
@Schema(description = "공지사항 내용", example = "공지사항 내용") | ||
String contents | ||
) { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/guttery/madii/domain/notice/application/dto/NoticeInfo.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,18 @@ | ||
package com.guttery.madii.domain.notice.application.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Schema(description = "공지사항 정보") | ||
public record NoticeInfo( | ||
@Schema(description = "공지사항 ID", example = "1") | ||
Long id, | ||
@Schema(description = "공지사항 제목", example = "공지사항 제목") | ||
String title, | ||
@Schema(description = "공지사항 내용", example = "공지사항 내용") | ||
String contents, | ||
@Schema(description = "공지사항 작성일자", example = "2021-10-01") | ||
LocalDateTime createdAt | ||
) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/guttery/madii/domain/notice/application/dto/RecentNoticesResponse.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,8 @@ | ||
package com.guttery.madii.domain.notice.application.dto; | ||
|
||
import java.util.List; | ||
|
||
public record RecentNoticesResponse( | ||
List<NoticeInfo> notices | ||
) { | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/guttery/madii/domain/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,27 @@ | ||
package com.guttery.madii.domain.notice.application.service; | ||
|
||
import com.guttery.madii.domain.notice.application.dto.CreateNoticeRequest; | ||
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse; | ||
import com.guttery.madii.domain.notice.domain.model.Notice; | ||
import com.guttery.madii.domain.notice.domain.repository.NoticeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Service | ||
public class NoticeService { | ||
private final NoticeRepository noticeRepository; | ||
|
||
public RecentNoticesResponse getRecentNotices() { | ||
return noticeRepository.findRecent30DaysNotices(LocalDateTime.now()); | ||
} | ||
|
||
public void createNotice(final CreateNoticeRequest createNoticeRequest) { | ||
final Notice newNotice = Notice.create(createNoticeRequest.title(), createNoticeRequest.contents()); | ||
noticeRepository.save(newNotice); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/guttery/madii/domain/notice/domain/model/Notice.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,37 @@ | ||
package com.guttery.madii.domain.notice.domain.model; | ||
|
||
import com.guttery.madii.common.domain.model.BaseTimeEntity; | ||
import jakarta.persistence.Access; | ||
import jakarta.persistence.AccessType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Table(name = "t_notice") | ||
@Access(AccessType.FIELD) | ||
public class Notice extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long noticeId; | ||
private String title; | ||
@Column(name = "contents", columnDefinition = "TEXT") | ||
private String contents; | ||
|
||
public Notice(String title, String contents) { | ||
this.title = title; | ||
this.contents = contents; | ||
} | ||
|
||
public static Notice create(String title, String contents) { | ||
return new Notice(title, contents); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...main/java/com/guttery/madii/domain/notice/domain/repository/NoticeQuerydslRepository.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,9 @@ | ||
package com.guttery.madii.domain.notice.domain.repository; | ||
|
||
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface NoticeQuerydslRepository { | ||
RecentNoticesResponse findRecent30DaysNotices(LocalDateTime date); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/guttery/madii/domain/notice/domain/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,7 @@ | ||
package com.guttery.madii.domain.notice.domain.repository; | ||
|
||
import com.guttery.madii.domain.notice.domain.model.Notice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long>, NoticeQuerydslRepository { | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/guttery/madii/domain/notice/infrastructure/NoticeRepositoryImpl.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,32 @@ | ||
package com.guttery.madii.domain.notice.infrastructure; | ||
|
||
import com.guttery.madii.common.domain.repository.BaseQueryDslRepository; | ||
import com.guttery.madii.domain.notice.application.dto.NoticeInfo; | ||
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse; | ||
import com.guttery.madii.domain.notice.domain.model.Notice; | ||
import com.guttery.madii.domain.notice.domain.repository.NoticeQuerydslRepository; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import static com.guttery.madii.domain.notice.domain.model.QNotice.notice; | ||
|
||
@Repository | ||
public class NoticeRepositoryImpl extends BaseQueryDslRepository<Notice> implements NoticeQuerydslRepository { | ||
public NoticeRepositoryImpl(JPAQueryFactory queryFactory) { | ||
super(queryFactory); | ||
} | ||
|
||
@Override | ||
public RecentNoticesResponse findRecent30DaysNotices(final LocalDateTime date) { | ||
final LocalDateTime startOfDay = date.toLocalDate().atStartOfDay(); | ||
final List<NoticeInfo> foundNotices = select(NoticeInfo.class, notice.noticeId, notice.title, notice.contents, notice.createdAt) | ||
.from(notice) | ||
.where(notice.createdAt.after(startOfDay.minusDays(30))) | ||
.fetch(); | ||
|
||
return new RecentNoticesResponse(foundNotices); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/guttery/madii/domain/notice/presentation/NoticeController.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,56 @@ | ||
package com.guttery.madii.domain.notice.presentation; | ||
|
||
import com.guttery.madii.domain.notice.application.dto.CreateNoticeRequest; | ||
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse; | ||
import com.guttery.madii.domain.notice.application.service.NoticeService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/notices") | ||
@Validated | ||
@Tag(name = "Notice", description = "공지사항 관련 API") | ||
public class NoticeController { | ||
private final NoticeService noticeService; | ||
|
||
@GetMapping | ||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "최근 30일간 공지사항 조회 성공", | ||
useReturnTypeSchema = true | ||
) | ||
} | ||
) | ||
@Operation(summary = "최근 30일간 공지사항 조회 API", description = "최근 30일간 공지사항 조회 API입니다.") | ||
public RecentNoticesResponse getRecentNotices() { | ||
return noticeService.getRecentNotices(); | ||
} | ||
|
||
@PostMapping | ||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "공지사항 생성 성공", | ||
useReturnTypeSchema = true | ||
) | ||
} | ||
) | ||
@Operation(summary = "공지사항 생성 API", description = "테스트용 공지사항 생성 API입니다.") | ||
public void createNotice(@Valid @RequestBody CreateNoticeRequest createNoticeRequest) { | ||
noticeService.createNotice(createNoticeRequest); | ||
} | ||
} |