Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.comatching.item.domain.notice.dto;

import java.time.LocalDateTime;

import com.comatching.item.domain.notice.entity.Notice;

public record AdminNoticeResponse(
Long noticeId,
String title,
String content,
LocalDateTime startTime,
LocalDateTime endTime,
boolean active
) {
public static AdminNoticeResponse from(Notice notice, LocalDateTime currentTime) {
return new AdminNoticeResponse(
notice.getId(),
notice.getTitle(),
notice.getContent(),
notice.getStartTime(),
notice.getEndTime(),
isActive(notice, currentTime)
);
}

private static boolean isActive(Notice notice, LocalDateTime currentTime) {
return !notice.getStartTime().isAfter(currentTime) && !notice.getEndTime().isBefore(currentTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public interface NoticeRepository extends JpaRepository<Notice, Long> {

List<Notice> findAllByOrderByStartTimeDescIdDesc();

List<Notice> findAllByStartTimeLessThanEqualAndEndTimeGreaterThanEqualOrderByStartTimeDescIdDesc(
LocalDateTime currentTime,
LocalDateTime currentTime2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.comatching.item.domain.notice.dto.AdminNoticeResponse;
import com.comatching.item.domain.notice.dto.ActiveNoticeResponse;
import com.comatching.item.domain.notice.dto.NoticeCreateRequest;
import com.comatching.item.domain.notice.dto.NoticeUpdateRequest;
Expand All @@ -15,4 +16,6 @@ public interface NoticeService {
void deleteNotice(Long noticeId);

List<ActiveNoticeResponse> getActiveNotices();

List<AdminNoticeResponse> getAdminNotices();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.comatching.common.exception.BusinessException;
import com.comatching.common.exception.code.GeneralErrorCode;
import com.comatching.item.domain.notice.dto.AdminNoticeResponse;
import com.comatching.item.domain.notice.dto.ActiveNoticeResponse;
import com.comatching.item.domain.notice.dto.NoticeCreateRequest;
import com.comatching.item.domain.notice.dto.NoticeUpdateRequest;
Expand Down Expand Up @@ -62,6 +63,16 @@ public List<ActiveNoticeResponse> getActiveNotices() {
.toList();
}

@Override
@Transactional(readOnly = true)
public List<AdminNoticeResponse> getAdminNotices() {
LocalDateTime currentTime = LocalDateTime.now();
return noticeRepository.findAllByOrderByStartTimeDescIdDesc()
.stream()
.map(notice -> AdminNoticeResponse.from(notice, currentTime))
.toList();
}

private Notice findNoticeOrThrow(Long noticeId) {
return noticeRepository.findById(noticeId)
.orElseThrow(() -> new BusinessException(GeneralErrorCode.NOT_FOUND, "공지사항을 찾을 수 없습니다."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.comatching.common.domain.enums.MemberRole;
import com.comatching.common.dto.member.MemberInfo;
import com.comatching.common.dto.response.ApiResponse;
import com.comatching.item.domain.notice.dto.AdminNoticeResponse;
import com.comatching.item.domain.notice.dto.ActiveNoticeResponse;
import com.comatching.item.domain.notice.dto.NoticeCreateRequest;
import com.comatching.item.domain.notice.dto.NoticeUpdateRequest;
Expand All @@ -35,6 +36,15 @@ public class NoticeController {

private final NoticeService noticeService;

@RequireRole(MemberRole.ROLE_ADMIN)
@Operation(summary = "관리자 공지사항 목록 조회", description = "관리자가 등록된 전체 공지사항을 노출 시작시간 내림차순으로 조회합니다.")
@GetMapping("/admin/notices")
public ResponseEntity<ApiResponse<List<AdminNoticeResponse>>> getAdminNotices(
@CurrentMember MemberInfo memberInfo
) {
return ResponseEntity.ok(ApiResponse.ok(noticeService.getAdminNotices()));
}

@RequireRole(MemberRole.ROLE_ADMIN)
@Operation(summary = "공지사항 등록", description = "관리자가 제목, 내용, 시작시간, 종료시간으로 공지사항을 등록합니다.")
@PostMapping("/admin/notices")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.comatching.common.exception.BusinessException;
import com.comatching.common.exception.code.GeneralErrorCode;
import com.comatching.item.domain.notice.dto.AdminNoticeResponse;
import com.comatching.item.domain.notice.dto.ActiveNoticeResponse;
import com.comatching.item.domain.notice.dto.NoticeCreateRequest;
import com.comatching.item.domain.notice.dto.NoticeUpdateRequest;
Expand Down Expand Up @@ -122,6 +123,40 @@ void shouldReturnActiveNotices() {
assertThat(responses.get(0).content()).isEqualTo("한 줄\n두 줄");
}

@Test
@DisplayName("관리자 공지사항 목록 조회 시 전체 공지사항과 활성 여부를 반환한다")
void shouldReturnAdminNotices() {
// given
LocalDateTime now = LocalDateTime.now();
Notice activeNotice = Notice.builder()
.title("활성 공지")
.content("활성 내용")
.startTime(now.minusDays(1))
.endTime(now.plusDays(1))
.build();
ReflectionTestUtils.setField(activeNotice, "id", 11L);

Notice expiredNotice = Notice.builder()
.title("종료 공지")
.content("종료 내용")
.startTime(now.minusDays(3))
.endTime(now.minusDays(2))
.build();
ReflectionTestUtils.setField(expiredNotice, "id", 12L);

given(noticeRepository.findAllByOrderByStartTimeDescIdDesc())
.willReturn(List.of(activeNotice, expiredNotice));

// when
List<AdminNoticeResponse> responses = noticeService.getAdminNotices();

// then
assertThat(responses).hasSize(2);
assertThat(responses).extracting(AdminNoticeResponse::noticeId).containsExactly(11L, 12L);
assertThat(responses).extracting(AdminNoticeResponse::title).containsExactly("활성 공지", "종료 공지");
assertThat(responses).extracting(AdminNoticeResponse::active).containsExactly(true, false);
}

@Test
@DisplayName("관리자 공지사항 수정 시 제목, 내용, 노출 기간을 변경한다")
void shouldUpdateNotice() {
Expand Down