|
1 | 1 | package com.synergy.backend.domain.session.service; |
2 | 2 |
|
| 3 | +import java.util.List; |
| 4 | +import java.util.UUID; |
| 5 | + |
| 6 | +import org.springframework.stereotype.Service; |
| 7 | +import org.springframework.transaction.annotation.Transactional; |
| 8 | +import org.springframework.web.multipart.MultipartFile; |
| 9 | + |
3 | 10 | import com.google.zxing.WriterException; |
4 | 11 | import com.synergy.backend.domain.conference.entity.Conference; |
5 | 12 | import com.synergy.backend.domain.conference.exception.NotFoundConference; |
|
11 | 18 | import com.synergy.backend.domain.member.repository.AdminRepository; |
12 | 19 | import com.synergy.backend.domain.member.repository.AttendeeRepository; |
13 | 20 | import com.synergy.backend.domain.qrCode.service.QrService; |
| 21 | +import com.synergy.backend.domain.session.dto.questionDto.QuestionResDto; |
14 | 22 | import com.synergy.backend.domain.session.dto.sessionDto.SessionDetailResDto; |
15 | 23 | import com.synergy.backend.domain.session.dto.sessionDto.SessionReqDto; |
16 | 24 | import com.synergy.backend.domain.session.dto.sessionDto.SessionResDto; |
17 | | -import com.synergy.backend.domain.session.dto.questionDto.QuestionResDto; |
18 | 25 | import com.synergy.backend.domain.session.entity.AttendeeSession; |
19 | 26 | import com.synergy.backend.domain.session.entity.Session; |
20 | 27 | import com.synergy.backend.domain.session.exception.NotAttendedSession; |
|
23 | 30 | import com.synergy.backend.domain.session.repository.sessionQuestionRepository.SessionQuestionRepository; |
24 | 31 | import com.synergy.backend.domain.session.repository.sessionRepository.SessionRepository; |
25 | 32 | import com.synergy.backend.global.exception.AuthorizedException; |
26 | | -import com.synergy.backend.global.util.file.dto.FileInformationDto; |
27 | 33 | import com.synergy.backend.global.util.file.util.FileS3Util; |
| 34 | + |
28 | 35 | import lombok.RequiredArgsConstructor; |
29 | 36 | import lombok.extern.slf4j.Slf4j; |
30 | | -import org.springframework.stereotype.Service; |
31 | | -import org.springframework.transaction.annotation.Transactional; |
32 | | -import org.springframework.web.multipart.MultipartFile; |
33 | | - |
34 | | -import java.util.List; |
35 | | -import java.util.UUID; |
36 | 37 |
|
37 | 38 | @Slf4j |
38 | 39 | @Service |
39 | 40 | @RequiredArgsConstructor |
40 | 41 | public class SessionServiceImpl implements SessionService { |
41 | 42 |
|
42 | | - private final AdminRepository adminRepository; |
43 | | - private final ConferenceRepository conferenceRepository; |
44 | | - private final SessionRepository sessionRepository; |
45 | | - private final AttendeeSessionRepository attendeeSessionRepository; |
46 | | - private final SessionQuestionRepository sessionQuestionRepository; |
47 | | - private final AttendeeRepository attendeeRepository; |
48 | | - private final FileS3Util fileS3Util; |
49 | | - |
50 | | - private final QrService qrService; |
51 | | - |
52 | | - @Transactional |
53 | | - @Override |
54 | | - public void createSession(String identifier, String router, Long conferenceId, SessionReqDto reqDto, MultipartFile multipartFile) throws WriterException { |
55 | | - Admin admin = findIfAdminExists(identifier); |
56 | | - Conference conference = ifConferenceExists(conferenceId); |
57 | | - |
58 | | - String secretCode = UUID.randomUUID().toString(); |
59 | | - |
60 | | - Session session = Session.of(reqDto, secretCode, conference); |
61 | | - admin.addSession(session); |
62 | | - |
63 | | - sessionRepository.save(session); |
64 | | - |
65 | | - String url = router + "/session/" + session.getId(); |
66 | | - byte[] qrCode = qrService.generateQRCode(url, secretCode); |
67 | | - log.info("sessionId: {}", session.getId()); |
68 | | - log.info("qrCode: {}", qrCode); |
69 | | - session.addQRCode(fileS3Util.uploadQRCode(qrCode, session.getTitle())); |
70 | | - if(multipartFile != null) { |
71 | | - session.addImage(fileS3Util.uploadFile(multipartFile)); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - @Transactional(readOnly = true) |
76 | | - @Override |
77 | | - public List<SessionResDto> getSessions(String identifier, Long conferenceId) { |
78 | | - Conference conference = ifConferenceExists(conferenceId); |
79 | | - List<Session> sessions = sessionRepository.findAllByConferenceOrderByStartTime(conference); |
80 | | - |
81 | | - return sessions.stream().map(SessionResDto::from).toList(); |
82 | | - } |
83 | | - |
84 | | - @Transactional(readOnly = true) |
85 | | - @Override |
86 | | - public SessionDetailResDto getSessionInfo(String identifier, RoleType role, Long conferenceId, Long sessionId) { |
87 | | - Conference conference = ifConferenceExists(conferenceId); |
88 | | - Session session = findByConferenceId(sessionId, conference); |
89 | | - Boolean qualified = Boolean.FALSE; |
90 | | - |
91 | | - try { |
92 | | - if(role.equals(RoleType.ATTENDEE)) { |
93 | | - Attendee attendee = findIfAttendeeExists(identifier); |
94 | | - ifAttendeeSessionExists(sessionId, attendee.getId()); |
95 | | - qualified = Boolean.TRUE; |
96 | | - } |
97 | | - List<QuestionResDto> questions = getQuestions(conference, sessionId); |
98 | | - return SessionDetailResDto.from(session, questions, qualified); |
99 | | - } catch (Exception e) { |
100 | | - return SessionDetailResDto.from(session, List.of(), qualified); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - @Transactional |
105 | | - @Override |
106 | | - public void updateSession(String identifier, Long sessionId, SessionReqDto reqDto, MultipartFile multipartFile) { |
107 | | - Session session = ifSessionExists(sessionId); |
108 | | - Admin admin = findIfAdminExists(identifier); |
109 | | - verifyAuthenticationRole(session, admin); |
110 | | - |
111 | | - if(multipartFile != null) { |
112 | | - session.addImage(fileS3Util.uploadFile(multipartFile)); |
113 | | - |
114 | | - } |
115 | | - session.updateSession(reqDto); |
116 | | - } |
117 | | - |
118 | | - @Override |
119 | | - public void deleteSession(String identifier, Long sessionId) { |
120 | | - Session session = ifSessionExists(sessionId); |
121 | | - Admin admin = findIfAdminExists(identifier); |
122 | | - verifyAuthenticationRole(session, admin); |
123 | | - |
124 | | - session.removeAllAdmins(); |
125 | | - |
126 | | - sessionRepository.delete(session); |
127 | | - } |
128 | | - |
129 | | - // --------------------------------- private method ---------------------------------------- |
130 | | - |
131 | | - private Session findByConferenceId(Long sessionId, Conference conference) { |
132 | | - return sessionRepository.findByIdAndConference(sessionId, conference).orElseThrow(NotFoundSession::new); |
133 | | - } |
134 | | - |
135 | | - private Admin findIfAdminExists(String identifier) { |
136 | | - return adminRepository.findByAdminAuthCode(identifier).orElseThrow(NotFoundUserException::new); |
137 | | - } |
138 | | - |
139 | | - private Attendee findIfAttendeeExists(String identifier) { |
140 | | - return attendeeRepository.findByEmail(identifier).orElseThrow(NotFoundUserException::new); |
141 | | - } |
142 | | - |
143 | | - private List<QuestionResDto> getQuestions(Conference conference, Long sessionId) { |
144 | | - findByConferenceId(sessionId, conference); |
145 | | - |
146 | | - return sessionQuestionRepository.findBySessionIdJoinAttendeeSession(sessionId); |
147 | | - } |
148 | | - |
149 | | - private AttendeeSession ifAttendeeSessionExists(Long sessionId, Long attendeeId) { |
150 | | - return attendeeSessionRepository.findBySessionIdAndAttendeeId(sessionId, attendeeId) |
151 | | - .orElseThrow(NotAttendedSession::new); |
152 | | - } |
153 | | - |
154 | | - private Conference ifConferenceExists(Long conferenceId) { |
155 | | - return conferenceRepository.findById(conferenceId).orElseThrow(NotFoundConference::new); |
156 | | - } |
157 | | - |
158 | | - private Session ifSessionExists(Long sessionId) { |
159 | | - return sessionRepository.findById(sessionId).orElseThrow(NotFoundSession::new); |
160 | | - } |
161 | | - |
162 | | - private void verifyAuthenticationRole(Session session, Admin admin) { |
163 | | - if(!sessionRepository.existsByIdAndAdmins_Id(session.getId(), admin.getId())){ |
164 | | - throw new AuthorizedException(); |
165 | | - } |
166 | | - } |
| 43 | + private final AdminRepository adminRepository; |
| 44 | + private final ConferenceRepository conferenceRepository; |
| 45 | + private final SessionRepository sessionRepository; |
| 46 | + private final AttendeeSessionRepository attendeeSessionRepository; |
| 47 | + private final SessionQuestionRepository sessionQuestionRepository; |
| 48 | + private final AttendeeRepository attendeeRepository; |
| 49 | + private final FileS3Util fileS3Util; |
| 50 | + |
| 51 | + private final QrService qrService; |
| 52 | + |
| 53 | + @Transactional |
| 54 | + @Override |
| 55 | + public void createSession(String identifier, String router, Long conferenceId, SessionReqDto reqDto, |
| 56 | + MultipartFile multipartFile) throws WriterException { |
| 57 | + Admin admin = findIfAdminExists(identifier); |
| 58 | + Conference conference = ifConferenceExists(conferenceId); |
| 59 | + |
| 60 | + String secretCode = UUID.randomUUID().toString(); |
| 61 | + |
| 62 | + Session session = Session.of(reqDto, secretCode, conference); |
| 63 | + admin.addSession(session); |
| 64 | + |
| 65 | + sessionRepository.save(session); |
| 66 | + |
| 67 | + String url = router + "/session/" + session.getId(); |
| 68 | + byte[] qrCode = qrService.generateQRCode(url, secretCode); |
| 69 | + log.info("sessionId: {}", session.getId()); |
| 70 | + log.info("qrCode: {}", qrCode); |
| 71 | + session.addQRCode(fileS3Util.uploadQRCode(qrCode, session.getTitle())); |
| 72 | + if (multipartFile != null) { |
| 73 | + session.addImage(fileS3Util.uploadFile(multipartFile)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Transactional(readOnly = true) |
| 78 | + @Override |
| 79 | + public List<SessionResDto> getSessions(String identifier, Long conferenceId) { |
| 80 | + Conference conference = ifConferenceExists(conferenceId); |
| 81 | + List<Session> sessions = sessionRepository.findAllByConferenceOrderByStartTime(conference); |
| 82 | + |
| 83 | + return sessions.stream().map(SessionResDto::from).toList(); |
| 84 | + } |
| 85 | + |
| 86 | + @Transactional(readOnly = true) |
| 87 | + @Override |
| 88 | + public SessionDetailResDto getSessionInfo(String identifier, RoleType role, Long conferenceId, Long sessionId) { |
| 89 | + Conference conference = ifConferenceExists(conferenceId); |
| 90 | + Session session = findByConferenceId(sessionId, conference); |
| 91 | + Boolean qualified = Boolean.FALSE; |
| 92 | + |
| 93 | + try { |
| 94 | + if (role.equals(RoleType.ATTENDEE)) { |
| 95 | + Attendee attendee = findIfAttendeeExists(identifier); |
| 96 | + ifAttendeeSessionExists(sessionId, attendee.getId()); |
| 97 | + qualified = Boolean.TRUE; |
| 98 | + } |
| 99 | + List<QuestionResDto> questions = getQuestions(conference, sessionId); |
| 100 | + return SessionDetailResDto.from(session, questions, qualified); |
| 101 | + } catch (Exception e) { |
| 102 | + return SessionDetailResDto.from(session, List.of(), qualified); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Transactional |
| 107 | + @Override |
| 108 | + public void updateSession(String identifier, Long sessionId, SessionReqDto reqDto, MultipartFile multipartFile) { |
| 109 | + Session session = ifSessionExists(sessionId); |
| 110 | + Admin admin = findIfAdminExists(identifier); |
| 111 | + verifyAuthenticationRole(session, admin); |
| 112 | + |
| 113 | + if (multipartFile != null) { |
| 114 | + session.addImage(fileS3Util.uploadFile(multipartFile)); |
| 115 | + |
| 116 | + } |
| 117 | + session.updateSession(reqDto); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void deleteSession(String identifier, Long sessionId) { |
| 122 | + Session session = ifSessionExists(sessionId); |
| 123 | + Admin admin = findIfAdminExists(identifier); |
| 124 | + verifyAuthenticationRole(session, admin); |
| 125 | + |
| 126 | + session.removeAllAdmins(); |
| 127 | + |
| 128 | + sessionRepository.delete(session); |
| 129 | + } |
| 130 | + |
| 131 | + // --------------------------------- private method ---------------------------------------- |
| 132 | + |
| 133 | + private Session findByConferenceId(Long sessionId, Conference conference) { |
| 134 | + return sessionRepository.findByIdAndConference(sessionId, conference).orElseThrow(NotFoundSession::new); |
| 135 | + } |
| 136 | + |
| 137 | + private Admin findIfAdminExists(String identifier) { |
| 138 | + return adminRepository.findByAdminAuthCode(identifier).orElseThrow(NotFoundUserException::new); |
| 139 | + } |
| 140 | + |
| 141 | + private Attendee findIfAttendeeExists(String identifier) { |
| 142 | + return attendeeRepository.findByEmail(identifier).orElseThrow(NotFoundUserException::new); |
| 143 | + } |
| 144 | + |
| 145 | + private List<QuestionResDto> getQuestions(Conference conference, Long sessionId) { |
| 146 | + findByConferenceId(sessionId, conference); |
| 147 | + |
| 148 | + return sessionQuestionRepository.findBySessionIdJoinAttendeeSession(sessionId); |
| 149 | + } |
| 150 | + |
| 151 | + private AttendeeSession ifAttendeeSessionExists(Long sessionId, Long attendeeId) { |
| 152 | + return attendeeSessionRepository.findBySessionIdAndAttendeeId(sessionId, attendeeId) |
| 153 | + .orElseThrow(NotAttendedSession::new); |
| 154 | + } |
| 155 | + |
| 156 | + private Conference ifConferenceExists(Long conferenceId) { |
| 157 | + return conferenceRepository.findById(conferenceId).orElseThrow(NotFoundConference::new); |
| 158 | + } |
| 159 | + |
| 160 | + private Session ifSessionExists(Long sessionId) { |
| 161 | + return sessionRepository.findById(sessionId).orElseThrow(NotFoundSession::new); |
| 162 | + } |
| 163 | + |
| 164 | + private void verifyAuthenticationRole(Session session, Admin admin) { |
| 165 | + if (!sessionRepository.existsByIdAndAdmins_Id(session.getId(), admin.getId())) { |
| 166 | + throw new AuthorizedException(); |
| 167 | + } |
| 168 | + } |
167 | 169 |
|
168 | 170 | } |
0 commit comments