Skip to content

Commit

Permalink
Merge pull request #245 from KJBig/feature/S2D-62
Browse files Browse the repository at this point in the history
Feature/s2 d 62
  • Loading branch information
KJBig authored Aug 23, 2023
2 parents d26ec32 + cd84d27 commit 2b9eb98
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 0 deletions.
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()
);
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
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 {
}
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);
}
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());
}
}
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);
}
}

0 comments on commit 2b9eb98

Please sign in to comment.