-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] 공지사항 목록 GET API 개발
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...src/main/java/com/wable/www/WableServer/api/notification/controller/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,30 @@ | ||
package com.wable.www.WableServer.api.notification.controller; | ||
|
||
import com.wable.www.WableServer.api.notification.service.NoticeQueryService; | ||
import com.wable.www.WableServer.common.response.ApiResponse; | ||
import com.wable.www.WableServer.common.response.SuccessStatus; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/") | ||
@RequiredArgsConstructor | ||
@SecurityRequirement(name = "JWT Auth") | ||
@Tag(name="공지사항 탭",description = "Notice Api Document") | ||
public class NoticeController { | ||
private final NoticeQueryService noticeQueryService; | ||
|
||
@GetMapping("information/notice") | ||
@Operation(summary = "공지사항 목록 조회 API 입니다.",description = "NoticeList") | ||
public ResponseEntity<ApiResponse<Object>> getNotices(@RequestParam(value = "cursor", defaultValue = "-1") Long cursor) { | ||
return ApiResponse.success(SuccessStatus.NOTICE_ALL_SUCCESS, | ||
noticeQueryService.getNotices(cursor)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
WableServer/src/main/java/com/wable/www/WableServer/api/notification/domain/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,26 @@ | ||
package com.wable.www.WableServer.api.notification.domain; | ||
|
||
import com.wable.www.WableServer.common.entity.BaseTimeEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = PROTECTED) | ||
public class Notice extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String noticeTitle; | ||
|
||
private String noticeText; | ||
|
||
private String noticeImage; | ||
} |
22 changes: 22 additions & 0 deletions
22
...in/java/com/wable/www/WableServer/api/notification/dto/response/NoticeAllResponseDto.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,22 @@ | ||
package com.wable.www.WableServer.api.notification.dto.response; | ||
|
||
import com.wable.www.WableServer.api.notification.domain.Notice; | ||
import com.wable.www.WableServer.common.util.TimeUtilCustom; | ||
|
||
public record NoticeAllResponseDto( | ||
Long noticeId, | ||
String noticeTitle, | ||
String noticeText, | ||
String noticeImage, | ||
String time | ||
) { | ||
public static NoticeAllResponseDto of(Notice notice) { | ||
return new NoticeAllResponseDto( | ||
notice.getId(), | ||
notice.getNoticeTitle(), | ||
notice.getNoticeText(), | ||
notice.getNoticeImage(), | ||
TimeUtilCustom.refineTime(notice.getCreatedAt()) | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...src/main/java/com/wable/www/WableServer/api/notification/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.wable.www.WableServer.api.notification.repository; | ||
|
||
import com.wable.www.WableServer.api.notification.domain.Notice; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long> { | ||
Slice<Notice> findTop15ByOrderByCreatedAtDesc(PageRequest pageRequest); | ||
|
||
@Query("SELECT c FROM Notice c WHERE c.id < :cursor ORDER BY c.createdAt DESC") | ||
Slice<Notice> findNoticeNextPage(Long cursor, PageRequest pageRequest); | ||
} |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/com/wable/www/WableServer/api/notification/service/NoticeQueryService.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,33 @@ | ||
package com.wable.www.WableServer.api.notification.service; | ||
|
||
import com.wable.www.WableServer.api.notification.domain.Notice; | ||
import com.wable.www.WableServer.api.notification.dto.response.NoticeAllResponseDto; | ||
import com.wable.www.WableServer.api.notification.repository.NoticeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class NoticeQueryService { | ||
private static final int NOTICE_DEFAULT_PAGE_SIZE = 15; | ||
|
||
private final NoticeRepository noticeRepository; | ||
|
||
public List<NoticeAllResponseDto> getNotices(Long cursor) { | ||
PageRequest pageRequest = PageRequest.of(0, NOTICE_DEFAULT_PAGE_SIZE); | ||
Slice<Notice> noticeSlice; | ||
|
||
if (cursor == -1) { | ||
noticeSlice = noticeRepository.findTop15ByOrderByCreatedAtDesc(pageRequest); | ||
} else { | ||
noticeSlice = noticeRepository.findNoticeNextPage(cursor, pageRequest); | ||
} | ||
return noticeSlice.stream() | ||
.map(NoticeAllResponseDto::of).collect(Collectors.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