File tree Expand file tree Collapse file tree 3 files changed +26
-23
lines changed
src/main/java/com/climbup/climbup Expand file tree Collapse file tree 3 files changed +26
-23
lines changed Original file line number Diff line number Diff line change 11package com .climbup .climbup .session .repository ;
22
33import com .climbup .climbup .session .entity .UserSession ;
4- import com .climbup .climbup .user .entity .User ;
54import org .springframework .data .jpa .repository .JpaRepository ;
5+ import org .springframework .data .jpa .repository .Query ;
6+ import org .springframework .data .repository .query .Param ;
67
78import java .util .Optional ;
89
910public interface UserSessionRepository extends JpaRepository <UserSession , Long > {
1011 Optional <UserSession > findByUserIdAndEndedAtIsNull (Long userId );
12+
13+ @ Query ("SELECT us FROM UserSession us WHERE us.user.id = :userId AND us.endedAt IS NULL ORDER BY us.createdAt DESC LIMIT 1" )
14+ Optional <UserSession > findLatestActiveSessionByUserId (@ Param ("userId" ) Long userId );
1115}
Original file line number Diff line number Diff line change 1212import lombok .RequiredArgsConstructor ;
1313import org .springframework .transaction .annotation .Transactional ;
1414import org .springframework .security .access .AccessDeniedException ;
15+ import org .springframework .dao .DataIntegrityViolationException ;
1516import org .springframework .stereotype .Service ;
1617
1718import java .time .LocalDate ;
@@ -31,29 +32,28 @@ public UserSession startSession(Long userId) {
3132 User user = userRepository .findById (userId )
3233 .orElseThrow (() -> new UserNotFoundException (userId ));
3334
34- var activeSession = userSessionRepository .findByUserIdAndEndedAtIsNull (userId ).orElse (null );
35-
36- if (activeSession != null ) {
35+ try {
36+ LocalDateTime now = LocalDateTime .now ();
37+ LocalDate today = now .toLocalDate ();
38+
39+ UserSession session = UserSession .builder ()
40+ .user (user )
41+ .gym (user .getGym ())
42+ .sessionDate (today )
43+ .startedAt (now )
44+ .srGained (0 )
45+ .completedCount (0 )
46+ .attemptedCount (0 )
47+ .build ();
48+
49+ session = userSessionRepository .save (session );
50+ recommendationService .generateRecommendationsForSession (session );
51+
52+ return session ;
53+
54+ } catch (DataIntegrityViolationException e ) {
3755 throw new UserSessionNotYetFinishedException ();
3856 }
39-
40- LocalDateTime now = LocalDateTime .now ();
41- LocalDate today = now .toLocalDate ();
42-
43- UserSession session = UserSession .builder ()
44- .user (user )
45- .gym (user .getGym ())
46- .sessionDate (today )
47- .startedAt (now )
48- .srGained (0 )
49- .completedCount (0 )
50- .attemptedCount (0 )
51- .build ();
52-
53- session = userSessionRepository .save (session );
54- recommendationService .generateRecommendationsForSession (session );
55-
56- return session ;
5757 }
5858
5959 @ Override
Original file line number Diff line number Diff line change 88import com .climbup .climbup .gym .exception .GymNotFoundException ;
99import com .climbup .climbup .session .entity .UserSession ;
1010import com .climbup .climbup .sr .entity .SRHistory ;
11- import com .climbup .climbup .level .entity .Level ;
1211import jakarta .persistence .*;
1312import lombok .AccessLevel ;
1413import lombok .Getter ;
You can’t perform that action at this time.
0 commit comments