Skip to content

Commit dc76e5b

Browse files
authored
[T3-152] 온보딩 등록 V2 API 및 유저 Role 추가 (#57)
* feat: ONBOARDING User Role 추가 * feat: 온보딩 등록 V2 API * refactor: 스트림 변수명 수정
1 parent 8f7f521 commit dc76e5b

10 files changed

Lines changed: 171 additions & 8 deletions

File tree

src/main/java/bitnagil/bitnagil_backend/enums/Role.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public enum Role implements EnumType {
77

88
GUEST("ROLE_GUEST"),
99
USER("ROLE_USER"),
10-
WITHDRAWN("ROLE_WITHDRAWN");
10+
WITHDRAWN("ROLE_WITHDRAWN"),
11+
ONBOARDING("ROLE_ONBOARDING"),;
1112

1213
private final String description;
1314

src/main/java/bitnagil/bitnagil_backend/global/config/SecurityConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6262
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
6363
// GUEST 권한으로만 접근 가능한 경로
6464
.requestMatchers("/api/v1/auth/agreements").hasRole("GUEST")
65+
// ONBAORDING 권한으로만 접근 가능한 경로
66+
.requestMatchers("/api/v1/onboardings").hasRole("ONBOARDING")
6567
// USER 권한으로만 접근 가능한 경로(전체)
6668
.requestMatchers("/**").hasRole("USER")
6769
.anyRequest().authenticated()

src/main/java/bitnagil/bitnagil_backend/onboarding/controller/OnboardingController.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,28 @@
1616

1717
@RestController
1818
@RequiredArgsConstructor
19-
@RequestMapping(value = "/api/v1/onboardings")
19+
@RequestMapping(value = "/api")
2020
public class OnboardingController implements OnboardingSpec {
2121

2222
private final OnboardingService onboardingService;
2323

24-
@PostMapping()
24+
@PostMapping("/v1/onboardings")
2525
public CustomResponseDto<OnboardingResponse> startOnboarding(@RequestBody OnboardingRequest onboardingRequest,
2626
@CurrentUser User user) {
2727
return onboardingService.startOnboarding(onboardingRequest, user);
2828
}
2929

30-
@PostMapping("/routines")
30+
// 온보딩 루틴 등록 API (V2)
31+
@PostMapping("/v2/onboardings/routines")
32+
public CustomResponseDto<Object> registrationRoutinesV2(@RequestBody RegistrationRoutinesRequest registrationRoutinesRequest,
33+
@CurrentUser User user) {
34+
onboardingService.registrationRoutinesV2(registrationRoutinesRequest, user);
35+
return CustomResponseDto.from(null);
36+
}
37+
38+
// TODO: v2로 전환 시 deprecated 처리
39+
@Deprecated()
40+
@PostMapping("/v1/onboardings/routines")
3141
public CustomResponseDto<Object> registrationRoutines(@RequestBody RegistrationRoutinesRequest registrationRoutinesRequest,
3242
@CurrentUser User user) {
3343
onboardingService.registrationRoutines(registrationRoutinesRequest, user);

src/main/java/bitnagil/bitnagil_backend/onboarding/controller/spec/OnboardingSpec.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public interface OnboardingSpec {
2121
})
2222
public CustomResponseDto<OnboardingResponse> startOnboarding(OnboardingRequest onboardingRequest, User user);
2323

24+
@Operation(summary = "(V2) 온보딩 시 추천 루틴을 등록합니다.(복수 선택이 가능합니다.)")
25+
@ApiErrorCodeExamples({
26+
ErrorCode.NOT_FOUND_USER, ErrorCode.NOT_FOUND_RECOMMENDED_ROUTINE
27+
})
28+
public CustomResponseDto<Object> registrationRoutinesV2(RegistrationRoutinesRequest registrationRoutinesRequest,
29+
User user);
30+
2431
@Operation(summary = "온보딩 시 추천 루틴을 등록합니다.(복수 선택이 가능합니다.)")
2532
@ApiErrorCodeExamples({
2633
ErrorCode.NOT_FOUND_USER, ErrorCode.NOT_FOUND_RECOMMENDED_ROUTINE

src/main/java/bitnagil/bitnagil_backend/onboarding/service/OnboardingService.java

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
import bitnagil.bitnagil_backend.recommendedRoutine.repository.RecommendedRoutineRepository;
2020
import bitnagil.bitnagil_backend.recommendedRoutine.repository.RecommendedSubRoutineRepository;
2121
import bitnagil.bitnagil_backend.recommendedRoutine.service.RecommendedRoutineManager;
22+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
23+
import bitnagil.bitnagil_backend.routineInfoV2.repository.RoutineInfoV2Repository;
24+
import bitnagil.bitnagil_backend.routineInfoV2.service.RoutineInfoFactoryV2;
25+
import bitnagil.bitnagil_backend.routineV2.domain.RoutineV2;
26+
import bitnagil.bitnagil_backend.routineV2.repository.RoutineV2Repository;
27+
import bitnagil.bitnagil_backend.routineV2.service.RoutineFactoryV2;
2228
import bitnagil.bitnagil_backend.user.domain.User;
23-
import bitnagil.bitnagil_backend.user.repository.UserRepository;
2429
import bitnagil.bitnagil_backend.user.service.UserManager;
2530
import lombok.RequiredArgsConstructor;
2631

@@ -38,17 +43,24 @@
3843
public class OnboardingService {
3944

4045
private final OnboardingRepository onboardingRepository;
41-
private final UserRepository userRepository;
4246
private final RecommendedRoutineRepository recommendRoutineRepository;
4347
private final RecommendedSubRoutineRepository recommendedSubRoutineRepository;
4448
private final ChangedRoutineRepository changedRoutineRepository;
4549
private final ChangedSubRoutineRepository changedSubRoutineRepository;
4650

47-
4851
private final RecommendedRoutineManager recommendedRoutineManager;
4952
private final ChangedRoutineFactory changedRoutineFactory;
5053
private final UserManager userManager;
5154

55+
// V2 관련 리포지토리
56+
// TODO: v2로 전환 시 Rename
57+
private final RoutineInfoV2Repository routineInfoV2Repository;
58+
private final RoutineV2Repository routineV2Repository;
59+
60+
private final RoutineFactoryV2 routineFactoryV2;
61+
private final RoutineInfoFactoryV2 routineInfoFactory;
62+
63+
5264
/**
5365
* 유저와 매칭되는 온보딩 결과를 설정하고, 리턴하는 메서드
5466
*/
@@ -86,6 +98,58 @@ public CustomResponseDto<OnboardingResponse> startOnboarding(OnboardingRequest r
8698
* 온보딩 시 추천 루틴을 저장하는 메서드
8799
*/
88100
@Transactional
101+
public void registrationRoutinesV2(RegistrationRoutinesRequest request, User user) {
102+
103+
LocalDate today = LocalDate.now();
104+
105+
for (Long recommendedRoutineId : request.getRecommendedRoutineIds()) {
106+
// 추천 루틴을 조회한다
107+
RecommendedRoutine recommendedRoutine = recommendRoutineRepository.findById(recommendedRoutineId)
108+
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_RECOMMENDED_ROUTINE));
109+
110+
// 온보딩으로 등록한 루틴은 루틴 시작, 종료일자가 당일로 설정된다.
111+
RoutineInfoV2 routineInfo = routineInfoFactory.createNewRoutineInfo(
112+
recommendedRoutine.getRecommendedRoutineName(),
113+
List.of(), // 온보딩은 반복일자를 설정하지 않는다.
114+
recommendedRoutine.getExecutionTime(),
115+
today,
116+
today,
117+
user
118+
);
119+
120+
routineInfoV2Repository.save(routineInfo);
121+
122+
// 추천 서브 루틴을 조회한다.
123+
List<RecommendedSubRoutine> recommendedSubRoutines =
124+
recommendedSubRoutineRepository.findByRecommendedRoutine(recommendedRoutine);
125+
126+
// 서브 루틴 이름 리스트 생성
127+
List<String> subRoutineNames = recommendedSubRoutines.stream()
128+
.map(RecommendedSubRoutine::getSubRoutineName)
129+
.toList();
130+
131+
// 서브 루틴 완료 여부 리스트 생성
132+
List<Boolean> subRoutineCompleteYn = recommendedSubRoutines.stream()
133+
.map(completeYn -> false)
134+
.toList();
135+
136+
// 루틴 정보에 해당하는 루틴을 생성한다.
137+
RoutineV2 routine = routineFactoryV2.createNewRoutine(
138+
today,
139+
false,
140+
subRoutineNames,
141+
subRoutineCompleteYn,
142+
routineInfo);
143+
routineV2Repository.save(routine);
144+
}
145+
}
146+
147+
/**
148+
* 온보딩 시 추천 루틴을 저장하는 메서드
149+
*/
150+
// TODO: v2로 전환 시 deprecated 처리
151+
@Deprecated
152+
@Transactional
89153
public void registrationRoutines(RegistrationRoutinesRequest request, User user) {
90154

91155
LocalDate today = LocalDate.now();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitnagil.bitnagil_backend.routineInfoV2.repository;
2+
3+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface RoutineInfoV2Repository extends JpaRepository<RoutineInfoV2, Long> {
9+
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bitnagil.bitnagil_backend.routineInfoV2.service;
2+
3+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
4+
import bitnagil.bitnagil_backend.user.domain.User;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.time.DayOfWeek;
8+
import java.time.LocalDate;
9+
import java.time.LocalTime;
10+
import java.util.List;
11+
12+
/**
13+
* 루틴 관련 엔티티 생성, 초기화 책임을 담당하는 클래스입니다.
14+
*/
15+
@Component
16+
public class RoutineInfoFactoryV2 {
17+
18+
// 신규 RoutineInfo 엔티티 생성 및 초기화
19+
public RoutineInfoV2 createNewRoutineInfo(String routineName, List<DayOfWeek> routineRepeatDay,
20+
LocalTime routineExecutionTime, LocalDate routineStartDate,
21+
LocalDate routineEndDate, User user) {
22+
return RoutineInfoV2.builder()
23+
.routineName(routineName)
24+
.routineRepeatDay(routineRepeatDay) // 온보딩은 반복일자를 설정하지 않는다.
25+
.routineExecutionTime(routineExecutionTime)
26+
.routineStartDate(routineStartDate)
27+
.routineEndDate(routineEndDate)
28+
.user(user)
29+
.build();
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitnagil.bitnagil_backend.routineV2.repository;
2+
3+
import bitnagil.bitnagil_backend.routineV2.domain.RoutineV2;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface RoutineV2Repository extends JpaRepository<RoutineV2, Long> {
9+
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package bitnagil.bitnagil_backend.routineV2.service;
2+
3+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
4+
import bitnagil.bitnagil_backend.routineV2.domain.RoutineV2;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.time.LocalDate;
8+
import java.util.List;
9+
10+
/**
11+
* 루틴 관련 엔티티 생성, 초기화 책임을 담당하는 클래스입니다.
12+
*/
13+
@Component
14+
public class RoutineFactoryV2 {
15+
16+
// 신규 Routine 엔티티 생성 및 초기화
17+
public RoutineV2 createNewRoutine(LocalDate routineDate, Boolean routineCompleteYn, List<String> subRoutineNames,
18+
List<Boolean> subRoutineCompleteYn, RoutineInfoV2 routineInfo) {
19+
return RoutineV2.builder()
20+
.routineDate(routineDate)
21+
.routineCompleteYn(routineCompleteYn)
22+
.subRoutineNames(subRoutineNames)
23+
.subRoutineCompleteYn(subRoutineCompleteYn)
24+
.routineInfo(routineInfo)
25+
.build();
26+
}
27+
}

src/main/java/bitnagil/bitnagil_backend/user/domain/User.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public void updateAgreements(Boolean agreedToTermsOfService, Boolean agreedToPri
6868
this.agreedToTermsOfService = agreedToTermsOfService;
6969
this.agreedToPrivacyPolicy = agreedToPrivacyPolicy;
7070
this.isOverFourteen = isOverFourteen;
71-
this.role = Role.USER; // 약관 동의 후 권한을 USER로 변경
71+
this.role = Role.ONBOARDING; // 약관 동의 후 권한을 임시 USER인 ONBOARDING으로 변경
7272
}
7373

7474
public void updateOnboarding(Onboarding onboarding) {
7575
this.onboarding = onboarding;
76+
this.role = Role.USER; // 온보딩 완료 후 권한을 USER로 변경
7677
}
7778

7879
// todo: 운영 반영 후 이슈가 없으면 제거

0 commit comments

Comments
 (0)