Skip to content

Commit

Permalink
Merge pull request #64 from DONGA-ST-A/feature/63-notice-all-get
Browse files Browse the repository at this point in the history
[Feature] 공지사항 전체 조회 기능 구현
  • Loading branch information
kyeong-hyeok authored Sep 15, 2023
2 parents 23dcf39 + a7b8cb7 commit 669a84e
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/teamA/hicardi/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospe
.requestMatchers(mvcMatcherBuilder.pattern("/v3/api-docs/**")).permitAll()
.requestMatchers(mvcMatcherBuilder.pattern("/faqs/**")).permitAll()
.requestMatchers(mvcMatcherBuilder.pattern("/items/**")).permitAll()
.requestMatchers(mvcMatcherBuilder.pattern("/notices/**")).permitAll()
.anyRequest().authenticated())
.addFilterAfter(customJsonUsernamePasswordAuthenticationFilter(), LogoutFilter.class)
.addFilterBefore(jwtAuthenticationProcessingFilter(), CustomJsonAuthenticationFilter.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.teamA.hicardi.domain.notice.controller;

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

import com.teamA.hicardi.common.dto.PageResponseDto;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.service.NoticeService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/notices")
public class NoticeController {

private final NoticeService noticeService;

@GetMapping
public ResponseEntity<PageResponseDto> getAllNotices(Pageable pageable){
Page<NoticeGetResponseDto> response = noticeService.getAllNotices(pageable);
return PageResponseDto.of(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.teamA.hicardi.domain.notice.dto.response;

import java.time.LocalDate;

import com.teamA.hicardi.domain.notice.entity.Notice;

public record NoticeGetResponseDto(Long id, String category, String title, String content, String file, Boolean isTop, LocalDate createdDate) {

public static NoticeGetResponseDto from(Notice notice) {
return new NoticeGetResponseDto(notice.getId(), notice.getCategory().getDesc(), notice.getTitle(), notice.getContent(), notice.getFile(), notice.getIsTop(), notice.getCreatedDate().toLocalDate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ public class Notice extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String writer;

private String title;

@Column(columnDefinition = "TEXT")
private String content;

private Boolean isTop;

private String File;

@Enumerated(EnumType.STRING)
private NoticeCategory category;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.teamA.hicardi.domain.notice.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum NoticeCategory {
NECESSARY("필독"), UPDATE("업데이트"), DATA("자료"), NEWS("언론 보도");

private final String desc;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.teamA.hicardi.domain.notice.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.teamA.hicardi.domain.notice.entity.Notice;

public interface NoticeRepository extends JpaRepository<Notice, Long> {
@Query("SELECT n FROM Notice n ORDER BY n.isTop DESC, n.createdDate DESC")
Page<Notice> findAllOrderedByIsTopAndCreateDate(Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.teamA.hicardi.domain.notice.service;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.teamA.hicardi.domain.faq.dto.response.FaqGetResponseDto;
import com.teamA.hicardi.domain.faq.entity.Faq;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.entity.Notice;
import com.teamA.hicardi.domain.notice.repository.NoticeRepository;

import lombok.RequiredArgsConstructor;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
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));
return response;
}
}
13 changes: 10 additions & 3 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ INSERT INTO ItemTag (id, itemId, Tag, createdDate, modifiedDate) VALUES(16, 8, '
INSERT INTO ItemTag (id, itemId, Tag, createdDate, modifiedDate) VALUES(17, 9, 'SmartPhone', NOW(), NOW());
INSERT INTO ItemTag (id, itemId, Tag, createdDate, modifiedDate) VALUES(18, 9, 'AdditionalItem', NOW(), NOW());




INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(1, '의료기기 스타트업, 내수 수출 교두부 확보', '내용', 0, null, 'NEWS', '2023-08-15', '2023-08-15');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(2, '[2023.08.20 기준] 하이카디 플러스 패치 업데이트 안내', '내용', 1, null, 'UPDATE', '2023-08-20', '2023-08-20');
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(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');
INSERT INTO Notice(id, title, content, istop, file, category, createdDate, modifiedDate) VALUES(10, '메쥬, "하이카디" 전남대병원과 혁신제품 시범구매사업 진행', '내용', 0, null, 'NEWS', '2023-09-12', '2023-09-12');
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.teamA.hicardi.domain.notice.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
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.setup.MockMvcBuilders;
import org.springframework.web.filter.CharacterEncodingFilter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.service.NoticeService;

@ExtendWith(MockitoExtension.class)
public class NoticeControllerTest {
@InjectMocks
private NoticeController noticeController;
@Mock
private NoticeService noticeService;
private ObjectMapper objectMapper = new ObjectMapper();
private MockMvc mockMvc;

@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(noticeController)
.addFilter(new CharacterEncodingFilter("UTF-8", true))
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build();
}

@Test
void 공지사항_전체_조회() throws Exception {
PageRequest pageable = PageRequest.of(0, 5);
List<NoticeGetResponseDto> dtos = new ArrayList<>();
dtos.add(new NoticeGetResponseDto(1L, "NEWS", "뉴스", "내용1", "첨부파일1", false, LocalDate.now()));
dtos.add(new NoticeGetResponseDto(2L, "DATA", "자료", "내용2", "첨부파일2", true, LocalDate.now()));
Page<NoticeGetResponseDto> response = new PageImpl<>(dtos, pageable, 2);

given(noticeService.getAllNotices(any())).willReturn(response);
ResultActions result = mockMvc.perform(
get("/notices")
);

result.andExpect(status().isOk());
verify(noticeService, times(1)).getAllNotices(any());
}
}

0 comments on commit 669a84e

Please sign in to comment.