-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdminNoticeQueryService.java
More file actions
75 lines (59 loc) · 2.82 KB
/
AdminNoticeQueryService.java
File metadata and controls
75 lines (59 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package gravit.code.admin.service;
import gravit.code.admin.dto.response.AdminNoticeDetailResponse;
import gravit.code.admin.dto.response.AdminNoticeSummaryResponse;
import gravit.code.global.dto.response.PageResponse;
import gravit.code.global.exception.domain.CustomErrorCode;
import gravit.code.global.exception.domain.RestApiException;
import gravit.code.notice.domain.Notice;
import gravit.code.notice.repository.NoticeRepository;
import gravit.code.user.domain.Role;
import gravit.code.user.domain.User;
import gravit.code.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class AdminNoticeQueryService {
private static final int PAGE_SIZE = 10;
private static final int SUMMARY_MAX_SIZE = 70;
// 정렬 규칙
private static final Sort NOTICE_LIST_SORT = Sort.by(
Sort.Order.desc("pinned"),
Sort.Order.desc("id")
);
private final NoticeRepository noticeRepository;
private final UserRepository userRepository;
@Transactional(readOnly = true)
public AdminNoticeDetailResponse getNoticeByAdmin(long userId, long noticeId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new RestApiException(CustomErrorCode.USER_NOT_FOUND));
if(user.getRole() != Role.ADMIN){
throw new RestApiException(CustomErrorCode.ADMIN_ONLY_FEATURE);
}
Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(()->new RestApiException(CustomErrorCode.NOTICE_NOT_FOUND));
return AdminNoticeDetailResponse.from(notice);
}
@Transactional(readOnly = true)
public PageResponse<AdminNoticeSummaryResponse> getNoticeSummaryByAdmin(long userId, int page) {
User user = userRepository.findById(userId)
.orElseThrow(()-> new RestApiException(CustomErrorCode.USER_NOT_FOUND));
if(user.getRole() != Role.ADMIN){
throw new RestApiException(CustomErrorCode.ADMIN_ONLY_FEATURE);
}
if(page < 1) throw new RestApiException(CustomErrorCode.PAGE_MUST_START_FROM_1);
int realPage = page - 1;
Pageable pageable = noticePageable(realPage);
Page<AdminNoticeSummaryResponse> summariesForAdmin = noticeRepository.findSummariesForAdmin(SUMMARY_MAX_SIZE, pageable);
return PageResponse.from(summariesForAdmin);
}
private Pageable noticePageable(int page) {
int safePage = Math.max(0, page);
return PageRequest.of(safePage, PAGE_SIZE, NOTICE_LIST_SORT);
}
}