Skip to content

Commit

Permalink
Merge pull request #30 from Team-Motivoo/feat/#24-onboarding_create
Browse files Browse the repository at this point in the history
[FEAT] 운동 정보 입력 API
  • Loading branch information
hyeyeonnnnn authored Jan 10, 2024
2 parents 33c1c9a + 531e867 commit 25e1f8d
Show file tree
Hide file tree
Showing 25 changed files with 431 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;


@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class})
public class MotivooServerApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public String getPayload(String token){

public void validateToken(String token) {
try {
log.info("토큰 값="+token);
token = token.replaceAll("\\s+", "");
token = token.replace(BEARER_TYPE, "");
Jwts.parserBuilder()
Expand Down Expand Up @@ -122,4 +123,6 @@ public static Long getUserFromPrincipal(Principal principal) {
}
return Long.valueOf(principal.getName());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -121,7 +120,6 @@ private Map<String, Object> getUserAttributes(ClientRegistration provider, Oauth
.block();
}


@Transactional
public void logout(String accessToken) {
String refreshToken = userRepository.findRefreshTokenById(getAuthenticatedUser());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sopt.org.motivooServer.domain.health.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import sopt.org.motivooServer.domain.health.entity.ExerciseFrequency;
import sopt.org.motivooServer.domain.health.entity.ExerciseTime;
import sopt.org.motivooServer.domain.health.entity.ExerciseType;
import sopt.org.motivooServer.domain.health.entity.HealthNote;
import sopt.org.motivooServer.domain.user.entity.UserType;

import java.util.List;

public record OnboardingRequest (
String type,
int age,
@JsonProperty("is_exercise")
boolean isExercise,
@JsonProperty("exercise_type")
String exerciseType,
@JsonProperty("exercise_count")
String exerciseCount,
@JsonProperty("exercise_time")
String exerciseTime,
@JsonProperty("exercise_note")
List<String> exerciseNote
){

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sopt.org.motivooServer.domain.health.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import sopt.org.motivooServer.domain.health.entity.ExerciseLevel;

public record OnboardingResponse (
@JsonProperty("user_id")
Long userId,
@JsonProperty("invite_code")
String inviteCode,
@JsonProperty("exercise_level")
String exerciseLevel
){

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
package sopt.org.motivooServer.domain.health.entity;

import jakarta.persistence.EntityNotFoundException;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExerciseFrequency {

// 1회 미만 | 1~2회 | 3~4회 | 5회 이상
LESS_THAN_ONCE("1회 미만"),
ONCE_OR_TWICE("1~2회"),
THREE_OR_FOUR_TIMES("3~4회"),
FIVE_OR_MORE_TIMES("5회 이상");

private final String value;

public static ExerciseFrequency of(String value) {
return Arrays.stream(ExerciseFrequency.values())
.filter(exerciseFrequency -> value.equals(exerciseFrequency.value))
.findFirst()
.orElseThrow(() -> new EntityNotFoundException());//TO-DO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sopt.org.motivooServer.domain.health.entity;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import sopt.org.motivooServer.domain.health.exception.HealthException;

import java.util.Arrays;

import static sopt.org.motivooServer.domain.health.exception.HealthExceptionType.INVALID_EXERCISE_LEVEL;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExerciseLevel {
ADVANCED("고수"),
INTERMEDIATE("중수"),
BEGINNER("초보");
private final String value;


public static ExerciseLevel of(String value) {
return Arrays.stream(ExerciseLevel.values())
.filter(exerciseLevel -> value.equals(exerciseLevel.value))
.findFirst()
.orElseThrow(() -> new HealthException(INVALID_EXERCISE_LEVEL));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
package sopt.org.motivooServer.domain.health.entity;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import sopt.org.motivooServer.domain.health.exception.HealthException;

import java.util.Arrays;

import static sopt.org.motivooServer.domain.health.exception.HealthExceptionType.INVALID_EXERCISE_TIME;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExerciseTime {

// 30분 미만 | 30분~1시간 | 1시간~2시간 | 2시간 이상
LESS_THAN_HALFHOUR("30분 미만"),
HALFHOUR_TO_ONEHOUR("30분~1시간"),
ONEHOUR_TO_TWOHOURS("1시간~2시간"),
TWOHOURS_OR_MORE("2시간 이상");

private final String value;

public static ExerciseTime of(String value) {
return Arrays.stream(ExerciseTime.values())
.filter(exerciseTime -> value.equals(exerciseTime.value))
.findFirst()
.orElseThrow(() -> new HealthException(INVALID_EXERCISE_TIME));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package sopt.org.motivooServer.domain.health.entity;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import sopt.org.motivooServer.domain.health.exception.HealthException;

import java.util.Arrays;

import static sopt.org.motivooServer.domain.health.exception.HealthExceptionType.*;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExerciseType {
HIGH_LEVEL("고강도"),
MEDIUM_LEVEL("중강도"),
LOW_LEVEL("저강도")
;
private final String value;

// 고강도 | 중강도 | 저강도
public static ExerciseType of(String value) {
return Arrays.stream(ExerciseType.values())
.filter(exerciseType -> value.equals(exerciseType.value))
.findFirst()
.orElseThrow(() -> new HealthException(INVALID_EXERCISE_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import sopt.org.motivooServer.domain.common.BaseTimeEntity;
import sopt.org.motivooServer.domain.user.entity.User;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class Health extends BaseTimeEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -55,4 +49,29 @@ public class Health extends BaseTimeEntity {
@Column(nullable = false)
@ElementCollection
private List<HealthNote> healthNotes = new ArrayList<>();

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private ExerciseLevel exerciseLevel;

@Builder
private Health(User user, boolean isExercise, ExerciseType exerciseType,
ExerciseFrequency exerciseFrequency, ExerciseTime exerciseTime, List<HealthNote> healthNotes, ExerciseLevel exerciseLevel){
this.user = user;
this.isExercise = isExercise;
this.exerciseType = exerciseType;
this.exerciseFrequency = exerciseFrequency;
this.exerciseTime = exerciseTime;
this.healthNotes = healthNotes;
this.exerciseLevel = exerciseLevel;
}

public void updateExerciseLevel(double score){
if(score>36 && score<=96)
this.exerciseLevel = ExerciseLevel.ADVANCED;
else if(score>=18 && score<=36)
this.exerciseLevel = ExerciseLevel.INTERMEDIATE;
else
this.exerciseLevel = ExerciseLevel.BEGINNER;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package sopt.org.motivooServer.domain.health.entity;

import static sopt.org.motivooServer.domain.health.exception.HealthExceptionType.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import sopt.org.motivooServer.domain.health.exception.HealthException;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum HealthNote {
Expand All @@ -20,10 +19,9 @@ public enum HealthNote {

private final String value;

public static HealthNote of(String value) {
public static List<HealthNote> of(List<String> values) {
return Arrays.stream(HealthNote.values())
.filter(healthNote -> value.equals(healthNote.value))
.findFirst()
.orElseThrow(() -> new HealthException(INVALID_HEALTH_NOTE));
.filter(healthNote -> values.contains(healthNote.value))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public enum HealthExceptionType implements BusinessExceptionType {
* 400 Bad Request
*/
INVALID_HEALTH_NOTE(HttpStatus.BAD_REQUEST, "유효하지 않는 건강 주의사항입니다."),
INVALID_EXERCISE_FREQUENCY(HttpStatus.BAD_REQUEST, "유효하지 않는 운동 횟수입니다."),
INVALID_EXERCISE_TIME(HttpStatus.BAD_REQUEST, "유효하지 않는 운동 시간입니다."),
INVALID_EXERCISE_TYPE(HttpStatus.BAD_REQUEST, "유효하지 않는 운동 종료입니다."),

INVALID_EXERCISE_LEVEL(HttpStatus.BAD_REQUEST, "유효하지 않는 유저 분류입니다."),

/**
* 404 Not Found
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package sopt.org.motivooServer.domain.health.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import sopt.org.motivooServer.domain.health.entity.Health;
import sopt.org.motivooServer.domain.user.entity.User;

public interface HealthRepository extends JpaRepository<Health, Long> {
import java.util.Optional;

public interface HealthRepository extends JpaRepository<Health, Long> {
Optional<Health> findByUser(User user);
Optional<Health> findByUserId(Long userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package sopt.org.motivooServer.domain.health.service;

import org.springframework.stereotype.Service;
import sopt.org.motivooServer.domain.health.entity.ExerciseFrequency;
import sopt.org.motivooServer.domain.health.entity.ExerciseTime;
import sopt.org.motivooServer.domain.health.entity.ExerciseType;

@Service
public class CalculateScore {
public double calculate(boolean isExercise, ExerciseType type,
ExerciseFrequency frequency, ExerciseTime time){
double exerciseScore = getExerciseScore(isExercise, type);
double frequencyScore = getFrequencyScore(frequency);
double timeScore = getTimeScore(time);

return exerciseScore+frequencyScore+timeScore;
}
private double getExerciseScore(boolean isExercise, ExerciseType type) {
if(!isExercise){
switch (type) {
case HIGH_LEVEL:
return 4.2;
case MEDIUM_LEVEL:
return 2.1;
default:
return 1.05;
}
}
switch (type) {
case HIGH_LEVEL:
return 6;
case MEDIUM_LEVEL:
return 3;
default:
return 1.5;
}
}
private double getFrequencyScore(ExerciseFrequency frequency) {
switch (frequency) {
case LESS_THAN_ONCE:
return 1.0;
case ONCE_OR_TWICE:
return 2.0;
case THREE_OR_FOUR_TIMES:
return 3.0;
default:
return 4.0;
}
}
private double getTimeScore(ExerciseTime time) {
switch (time) {
case LESS_THAN_HALFHOUR:
return 1.0;
case HALFHOUR_TO_ONEHOUR:
return 2.0;
case ONEHOUR_TO_TWOHOURS:
return 3.0;
default:
return 4.0;
}
}

}
Loading

0 comments on commit 25e1f8d

Please sign in to comment.