99import io .wisoft .prepair .prepair_api .interview .answer .repository .FeedbackRepository ;
1010import io .wisoft .prepair .prepair_api .interview .question .entity .InterviewQuestion ;
1111import io .wisoft .prepair .prepair_api .interview .question .repository .QuestionRepository ;
12+ import io .wisoft .prepair .prepair_api .interview .session .dto .response .CreateSessionResponse ;
1213import io .wisoft .prepair .prepair_api .interview .session .dto .response .SessionDetailResponse ;
1314import io .wisoft .prepair .prepair_api .interview .session .dto .response .SessionResponse ;
1415import io .wisoft .prepair .prepair_api .interview .session .entity .InterviewSession ;
@@ -60,7 +61,7 @@ public SessionDetailResponse getSessionDetail(UUID sessionId, UUID memberId) {
6061 }
6162
6263 public List <SessionDetailResponse .QuestionFeedback > buildQuestionFeedbacks (UUID sessionId ) {
63- List <InterviewQuestion > questions = questionRepository .findByInterviewSessionId (sessionId );
64+ List <InterviewQuestion > questions = questionRepository .findByInterviewSessionIdOrderByCreatedAtAsc (sessionId );
6465 Map <UUID , InterviewAnswer > answerMap = getAnswerMap (sessionId );
6566 Map <UUID , List <InterviewFeedback >> feedbackMap = getFeedbackMap (sessionId );
6667
@@ -76,7 +77,7 @@ public List<SessionDetailResponse.QuestionFeedback> buildQuestionFeedbacks(UUID
7677 }
7778
7879 List <InterviewFeedback > answerFeedbacks = feedbackMap .getOrDefault (answer .getId (), List .of ());
79- InterviewFeedback combined = findOptionalFeedback (answerFeedbacks , FeedbackType .COMBINED );
80+ InterviewFeedback combined = findFeedback (answerFeedbacks , FeedbackType .COMBINED );
8081
8182 result .add (new SessionDetailResponse .QuestionFeedback (
8283 question .getId (),
@@ -91,10 +92,68 @@ public List<SessionDetailResponse.QuestionFeedback> buildQuestionFeedbacks(UUID
9192 return result ;
9293 }
9394
95+ @ Transactional
96+ public CreateSessionResponse createSession (UUID oldSessionId , UUID memberId ) {
97+ InterviewSession oldSession = sessionRepository .findById (oldSessionId )
98+ .orElseThrow (() -> new BusinessException (ErrorCode .SESSION_NOT_FOUND ));
99+
100+ if (!oldSession .getMemberId ().equals (memberId )) {
101+ throw new BusinessException (ErrorCode .FORBIDDEN );
102+ }
103+ if (oldSession .getStatus () == SessionStatus .IN_PROGRESS ) {
104+ throw new BusinessException (ErrorCode .SESSION_NOT_FINISHED );
105+ }
106+
107+ List <InterviewQuestion > oldQuestions = questionRepository .findByInterviewSessionIdOrderByCreatedAtAsc (oldSessionId );
108+ InterviewSession newSession = sessionRepository .save (new InterviewSession (memberId , oldQuestions .size ()));
109+
110+ List <UUID > newQuestionIds = oldQuestions .stream ()
111+ .map (q -> cloneQuestion (q , newSession ).getId ())
112+ .toList ();
113+
114+ return new CreateSessionResponse (newSession .getId (), newQuestionIds );
115+ }
116+
117+ @ Transactional
118+ public CreateSessionResponse createQuestion (UUID oldSessionId , UUID oldQuestionId , UUID memberId ) {
119+ InterviewSession oldSession = sessionRepository .findById (oldSessionId )
120+ .orElseThrow (() -> new BusinessException (ErrorCode .SESSION_NOT_FOUND ));
121+
122+ if (!oldSession .getMemberId ().equals (memberId )) {
123+ throw new BusinessException (ErrorCode .FORBIDDEN );
124+ }
125+ if (oldSession .getStatus () == SessionStatus .IN_PROGRESS ) {
126+ throw new BusinessException (ErrorCode .SESSION_NOT_FINISHED );
127+ }
128+
129+ InterviewQuestion oldQuestion = questionRepository .findByIdAndMemberIdAndInterviewSessionId (
130+ oldQuestionId ,
131+ memberId ,
132+ oldSessionId
133+ )
134+ .orElseThrow (() -> new BusinessException (ErrorCode .QUESTION_NOT_FOUND ));
135+
136+ InterviewSession newSession = sessionRepository .save (new InterviewSession (memberId , 1 ));
137+ InterviewQuestion newQuestion = cloneQuestion (oldQuestion , newSession );
138+
139+ return new CreateSessionResponse (newSession .getId (), List .of (newQuestion .getId ()));
140+ }
141+
142+ private InterviewQuestion cloneQuestion (InterviewQuestion source , InterviewSession newSession ) {
143+ return questionRepository .save (new InterviewQuestion (
144+ source .getMemberId (),
145+ source .getQuestion (),
146+ source .getQuestionType (),
147+ source .getQuestionTag (),
148+ source .getJobPosting (),
149+ newSession
150+ ));
151+ }
152+
94153 @ Transactional
95154 public void saveCompletedSession (UUID sessionId , int finalScore , String finalFeedback ) {
96155 InterviewSession session = sessionRepository .findById (sessionId )
97- .orElseThrow (() -> new IllegalArgumentException ( "존재하지 않는 세션입니다." ));
156+ .orElseThrow (() -> new BusinessException ( ErrorCode . SESSION_NOT_FOUND ));
98157
99158 if (session .getStatus () == SessionStatus .COMPLETED ) {
100159 return ;
@@ -106,7 +165,7 @@ public void saveCompletedSession(UUID sessionId, int finalScore, String finalFee
106165 @ Transactional
107166 public void saveFailedSession (UUID sessionId ) {
108167 InterviewSession session = sessionRepository .findById (sessionId )
109- .orElseThrow (() -> new IllegalArgumentException ( "존재하지 않는 세션입니다." ));
168+ .orElseThrow (() -> new BusinessException ( ErrorCode . SESSION_NOT_FOUND ));
110169
111170 if (session .getStatus () != SessionStatus .IN_PROGRESS ) {
112171 return ;
@@ -130,15 +189,15 @@ private Map<UUID, List<InterviewFeedback>> getFeedbackMap(UUID sessionId) {
130189 .collect (Collectors .groupingBy (f -> f .getInterviewAnswer ().getId ()));
131190 }
132191
133- private InterviewFeedback findOptionalFeedback (List <InterviewFeedback > feedbacks , FeedbackType type ) {
192+ private InterviewFeedback findFeedback (List <InterviewFeedback > feedbacks , FeedbackType type ) {
134193 return feedbacks .stream ()
135194 .filter (f -> f .getFeedbackType () == type )
136195 .findFirst ()
137196 .orElse (null );
138197 }
139198
140199 private String getFeedbackText (List <InterviewFeedback > feedbacks , FeedbackType type ) {
141- InterviewFeedback feedback = findOptionalFeedback (feedbacks , type );
200+ InterviewFeedback feedback = findFeedback (feedbacks , type );
142201 return feedback == null ? null : feedback .getFeedback ();
143202 }
144203}
0 commit comments