Skip to content

Commit

Permalink
Merge pull request #37 from Team-Wable/feat/#36
Browse files Browse the repository at this point in the history
[FEAT] 공지사항 목록 GET API 개발
  • Loading branch information
Hong0329 authored Nov 11, 2024
2 parents 1a47320 + 7609e37 commit fc0084c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
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));
}
}
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;
}
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())
);
}
}
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);
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum SuccessStatus {
POST_GAMESTART_INFONOTIFICATION_SUCCESS(HttpStatus.CREATED,"게임 시작 정보 노티 생성 완료"),
POST_WEEKDONE_INFONOTIFICATION_SUCCESS(HttpStatus.CREATED,"한 주 종료 정보 노티 생성 완료"),
NEWS_ALL_SUCCESS(HttpStatus.OK, "뉴스 목록 조회 성공"),
NOTICE_ALL_SUCCESS(HttpStatus.OK, "공지사항 목록 조회 성공"),

/**
* report
Expand Down

0 comments on commit fc0084c

Please sign in to comment.