Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#16] 신체 정보를 받는다 #33

Merged
merged 17 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/docs/asciidoc/physical-profile.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
:toc: left
:source-highlighter: highlightjs
:sectlinks:
:toclevels: 2
:sectlinks:
:sectnums:

== PhysicalProfile

=== 신체 정보 프로필 등록 (POST /api/members/me/physical-profile)

==== 요청
include::{snippets}/physical-profile-controller-web-mvc-test/신체_정보_프로필_생성/http-request.adoc[]
include::{snippets}/physical-profile-controller-web-mvc-test/신체_정보_프로필_생성/request-headers.adoc[]
include::{snippets}/physical-profile-controller-web-mvc-test/신체_정보_프로필_생성/request-fields.adoc[]

==== 응답
include::{snippets}/physical-profile-controller-web-mvc-test/신체_정보_프로필_생성/http-response.adoc[]
include::{snippets}/physical-profile-controller-web-mvc-test/신체_정보_프로필_생성/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import static com.flab.eattofit.global.config.interceptor.support.HttpMethod.DELETE;
import static com.flab.eattofit.global.config.interceptor.support.HttpMethod.GET;
import static com.flab.eattofit.global.config.interceptor.support.HttpMethod.OPTIONS;
import static com.flab.eattofit.global.config.interceptor.support.HttpMethod.PATCH;
import static com.flab.eattofit.global.config.interceptor.support.HttpMethod.POST;

