-
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 #245 from KJBig/feature/S2D-62
Feature/s2 d 62
- Loading branch information
Showing
9 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/com/sluv/server/domain/notice/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,55 @@ | ||
package com.sluv.server.domain.notice.controller; | ||
|
||
import com.sluv.server.domain.notice.dto.NoticeDetailResDto; | ||
import com.sluv.server.domain.notice.dto.NoticeSimpleResDto; | ||
import com.sluv.server.domain.notice.service.NoticeService; | ||
import com.sluv.server.global.common.response.PaginationResDto; | ||
import com.sluv.server.global.common.response.SuccessDataResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/app/notice") | ||
@RequiredArgsConstructor | ||
public class NoticeController { | ||
private final NoticeService noticeService; | ||
@Operation( | ||
summary = "공지사항 리스트 조회", | ||
description = """ | ||
- 공지사항 리스트 조회 API\n | ||
- Pagination 적용 | ||
""" | ||
) | ||
@GetMapping("") | ||
public ResponseEntity<SuccessDataResponse<PaginationResDto<NoticeSimpleResDto>>> getAllNotice(Pageable pageable){ | ||
|
||
return ResponseEntity.ok().body( | ||
SuccessDataResponse.<PaginationResDto<NoticeSimpleResDto>>builder() | ||
.result(noticeService.getAllNotice(pageable)) | ||
.build() | ||
); | ||
} | ||
|
||
@Operation( | ||
summary = "공지사항 상세 조회", | ||
description = """ | ||
- 공지사항 상세 조회 API\n | ||
- 상세 조회할 공지사항의 Id를 PathVariable로 받음. | ||
""" | ||
) | ||
@GetMapping("/{noticeId}") | ||
public ResponseEntity<SuccessDataResponse<NoticeDetailResDto>> getNoticeDetail(@PathVariable("noticeId") Long noticeId){ | ||
|
||
return ResponseEntity.ok().body( | ||
SuccessDataResponse.<NoticeDetailResDto>builder() | ||
.result(noticeService.getNoticeDetail(noticeId)) | ||
.build() | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/sluv/server/domain/notice/dto/NoticeDetailResDto.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.sluv.server.domain.notice.dto; | ||
|
||
import com.sluv.server.domain.notice.entity.Notice; | ||
import com.sluv.server.domain.notice.enums.NoticeType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class NoticeDetailResDto { | ||
|
||
@Schema(description = "공지사항 제목") | ||
private String title; | ||
@Schema(description = "공지사항 내용") | ||
private String content; | ||
@Schema(description = "공지사항 공지일") | ||
private LocalDateTime createdAt; | ||
@Schema(description = "공지사항 타입") | ||
private NoticeType noticeType; | ||
|
||
public static NoticeDetailResDto of(Notice notice){ | ||
|
||
return NoticeDetailResDto.builder() | ||
.title(notice.getTitle()) | ||
.content(notice.getContent()) | ||
.createdAt(notice.getCreatedAt()) | ||
.noticeType(notice.getNoticeType()) | ||
.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/sluv/server/domain/notice/dto/NoticeSimpleResDto.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.sluv.server.domain.notice.dto; | ||
|
||
import com.sluv.server.domain.notice.entity.Notice; | ||
import com.sluv.server.domain.notice.enums.NoticeType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class NoticeSimpleResDto { | ||
@Schema(description = "공지사항 제목") | ||
private String title; | ||
@Schema(description = "공지사항 공지일") | ||
private LocalDateTime createdAt; | ||
@Schema(description = "공지사항 타입") | ||
private NoticeType noticeType; | ||
|
||
public static NoticeSimpleResDto of(Notice notice){ | ||
|
||
return NoticeSimpleResDto.builder() | ||
.title(notice.getTitle()) | ||
.createdAt(notice.getCreatedAt()) | ||
.noticeType(notice.getNoticeType()) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sluv/server/domain/notice/exception/NoticeException.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,10 @@ | ||
package com.sluv.server.domain.notice.exception; | ||
|
||
import com.sluv.server.global.common.exception.ApplicationException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public abstract class NoticeException extends ApplicationException { | ||
public NoticeException(int errorCode, HttpStatus httpStatus, String message) { | ||
super(errorCode, httpStatus, message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sluv/server/domain/notice/exception/NoticeNotFoundException.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.sluv.server.domain.notice.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class NoticeNotFoundException extends NoticeException{ | ||
|
||
private static final int ERROR_CODE = 2024; | ||
private static final String MESSAGE = "존재하지 않는 Notice입니다."; | ||
private static final HttpStatus STATUS = HttpStatus.BAD_REQUEST; | ||
|
||
public NoticeNotFoundException() { | ||
super(ERROR_CODE, STATUS, MESSAGE); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sluv/server/domain/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,8 @@ | ||
package com.sluv.server.domain.notice.repository; | ||
|
||
import com.sluv.server.domain.notice.entity.Notice; | ||
import com.sluv.server.domain.notice.repository.impl.NoticeRepositoryCustom; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long>, NoticeRepositoryCustom { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/sluv/server/domain/notice/repository/impl/NoticeRepositoryCustom.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.sluv.server.domain.notice.repository.impl; | ||
|
||
import com.sluv.server.domain.notice.entity.Notice; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface NoticeRepositoryCustom { | ||
Page<Notice> getAllNotice(Pageable pageable); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sluv/server/domain/notice/repository/impl/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,34 @@ | ||
package com.sluv.server.domain.notice.repository.impl; | ||
|
||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.sluv.server.domain.notice.entity.Notice; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
|
||
import java.util.List; | ||
|
||
import static com.sluv.server.domain.notice.entity.QNotice.notice; | ||
|
||
@RequiredArgsConstructor | ||
public class NoticeRepositoryImpl implements NoticeRepositoryCustom { | ||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public Page<Notice> getAllNotice(Pageable pageable) { | ||
List<Notice> content = jpaQueryFactory.selectFrom(notice) | ||
.orderBy(notice.createdAt.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
// Count Query | ||
JPAQuery<Notice> count = jpaQueryFactory.selectFrom(notice) | ||
.orderBy(notice.createdAt.desc()); | ||
|
||
|
||
return PageableExecutionUtils.getPage(content, pageable, () -> count.fetch().size()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/sluv/server/domain/notice/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,50 @@ | ||
package com.sluv.server.domain.notice.service; | ||
|
||
import com.sluv.server.domain.notice.dto.NoticeDetailResDto; | ||
import com.sluv.server.domain.notice.dto.NoticeSimpleResDto; | ||
import com.sluv.server.domain.notice.entity.Notice; | ||
import com.sluv.server.domain.notice.exception.NoticeNotFoundException; | ||
import com.sluv.server.domain.notice.repository.NoticeRepository; | ||
import com.sluv.server.global.common.response.PaginationResDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class NoticeService { | ||
private final NoticeRepository noticeRepository; | ||
|
||
/** | ||
* 공지사항 리스트 조회 | ||
*/ | ||
public PaginationResDto<NoticeSimpleResDto> getAllNotice(Pageable pageable) { | ||
// 공지사항 리스트 Page 조회 | ||
Page<Notice> noticePage = noticeRepository.getAllNotice(pageable); | ||
|
||
// Content가 될 Dto 제작 | ||
List<NoticeSimpleResDto> content = noticePage.stream() | ||
.map(NoticeSimpleResDto::of) | ||
.toList(); | ||
|
||
return PaginationResDto.<NoticeSimpleResDto>builder() | ||
.page(noticePage.getNumber()) | ||
.hasNext(noticePage.hasNext()) | ||
.content(content) | ||
.build(); | ||
} | ||
|
||
/** | ||
* 공지사항 상세 조회 | ||
*/ | ||
public NoticeDetailResDto getNoticeDetail(Long noticeId) { | ||
|
||
// noticeId에 해당하는 공지사항 조회 | ||
Notice notice = noticeRepository.findById(noticeId).orElseThrow(NoticeNotFoundException::new); | ||
|
||
return NoticeDetailResDto.of(notice); | ||
} | ||
} |