Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 공지사항 상세조회 기능 구현 #73

Merged
merged 7 commits into from
Sep 15, 2023
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.teamA.hicardi.domain.notice.controller;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.teamA.hicardi.common.dto.PageResponseDto;
import com.teamA.hicardi.common.dto.ResponseDto;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.service.NoticeService;
import com.teamA.hicardi.error.ErrorCode;
Expand All @@ -26,7 +30,7 @@ public class NoticeController {
private final NoticeService noticeService;

@GetMapping
public ResponseEntity<PageResponseDto> getAllNotices(Pageable pageable){
public ResponseEntity<PageResponseDto> getAllNotices(Pageable pageable) {
Page<NoticeGetResponseDto> response = noticeService.getAllNotices(pageable);
return PageResponseDto.of(response);
}
Expand All @@ -48,4 +52,10 @@ public ResponseEntity<PageResponseDto> searchFaq(@RequestParam String search, Pa
Page<NoticeGetResponseDto> response = noticeService.searchNotice(search, pageable);
return PageResponseDto.of(response);
}

@GetMapping("/{noticeId}")
public ResponseEntity<List<NoticeGetResponseDto>> getNotice(@PathVariable Long noticeId) {
List<NoticeGetResponseDto> response = noticeService.getNotice(noticeId);
return ResponseDto.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public interface NoticeRepository extends JpaRepository<Notice, Long> {
@Query("SELECT n FROM Notice n " +
" WHERE n.title LIKE %:search% OR n.content LIKE %:search% ")
Page<Notice> findAllBySearch(@Param("search") String search, Pageable pageable);

@Query("SELECT n FROM Notice n WHERE n.id < :noticeId ORDER BY n.id DESC LIMIT 1")
Notice findPreviousNotice(@Param("noticeId") Long noticeId);
@Query("SELECT n FROM Notice n WHERE n.id > :noticeId ORDER BY n.id LIMIT 1")
Notice findNextNotice(@Param("noticeId") Long noticeId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 바꿨네 좋아좋아 굿

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿 굿


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.teamA.hicardi.domain.notice.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -9,6 +12,8 @@
import com.teamA.hicardi.domain.notice.entity.Notice;
import com.teamA.hicardi.domain.notice.entity.NoticeCategory;
import com.teamA.hicardi.domain.notice.repository.NoticeRepository;
import com.teamA.hicardi.error.ErrorCode;
import com.teamA.hicardi.error.exception.custom.BusinessException;

import lombok.RequiredArgsConstructor;

Expand All @@ -18,6 +23,7 @@
public class NoticeService {

private final NoticeRepository noticeRepository;

public Page<NoticeGetResponseDto> getAllNotices(Pageable pageable) {
Page<Notice> notices = noticeRepository.findAllOrderedByIsTopAndCreateDate(pageable);
Page<NoticeGetResponseDto> response = notices.map(f -> NoticeGetResponseDto.from(f));
Expand All @@ -36,4 +42,30 @@ public Page<NoticeGetResponseDto> searchNotice(String search, Pageable pageable)
Page<NoticeGetResponseDto> response = notices.map(f -> NoticeGetResponseDto.from(f));
return response;
}

public List<NoticeGetResponseDto> getNotice(Long noticeId) {

Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new BusinessException(ErrorCode.NOTICE_NOT_FOUND));

Notice previousNotice = noticeRepository.findPreviousNotice(noticeId);

Notice nextNotice = noticeRepository.findNextNotice(noticeId);

List<NoticeGetResponseDto> response = new ArrayList<>();

if (previousNotice != null) {
response.add(NoticeGetResponseDto.from(previousNotice));
}else
response.add(null);

response.add(NoticeGetResponseDto.from(notice));

if (nextNotice != null) {
response.add(NoticeGetResponseDto.from(nextNotice));
}else
response.add(null);

return response;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공지사항 상세 조회에서 이전글이랑 다음글의 카테고리, 제목, 날짜 불러와야 해서 그 부분 수정하고 Dto 새로 만들어야 할 듯합니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
3 changes: 2 additions & 1 deletion src/main/java/com/teamA/hicardi/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum ErrorCode {
WRONG_SEARCH(BAD_REQUEST, "검색어를 입력해야 합니다."),
INVALID_TOKEN(UNAUTHORIZED, "잘못된 토큰입니다."),
CART_NOT_FOUND(NOT_FOUND, "해당 장바구니를 찾을 수 없습니다."),
ITEM_NOT_FOUND(NOT_FOUND, "해당 상품을 찾을 수 없습니다.");
ITEM_NOT_FOUND(NOT_FOUND, "해당 상품을 찾을 수 없습니다."),
NOTICE_NOT_FOUND(NOT_FOUND, "해당 공지사항을 찾을 수 없습니다.");



Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modif
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(3, '의정부 튼튼어린이병원, 심질환 환아 감시 하이카디플러스 도입', '내용', 0, null, 'NEWS', '2023-08-20', '2023-08-20');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(4, '[2023.09.17 마감] 하이카디 체험단 모집', '내용', 1, '첨부파일', 'NECESSARY', '2023-09-08', '2023-09-08');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(5, '하이카디 갤럭시A13 연동 방법', '내용', 0, '첨부파일', 'DATA', '2023-09-08', '2023-09-08');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(6, '[2023.09.15 마감] 하이카디플러스 소개 및 설명회 참여 인원 모집', '내용', 1, '첨부파일', 'NECESSARY', '2023-09-09', '2023-09-09');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(6, '[2023.09.15 마감] 하이카디플러스 소개 및 설명회 참여 인원 모집', '내용', 1, 'https://hicardi.s3.ap-northeast-2.amazonaws.com/notice1.svg', 'NECESSARY', '2023-09-09', '2023-09-09');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(7, '하이카디 패치 사용법', '내용', 0, '첨부파일', 'DATA', '2023-09-09', '2023-09-09');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(8, '하이카디 갤럭시A13 연동 방법', '내용', 0, '첨부파일', 'DATA', '2023-09-09', '2023-09-09');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(9, '하이카디 패치 사용법', '내용', 0, '첨부파일', 'DATA', '2023-09-10', '2023-09-10');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.filter.CharacterEncodingFilter;

Expand Down Expand Up @@ -99,4 +100,21 @@ void setUp() {
result.andExpect(status().isOk());
verify(noticeService, times(1)).searchNotice(anyString(), any());
}

@Test
void 공지사항_상세_조회() throws Exception{

List<NoticeGetResponseDto> dtos = new ArrayList<>();
dtos.add(new NoticeGetResponseDto(1L, "DATA", "자료", "내용2", "첨부파일2", true, LocalDate.now()));
dtos.add(new NoticeGetResponseDto(2L, "DATA", "자료", "내용2", "첨부파일2", true, LocalDate.now()));

given(noticeService.getNotice(any())).willReturn(dtos);
ResultActions result = mockMvc.perform(
get("/notices/1")
);

result.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
verify(noticeService, times(1)).getNotice(any());
}
}
Loading