@RequiredArgsConstructor
Expand All @@ -39,7 +41,8 @@ private HandlerInterceptor loginValidCheckerInterceptor() {
return new PathMatcherInterceptor(memberLoginValidCheckerInterceptor)
.excludePathPatterns("/api/auth/login/**", POST)
.addPathPatterns("/api/storage/**", GET)
.addPathPatterns("/api/foods/**", GET);
.addPathPatterns("/api/foods/**", GET)
.addPathPatterns("/api/members/**", GET, POST, PATCH, DELETE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.flab.eattofit.profile.application.physicalprofile;

import com.flab.eattofit.profile.application.physicalprofile.dto.PhysicalProfileCreateRequest;
import com.flab.eattofit.profile.domain.physicalprofile.PhysicalProfile;
import com.flab.eattofit.profile.domain.physicalprofile.PhysicalProfileRepository;
import com.flab.eattofit.profile.domain.physicalprofile.YearManager;
import com.flab.eattofit.profile.domain.physicalprofile.vo.Gender;
import com.flab.eattofit.profile.domain.physicalprofile.vo.Height;
import com.flab.eattofit.profile.domain.physicalprofile.vo.Physical;
import com.flab.eattofit.profile.domain.physicalprofile.vo.Weight;
import com.flab.eattofit.profile.domain.physicalprofile.vo.Year;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional
@Service
public class PhysicalProfileService {

private final YearManager yearManager;
private final PhysicalProfileRepository physicalProfileRepository;

public PhysicalProfile createPhysicalProfile(final PhysicalProfileCreateRequest request, final Long memberId) {
Physical physical = Physical.builder()
.birthYear(Year.createWith(request.birthYear(), yearManager))
.gender(Gender.findByName(request.gender()))
.weight(Weight.createWith(request.weight()))
.height(Height.createWith(request.height()))
.build();
PhysicalProfile physicalProfile = PhysicalProfile.createWith(physical, memberId);

return physicalProfileRepository.save(physicalProfile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.flab.eattofit.profile.application.physicalprofile.dto;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.math.BigDecimal;

public record PhysicalProfileCreateRequest(
@NotNull(message = "출생 년도가 필요합니다.")
Integer birthYear,

@NotEmpty(message = "성별이 필요합니다.")
String gender,

@NotNull(message = "몸무게가 필요합니다.")
BigDecimal weight,

@NotNull(message = "신장이 필요합니다.")
BigDecimal height
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.flab.eattofit.profile.domain.physicalprofile;

import com.flab.eattofit.profile.domain.physicalprofile.vo.Physical;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Entity
public class PhysicalProfile {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
private Physical physical;

@Column(nullable = false)
private Long memberId;

private PhysicalProfile(
final Physical physical,
final Long memberId
) {
this.physical = physical;
this.memberId = memberId;
}

public static PhysicalProfile createWith(
final Physical physical,
final Long memberId
) {
return new PhysicalProfile(physical, memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.flab.eattofit.profile.domain.physicalprofile;

public interface PhysicalProfileRepository {

PhysicalProfile save(PhysicalProfile physicalProfile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.flab.eattofit.profile.domain.physicalprofile;

public interface YearManager {

int getCurrentYear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.flab.eattofit.profile.domain.physicalprofile.vo;

import com.flab.eattofit.profile.exception.exceptions.physicalprofile.GenderNotFoundException;
import lombok.Getter;

import java.util.Arrays;

@Getter
public enum Gender {

MALE("남성"),
FEMALE("여성");

private final String name;

Gender(final String name) {
this.name = name;
}

public static Gender findByName(final String name) {
return Arrays.stream(values())
.filter(gender -> gender.isSame(name))
.findAny()
.orElseThrow(GenderNotFoundException::new);
}

private boolean isSame(final String name) {
return name.equals(this.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.flab.eattofit.profile.domain.physicalprofile.vo;

import com.flab.eattofit.profile.exception.exceptions.physicalprofile.BadMemberHeightException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class Height {

private static final BigDecimal MIN_HEIGHT = BigDecimal.valueOf(100);
private static final BigDecimal MAX_HEIGHT = BigDecimal.valueOf(250);

@Column(nullable = false, name = "height")
private BigDecimal value;

private Height(final BigDecimal value) {
this.value = value;
}

public static Height createWith(final BigDecimal height) {
validateHeight(height);
return new Height(height);
}

private static void validateHeight(final BigDecimal height) {
if (height.compareTo(MIN_HEIGHT) < 0 || height.compareTo(MAX_HEIGHT) > 0) {
throw new BadMemberHeightException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.flab.eattofit.profile.domain.physicalprofile.vo;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Embeddable
public class Physical {

@Embedded
private Year birthYear;

@Enumerated(value = EnumType.STRING)
@Column(nullable = false)
private Gender gender;

@Embedded
private Weight weight;

@Embedded
private Height height;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.flab.eattofit.profile.domain.physicalprofile.vo;

import com.flab.eattofit.profile.exception.exceptions.physicalprofile.BadMemberWeightException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class Weight {

private static final BigDecimal MIN_WEIGHT = BigDecimal.valueOf(15);
private static final BigDecimal MAX_WEIGHT = BigDecimal.valueOf(200);

@Column(nullable = false, name = "weight")
private BigDecimal value;

private Weight(final BigDecimal value) {
this.value = value;
}

public static Weight createWith(final BigDecimal weight) {
validateWeight(weight);
return new Weight(weight);
}

private static void validateWeight(final BigDecimal weight) {
if (weight.compareTo(MIN_WEIGHT) < 0 || weight.compareTo(MAX_WEIGHT) > 0) {
throw new BadMemberWeightException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.flab.eattofit.profile.domain.physicalprofile.vo;

import com.flab.eattofit.profile.domain.physicalprofile.YearManager;
import com.flab.eattofit.profile.exception.exceptions.physicalprofile.BadMemberAgeException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class Year {

private static final int MIN_AGE = 8;
private static final int MAX_AGE = 100;

@Column(nullable = false, name = "birth_year")
private Integer value;

private Year(final Integer value) {
this.value = value;
}

public static Year createWith(final Integer birthYear, final YearManager yearManager) {
validateAge(birthYear, yearManager);
return new Year(birthYear);
}

private static void validateAge(final Integer birthYear, final YearManager yearManager) {
int currentYear = yearManager.getCurrentYear();
int memberAge = currentYear - birthYear;

if (memberAge < MIN_AGE || memberAge > MAX_AGE) {
throw new BadMemberAgeException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flab.eattofit.profile.exception.exceptions.physicalprofile;

import com.flab.eattofit.global.exception.GlobalException;
import org.springframework.http.HttpStatus;

public class BadMemberAgeException extends GlobalException {

public BadMemberAgeException() {
super(HttpStatus.BAD_REQUEST, "BAD_MEMBER_AGE", "가능한 회원 나이를 벗어났습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flab.eattofit.profile.exception.exceptions.physicalprofile;

import com.flab.eattofit.global.exception.GlobalException;
import org.springframework.http.HttpStatus;

public class BadMemberHeightException extends GlobalException {

public BadMemberHeightException() {
super(HttpStatus.BAD_REQUEST, "BAD_MEMBER_HEIGHT", "가능한 회원 신장을 벗어났습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flab.eattofit.profile.exception.exceptions.physicalprofile;

import com.flab.eattofit.global.exception.GlobalException;
import org.springframework.http.HttpStatus;

public class BadMemberWeightException extends GlobalException {

public BadMemberWeightException() {
super(HttpStatus.BAD_REQUEST, "BAD_MEMBER_WEIGHT", "가능한 회원 몸무게를 벗어났습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flab.eattofit.profile.exception.exceptions.physicalprofile;

import com.flab.eattofit.global.exception.GlobalException;
import org.springframework.http.HttpStatus;

public class GenderNotFoundException extends GlobalException {

public GenderNotFoundException() {
super(HttpStatus.NOT_FOUND, "GENDER_NOT_FOUND", "이용 가능한 성별이 아닙니다.");
}
}
Loading
Loading