-
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 #64 from DONGA-ST-A/feature/63-notice-all-get
[Feature] 공지사항 전체 조회 기능 구현
- Loading branch information
Showing
9 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/teamA/hicardi/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,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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/teamA/hicardi/domain/notice/dto/response/NoticeGetResponseDto.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,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()); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/teamA/hicardi/domain/notice/entity/NoticeCategory.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,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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/teamA/hicardi/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,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); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/teamA/hicardi/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,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; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/test/java/com/teamA/hicardi/domain/notice/controller/NoticeControllerTest.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,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()); | ||
} | ||
} |