66import com .postdm .backend .domain .estimate .service .policy .AdminEstimatePolicy ;
77import com .postdm .backend .domain .estimate .service .policy .UserEstimatePolicy ;
88import com .postdm .backend .domain .member .domain .entity .Member ;
9+ import com .postdm .backend .domain .member .domain .entity .MemberRole ;
10+ import com .postdm .backend .domain .member .domain .repository .MemberRepository ;
11+ import com .postdm .backend .domain .member .dto .MemberPrincipalDto ;
912import com .postdm .backend .global .common .exception .CustomException ;
1013import com .postdm .backend .global .common .response .ErrorCode ;
14+ import lombok .RequiredArgsConstructor ;
1115import lombok .extern .slf4j .Slf4j ;
12- import org .springframework .security .core .Authentication ;
13- import org .springframework .security .core .context .SecurityContextHolder ;
1416import org .springframework .stereotype .Service ;
1517
1618import java .util .List ;
1719import java .util .stream .Collectors ;
1820
19- import static com .postdm .backend .domain .member .domain .entity .MemberRole .ADMIN ;
20-
2121@ Slf4j
2222@ Service
23+ @ RequiredArgsConstructor
2324public class EstimateService {
2425 private final EstimateRepository estimateRepository ;
26+ private final MemberRepository memberRepository ;
2527 private final AdminEstimatePolicy adminPolicy ;
2628 private final UserEstimatePolicy userPolicy ;
2729
28- public EstimateService (EstimateRepository estimateRepository ,
29- AdminEstimatePolicy adminPolicy ,
30- UserEstimatePolicy userPolicy ) {
31- this .estimateRepository = estimateRepository ;
32- this .adminPolicy = adminPolicy ;
33- this .userPolicy = userPolicy ;
34- }
35-
36- public EstimateResponseDto createEstimate (String content , Member member ) {
30+ public EstimateResponseDto createEstimate (String content , MemberPrincipalDto principal ) {
3731 if (content == null || content .trim ().isEmpty ()) {
3832 throw new CustomException (ErrorCode .ESTIMATE_NULL );
3933 }
4034
41- if (member == null ) {
42- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
43- if (authentication != null && authentication .getPrincipal () instanceof Member ) {
44- member = (Member ) authentication .getPrincipal ();
45- } else {
46- throw new CustomException (ErrorCode .AUTHORIZED_MEMBER_NOT_FOUND );
47- }
48- }
35+ Member member = findMemberOrThrow (principal .getId ());
4936
5037 Estimate estimate = new Estimate (content , member );
51- Estimate savedEstimate = estimateRepository .save (estimate );
38+ Estimate saved = estimateRepository .save (estimate );
5239
5340 return new EstimateResponseDto (
54- savedEstimate .getId (),
55- savedEstimate .getTitle (),
56- savedEstimate .getContent (),
57- savedEstimate .getCreatedAt (),
58- savedEstimate .getMember ().getId ()
41+ saved .getId (),
42+ saved .getTitle (),
43+ saved .getContent (),
44+ saved .getCreatedAt (),
45+ saved .getMember ().getId ()
5946 );
6047 }
6148
62- public List <EstimateResponseDto > getEstimates (Member member ) {
63- if (member == null ) {
64- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
65- if (authentication != null && authentication .getPrincipal () instanceof Member ) {
66- member = (Member ) authentication .getPrincipal ();
67- } else {
68- throw new CustomException (ErrorCode .AUTHORIZED_MEMBER_NOT_FOUND );
69- }
70- }
71-
72- List <Estimate > estimates ;
49+ public List <EstimateResponseDto > getEstimates (MemberPrincipalDto principal ) {
50+ Member member = findMemberOrThrow (principal .getId ());
7351
74- if (member .getRole () == ADMIN ) {
75- estimates = adminPolicy .getEstimates (member , estimateRepository );
76- } else {
77- estimates = userPolicy .getEstimates (member , estimateRepository );
78- }
52+ List <Estimate > estimates = (member .getRole () == MemberRole .ADMIN )
53+ ? adminPolicy .getEstimates (member , estimateRepository )
54+ : userPolicy .getEstimates (member , estimateRepository );
7955
8056 return estimates .stream ()
81- .map (estimate -> new EstimateResponseDto (
82- estimate .getId (),
83- estimate .getTitle (),
84- estimate .getContent (),
85- estimate .getCreatedAt (),
86- estimate .getMember ().getId ()
57+ .map (e -> new EstimateResponseDto (
58+ e .getId (), e .getTitle (), e .getContent (), e .getCreatedAt (), e .getMember ().getId ()
8759 ))
8860 .collect (Collectors .toList ());
8961 }
9062
91- public EstimateResponseDto getEstimateDetail (Long estimateId , Member member ) {
92- if (member == null ) {
93- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
94- if (authentication != null && authentication .getPrincipal () instanceof Member ) {
95- member = (Member ) authentication .getPrincipal ();
96- } else {
97- throw new CustomException (ErrorCode .AUTHORIZED_MEMBER_NOT_FOUND );
98- }
99- }
63+ public EstimateResponseDto getEstimateDetail (Long estimateId , MemberPrincipalDto principal ) {
64+ Member member = findMemberOrThrow (principal .getId ());
10065
10166 Estimate estimate = estimateRepository .findById (estimateId )
10267 .orElseThrow (() -> new CustomException (ErrorCode .ESTIMATE_NOT_FOUND ));
10368
104- // 권한 체크
105- if (member .getRole () != ADMIN &&
106- estimate .getMember ().getId () != member .getId ()) {
69+ if (member .getRole () != MemberRole .ADMIN
70+ && estimate .getMember ().getId () != member .getId ()) {
10771 throw new CustomException (ErrorCode .ACCESS_DENIED );
10872 }
10973
@@ -115,4 +79,9 @@ public EstimateResponseDto getEstimateDetail(Long estimateId, Member member) {
11579 estimate .getMember ().getId ()
11680 );
11781 }
82+
83+ private Member findMemberOrThrow (Long memberId ) {
84+ return memberRepository .findById (memberId )
85+ .orElseThrow (() -> new CustomException (ErrorCode .AUTHORIZED_MEMBER_NOT_FOUND ));
86+ }
11887}
0 commit comments