diff --git a/build.gradle b/build.gradle index 11ddf4ee..30b93c3f 100644 --- a/build.gradle +++ b/build.gradle @@ -130,6 +130,8 @@ jacocoTestReport { fileTree(dir: it, exclude: [ '**/exception/**' // 테스트 커버리지 측정 대상에서 제거 ,'**/dto/**' // 테스트 커버리지 측정 대상에서 제거 + ,'**/config/**' + ,'**/health_check/**' ,'**/Dotori/DotoriApplication*' ,'**/Dotori/**/Q*' // Qclass 테스트 커버리지 측정 대상에서 제거 ]) diff --git a/src/main/java/com/server/Dotori/domain/board/controller/common/CommonBoardController.java b/src/main/java/com/server/Dotori/domain/board/controller/common/CommonBoardController.java deleted file mode 100644 index 32e86f6f..00000000 --- a/src/main/java/com/server/Dotori/domain/board/controller/common/CommonBoardController.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.server.Dotori.domain.board.controller.common; - -import com.server.Dotori.domain.board.service.BoardService; -import com.server.Dotori.global.response.ResponseService; -import com.server.Dotori.global.response.result.SingleResult; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequiredArgsConstructor -@RequestMapping("/v1/board") -public class CommonBoardController { - - private final BoardService boardService; - private final ResponseService responseService; - - @GetMapping(value = "/count") - @ResponseStatus( HttpStatus.OK ) - public SingleResult countBoard() { - return responseService.getSingleResult(boardService.countBoard()); - } -} diff --git a/src/main/java/com/server/Dotori/domain/board/repository/BoardRepository.java b/src/main/java/com/server/Dotori/domain/board/repository/BoardRepository.java index a4f075d9..cf90bb36 100644 --- a/src/main/java/com/server/Dotori/domain/board/repository/BoardRepository.java +++ b/src/main/java/com/server/Dotori/domain/board/repository/BoardRepository.java @@ -6,4 +6,5 @@ @Repository public interface BoardRepository extends JpaRepository, BoardRepositoryCustom { + Board findTop1ByOrderByCreatedDateDesc(); // 제일 마지막에 등록된 공지사항을 불러온다. } diff --git a/src/main/java/com/server/Dotori/domain/board/service/BoardService.java b/src/main/java/com/server/Dotori/domain/board/service/BoardService.java index 0971bc4b..fa996efc 100644 --- a/src/main/java/com/server/Dotori/domain/board/service/BoardService.java +++ b/src/main/java/com/server/Dotori/domain/board/service/BoardService.java @@ -19,6 +19,4 @@ public interface BoardService { Board updateBoard(Long boardId, BoardDto boardUpdateDto); void deleteBoard(Long boardId); - - Long countBoard(); } diff --git a/src/main/java/com/server/Dotori/domain/board/service/Impl/BoardServiceImpl.java b/src/main/java/com/server/Dotori/domain/board/service/Impl/BoardServiceImpl.java index 0d83cba1..5f34289c 100644 --- a/src/main/java/com/server/Dotori/domain/board/service/Impl/BoardServiceImpl.java +++ b/src/main/java/com/server/Dotori/domain/board/service/Impl/BoardServiceImpl.java @@ -126,14 +126,4 @@ public void deleteBoard(Long boardId) { boardRepository.deleteById(board.getId()); } } - - /** - * 공지사항 카운트 서비스 로직 (로그인 되지 않은 유저 사용가능) - * @return board count - * @author 배태현 - */ - @Override - public Long countBoard() { - return boardRepository.count(); - } } diff --git a/src/main/java/com/server/Dotori/domain/main_page/controller/BoardAlarmController.java b/src/main/java/com/server/Dotori/domain/main_page/controller/BoardAlarmController.java new file mode 100644 index 00000000..5029b861 --- /dev/null +++ b/src/main/java/com/server/Dotori/domain/main_page/controller/BoardAlarmController.java @@ -0,0 +1,32 @@ +package com.server.Dotori.domain.main_page.controller; + +import com.server.Dotori.domain.main_page.service.BoardAlarmService; +import com.server.Dotori.global.response.ResponseService; +import com.server.Dotori.global.response.result.SingleResult; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/v1/board") +public class BoardAlarmController { + + private final BoardAlarmService boardService; + private final ResponseService responseService; + + @GetMapping(value = "/alarm") + @ResponseStatus( HttpStatus.OK ) + @ApiImplicitParams({ + @ApiImplicitParam(name = "Authorization", value = "로그인 성공 후 access_token", required = true, dataType = "String", paramType = "header"), + @ApiImplicitParam(name = "RefreshToken", value = "로그인 성공 후 refresh_token", required = false, dataType = "String", paramType = "header") + }) + public SingleResult getBoardAlarmInfo() { + return responseService.getSingleResult(boardService.getBoardAlarmInfo()); + } +} diff --git a/src/main/java/com/server/Dotori/domain/main_page/dto/BoardAlarmDto.java b/src/main/java/com/server/Dotori/domain/main_page/dto/BoardAlarmDto.java new file mode 100644 index 00000000..3bd09d52 --- /dev/null +++ b/src/main/java/com/server/Dotori/domain/main_page/dto/BoardAlarmDto.java @@ -0,0 +1,22 @@ +package com.server.Dotori.domain.main_page.dto; + +import com.server.Dotori.domain.member.enumType.Role; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.util.List; + +import static lombok.AccessLevel.PROTECTED; + +@Getter @Builder +@NoArgsConstructor(access = PROTECTED) @AllArgsConstructor +public class BoardAlarmDto { + + private Long id; + private String title; + private List writerRole; + private LocalDate lastBoardWriteDate; +} diff --git a/src/main/java/com/server/Dotori/domain/main_page/service/BoardAlarmService.java b/src/main/java/com/server/Dotori/domain/main_page/service/BoardAlarmService.java new file mode 100644 index 00000000..7230d0ca --- /dev/null +++ b/src/main/java/com/server/Dotori/domain/main_page/service/BoardAlarmService.java @@ -0,0 +1,8 @@ +package com.server.Dotori.domain.main_page.service; + +import com.server.Dotori.domain.main_page.dto.BoardAlarmDto; + +public interface BoardAlarmService { + + BoardAlarmDto getBoardAlarmInfo(); +} diff --git a/src/main/java/com/server/Dotori/domain/main_page/service/Impl/BoardAlarmServiceImpl.java b/src/main/java/com/server/Dotori/domain/main_page/service/Impl/BoardAlarmServiceImpl.java new file mode 100644 index 00000000..e13326af --- /dev/null +++ b/src/main/java/com/server/Dotori/domain/main_page/service/Impl/BoardAlarmServiceImpl.java @@ -0,0 +1,32 @@ +package com.server.Dotori.domain.main_page.service.Impl; + +import com.server.Dotori.domain.board.Board; +import com.server.Dotori.domain.main_page.dto.BoardAlarmDto; +import com.server.Dotori.domain.board.repository.BoardRepository; +import com.server.Dotori.domain.main_page.service.BoardAlarmService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class BoardAlarmServiceImpl implements BoardAlarmService { + + private final BoardRepository boardRepository; + + /** + * 새로운 공지사항 등록 여부를 판단할 수 있는 서비스 로직 (로그인 된 유저 사용가능) + * @return board count + * @author 배태현 + */ + @Override + public BoardAlarmDto getBoardAlarmInfo() { + Board lastWriteBoard = boardRepository.findTop1ByOrderByCreatedDateDesc(); + + return BoardAlarmDto.builder() + .id(lastWriteBoard.getId()) + .title(lastWriteBoard.getTitle()) + .writerRole(lastWriteBoard.getMember().getRoles()) + .lastBoardWriteDate(lastWriteBoard.getCreatedDate().toLocalDate()) + .build(); + } +} diff --git a/src/main/java/com/server/Dotori/global/security/SecurityConfig.java b/src/main/java/com/server/Dotori/global/security/SecurityConfig.java index 9b5f81f5..41267f49 100644 --- a/src/main/java/com/server/Dotori/global/security/SecurityConfig.java +++ b/src/main/java/com/server/Dotori/global/security/SecurityConfig.java @@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; @@ -20,7 +21,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; @RequiredArgsConstructor -@EnableWebSecurity(debug = true) +@EnableWebSecurity(debug = false) @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @@ -68,7 +69,6 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/v1/members/password/email/check").permitAll() .antMatchers("/v1/members/gender").permitAll() .antMatchers("/v1/refresh").permitAll() - .antMatchers("/v1/board/count").permitAll() // 권한 별 url 접근 .antMatchers("/v1/admin/**").hasRole("ADMIN") diff --git a/src/test/java/com/server/Dotori/domain/board/service/BoardServiceTest.java b/src/test/java/com/server/Dotori/domain/board/service/BoardServiceTest.java index 97ffcc26..a90d1b2b 100644 --- a/src/test/java/com/server/Dotori/domain/board/service/BoardServiceTest.java +++ b/src/test/java/com/server/Dotori/domain/board/service/BoardServiceTest.java @@ -177,20 +177,4 @@ public void deleteBoardTest() { List findAll = boardRepository.findAll(); assertTrue(findAll.size() == 0); } - - @Test - @DisplayName("공지사항 카운트 조회 테스트") - public void countBoardTest() { - // given // when - boardService.createBoard( - BoardDto.builder() - .title("도토리 공지사항") - .content("도토리 공지사항 생성 테스트") - .build(), null - ); - Long boardCount = boardService.countBoard(); - - // then - assertTrue(boardCount.equals(1L)); - } } \ No newline at end of file diff --git a/src/test/java/com/server/Dotori/domain/main_page/board_alarm/service/BoardAlarmServiceTest.java b/src/test/java/com/server/Dotori/domain/main_page/board_alarm/service/BoardAlarmServiceTest.java new file mode 100644 index 00000000..a3f733b0 --- /dev/null +++ b/src/test/java/com/server/Dotori/domain/main_page/board_alarm/service/BoardAlarmServiceTest.java @@ -0,0 +1,90 @@ +package com.server.Dotori.domain.main_page.board_alarm.service; + +import com.server.Dotori.domain.board.dto.BoardDto; +import com.server.Dotori.domain.board.service.BoardService; +import com.server.Dotori.domain.main_page.dto.BoardAlarmDto; +import com.server.Dotori.domain.main_page.service.BoardAlarmService; +import com.server.Dotori.domain.member.dto.MemberDto; +import com.server.Dotori.domain.member.enumType.Gender; +import com.server.Dotori.domain.member.repository.member.MemberRepository; +import com.server.Dotori.global.util.CurrentMemberUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.util.List; + +import static com.server.Dotori.domain.member.enumType.Role.ROLE_ADMIN; +import static com.server.Dotori.domain.member.enumType.Role.ROLE_MEMBER; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Transactional +@SpringBootTest +class BoardAlarmServiceTest { + + @Autowired private MemberRepository memberRepository; + @Autowired private PasswordEncoder passwordEncoder; + @Autowired private BoardService boardService; + @Autowired private BoardAlarmService boardAlarmService; + + @BeforeEach + @DisplayName("로그인 되어있는 유저를 세팅하는 테스트") + void currentUser() { + //given + MemberDto memberDto = MemberDto.builder() + .memberName("배태현") + .stuNum("2409") + .password("0809") + .email("s20032@gsm.hs.kr") + .gender(Gender.MAN) + .build(); + memberRepository.save( + memberDto.toEntity( + passwordEncoder.encode(memberDto.getPassword()) + ) + ); + System.out.println("======== saved ========="); + + // when login session 발급 + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( + memberDto.getEmail(), + memberDto.getPassword(), + List.of(new SimpleGrantedAuthority(ROLE_ADMIN.name()))); + SecurityContext context = SecurityContextHolder.getContext(); + context.setAuthentication(token); + System.out.println("================================="); + System.out.println(context); + + //then + String currentMemberEmail = CurrentMemberUtil.getCurrentEmail(); + assertEquals("s20032@gsm.hs.kr", currentMemberEmail); + } + + @Test + @DisplayName("가장 마지막에 등록된 공지사항이 잘 가져와지나요 ?") + public void boardAlarmTest() { + // given // when + boardService.createBoard( + BoardDto.builder() + .title("도토리 공지사항") + .content("도토리 공지사항 생성 테스트") + .build(), null + ); + + BoardAlarmDto boardAlarmInfo = boardAlarmService.getBoardAlarmInfo(); + + // then + assertEquals("도토리 공지사항", boardAlarmInfo.getTitle()); + assertEquals(ROLE_MEMBER.name(), boardAlarmInfo.getWriterRole().get(0).name()); + assertEquals(LocalDate.now(), boardAlarmInfo.getLastBoardWriteDate()); + } +} \ No newline at end of file