Skip to content

Commit

Permalink
Merge pull request #5 from kim3ho1/feature/#4
Browse files Browse the repository at this point in the history
Feature/#4
  • Loading branch information
seheonnn authored Jan 11, 2024
2 parents 7741a7c + 80909ff commit 235dac2
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -34,6 +37,12 @@ public ResponseEntity<String> noteProtein(@RequestBody double protein) {
return ResponseEntity.ok("ok");
}

@DeleteMapping("/note/{noteId}")
public ResponseEntity<HttpStatus> deleteNote(@PathVariable("noteId") Long noteId) {
// TODO Delete 테스트
return ResponseEntity.ok(foodService.deleteNote(noteId));
}

// 메인 화면 정보 - 오늘 섭취량/목표 섭취량
@GetMapping("")
public ResponseEntity<NoteResponseDto.NoteStatisticsResponseDto> getNoteInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@ public ResponseEntity<?> modifyUserDetails(@RequestBody UserRegisterDto.Register
return ResponseEntity.ok(null);
}

@PutMapping("/update") // TODO 마이페이지 업데이트 Test
public ResponseEntity<UserRegisterDto.UserResponseDto> updateUserDetails(@RequestBody UserRegisterDto.UpdateUserRequestDto updateUserRequestDto) {
return ResponseEntity.ok(userService.updateUserDetails(updateUserRequestDto));
}
@GetMapping("") // TODO 마이페이지 조회 테스트
public ResponseEntity<UserRegisterDto.UserResponseDto> getUserDetails() {
return ResponseEntity.ok(userService.getUserDetails());
}

@PutMapping("/reset") // TODO 목표 단백질량 초기화
public ResponseEntity<Double> resetGoal() {
return ResponseEntity.ok(userService.resetGoal());
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/kim3ho1/yourprotein/domain/PurposeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum PurposeType {
TRAINING(2,1.2, "TRAINING"), // 운동 하는 사람
BULK(3,2.0, "BULK"); // 벌크업을 원하는 사람
private final int index;
private final Double value;
private final double value;
private final String role;


Expand All @@ -19,6 +19,6 @@ public int getIndex() {
return index;
}

public Double getActivity() { return value; }
public double getPurpose() { return value; }

}
4 changes: 4 additions & 0 deletions src/main/java/com/kim3ho1/yourprotein/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public void setGender(Gender gender) {
public void setPurpose(PurposeType purpose) {
this.purpose = purpose;
}

public void setGoalProtein(double goalProtein) {
this.goalProtein = goalProtein;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/kim3ho1/yourprotein/dto/UserRegisterDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import com.kim3ho1.yourprotein.domain.Gender;
import com.kim3ho1.yourprotein.domain.PurposeType;

import jakarta.persistence.Column;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -46,4 +53,31 @@ public static class RegisterRequestDto {

}

@Data
@AllArgsConstructor
@Builder
public static class UpdateUserRequestDto {
private double height;
private double weight;

public PurposeType purpose;
public double goalProtein;

}
@Data
@AllArgsConstructor
@Builder
public static class UserResponseDto {

private Long id;
private String name;
private String email;
private Integer age;
private double height;
private double weight;
private Gender gender;
private PurposeType purpose;
private double goalProtein;

}
}
21 changes: 17 additions & 4 deletions src/main/java/com/kim3ho1/yourprotein/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.transaction.annotation.Transactional;

import com.kim3ho1.yourprotein.domain.Food;
import com.kim3ho1.yourprotein.domain.Note;
Expand Down Expand Up @@ -55,8 +56,7 @@ public NoteResponseDto.NoteStatisticsResponseDto getNoteInfo() {
List<Note> notesByUserAndToday = noteRepository.calculateToday(user.getId(), LocalDate.now().toString());
notesByUserAndToday.stream().map(data->{current+=data.getProtein(); return null; }).collect(Collectors.toList());

// TODO goal 추가
return new NoteResponseDto.NoteStatisticsResponseDto(1000.0, current);
return new NoteResponseDto.NoteStatisticsResponseDto(user.getGoalProtein(), current);
}

// 메인 화면 정보 - 일주일간 섭취량
Expand All @@ -73,4 +73,17 @@ public List<NoteResponseDto.WeeklyNoteStatisticsResponseDto> getWeekly() {
.build();
}).collect(Collectors.toList());
}

@Transactional
public HttpStatus deleteNote(Long noteId) {
CustomUserDetails principal = (CustomUserDetails)SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
User user = principal.getUser();
Note note = noteRepository.findById(noteId).orElseThrow(() -> new RuntimeException());
if (note.getUser() != user)
return HttpStatus.BAD_GATEWAY;
noteRepository.delete(note);
return HttpStatus.OK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public interface UserService {

void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequestDto);

UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto);
UserRegisterDto.UserResponseDto getUserDetails();

double calculateGoal(double weight, double val);
double resetGoal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
Expand Down Expand Up @@ -60,7 +61,7 @@ public void setRefreshTokenByKakaoId(Long kakaoId, String refreshToken) {
user.setRefreshToken(refreshToken);
}

@Override
@Override // TODO 사용자 추가 정보 설정
public void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequestDto) {
log.info("modify user details");
CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Expand All @@ -73,8 +74,62 @@ public void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequest
user.setHeight(Double.parseDouble(registerRequestDto.getHeight()));
user.setWeight(Double.parseDouble(registerRequestDto.getWeight()));
user.setPurpose(registerRequestDto.getPurpose());
user.setGoalProtein(calculateGoal(
Double.parseDouble(registerRequestDto.getWeight()), registerRequestDto.getPurpose().getPurpose()));

userRepository.save(user);

}

@Transactional // TODO 마이페이지 업데이트 Test
public UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto) {
CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = userDetails.getUser();
user.setHeight(updateUserRequestDto.getHeight());
user.setWeight(updateUserRequestDto.getWeight());
user.setPurpose(updateUserRequestDto.getPurpose());
user.setGoalProtein(user.getGoalProtein());
return UserRegisterDto.UserResponseDto.builder()
.id(user.getId())
.name(user.getName())
.email(user.getEmail())
.age(user.getAge())
.height(user.getHeight())
.weight(user.getWeight())
.gender(user.getGender())
.purpose(user.getPurpose())
.goalProtein(user.getGoalProtein())
.build();
}

@Override // TODO 마이페이지 테스트
public UserRegisterDto.UserResponseDto getUserDetails() {
CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = userDetails.getUser();
return UserRegisterDto.UserResponseDto.builder()
.id(user.getId())
.name(user.getName())
.email(user.getEmail())
.age(user.getAge())
.height(user.getHeight())
.weight(user.getWeight())
.gender(user.getGender())
.purpose(user.getPurpose())
.goalProtein(user.getGoalProtein())
.build();
}

@Override
public double calculateGoal(double weight, double val) {
return weight * val;
}

@Override
@Transactional
public double resetGoal() {
CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = userDetails.getUser();
user.setGoalProtein(calculateGoal(user.getWeight(), user.getPurpose().getPurpose()));
return user.getGoalProtein();
}
}

0 comments on commit 235dac2

Please sign in to comment.