Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.onebridge.ouch.apiPayload.code.error;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum MedicalHistoryErrorCode implements ErrorCode {

USER_NOT_FOUND(HttpStatus.NOT_FOUND, "HEALTH-STATUS400", "User not found."),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USER_NOT_FOUND는 기존 에러 사용해주세요.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다:)

MEDICAL_HISTORY_NOT_FOUND(HttpStatus.NOT_FOUND, "HEALTH-STATUS401", "Health status not found.");

private final HttpStatus httpStatus;
private final String code;
private final String message;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api URI restful하게 바꿔주세요. /health-status 으로 시작하는 건 좋은 것 같습니다. 추후 도메인 수정해놓겠습니다.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전반적으로 다른 pr에서 리뷰한 거랑 거의 비슷한 문제들이 있는 것 같아서 참고해서 수정해주세요.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다 :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.onebridge.ouch.controller.medicalHistory;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.onebridge.ouch.apiPayload.ApiResponse;
import com.onebridge.ouch.dto.medicalHistory.request.MedicalHistoryCreateRequest;
import com.onebridge.ouch.dto.medicalHistory.request.MedicalHistoryUpdateRequest;
import com.onebridge.ouch.dto.medicalHistory.response.DateAndDisease;
import com.onebridge.ouch.dto.medicalHistory.response.GetMedicalHistoryResponse;
import com.onebridge.ouch.dto.medicalHistory.response.MedicalHistoryCreateResponse;
import com.onebridge.ouch.dto.medicalHistory.response.MedicalHistoryUpdateResponse;
import com.onebridge.ouch.security.userDetail.OuchUserDetails;
import com.onebridge.ouch.service.medicalHistory.MedicalHistoryService;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/health-status")
@RequiredArgsConstructor
public class MedicalHistoryController {

private final MedicalHistoryService medicalHistoryService;

//건강상태 생성
// @PostMapping("/{userId}")
@PostMapping("/create")
public ResponseEntity<ApiResponse<MedicalHistoryCreateResponse>> createMedicalHistory(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post에서 응답은 따로 필요 없으니 Void로 받으시면 되시고 관련 dto도 없에주시면 감사하겠습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다:)

@RequestBody @Valid MedicalHistoryCreateRequest request,
// @PathVariable Long userId
@AuthenticationPrincipal OuchUserDetails userDetails
) {
Long userId = userDetails.getDatabaseId();
MedicalHistoryCreateResponse response = medicalHistoryService.createMedicalHistory(request, userId);
return ResponseEntity.ok(ApiResponse.created(response));
}

//특정 건강상태 조회
@GetMapping("/get/{healthStatusId}")
public ResponseEntity<ApiResponse<GetMedicalHistoryResponse>> getMedicalHistory(@PathVariable Long healthStatusId) {
GetMedicalHistoryResponse response = medicalHistoryService.getMedicalHistory(healthStatusId);
return ResponseEntity.ok(ApiResponse.success(response));
}

//특정 사용자의 모든 건강상태 조회
// @GetMapping("/get-all/{userId}")
@GetMapping("/get-all")
public ResponseEntity<ApiResponse<List<DateAndDisease>>> getUsersAllMedicalHistory(
// @PathVariable Long userId
@AuthenticationPrincipal OuchUserDetails userDetails
) {
Long userId = userDetails.getDatabaseId();
List<DateAndDisease> list = medicalHistoryService.getUsersAllMedicalHistory(userId);
return ResponseEntity.ok(ApiResponse.success(list));
}

//특정 건강상태 수정
@PutMapping("/update/{healthStatusId}")
public ResponseEntity<ApiResponse<MedicalHistoryUpdateResponse>> updateMedicalHistory(
@RequestBody @Valid MedicalHistoryUpdateRequest request,
@PathVariable Long healthStatusId) {
MedicalHistoryUpdateResponse response = medicalHistoryService.updateMedicalHistory(request, healthStatusId);
return ResponseEntity.ok(ApiResponse.success(response));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

응답 삭제2


//특정 건강상태 삭제
@DeleteMapping("/delete/{healthStatusId}")
public ResponseEntity<ApiResponse<Void>> deleteMedicalHistory(@PathVariable Long healthStatusId) {
medicalHistoryService.deleteMedicalHistory(healthStatusId);
return ResponseEntity.ok(ApiResponse.successWithNoData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.onebridge.ouch.converter;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.onebridge.ouch.domain.MedicalHistory;
import com.onebridge.ouch.domain.User;
import com.onebridge.ouch.dto.medicalHistory.request.MedicalHistoryCreateRequest;
import com.onebridge.ouch.dto.medicalHistory.request.MedicalHistoryUpdateRequest;
import com.onebridge.ouch.dto.medicalHistory.response.DateAndDisease;
import com.onebridge.ouch.dto.medicalHistory.response.GetMedicalHistoryResponse;
import com.onebridge.ouch.dto.medicalHistory.response.MedicalHistoryCreateResponse;
import com.onebridge.ouch.dto.medicalHistory.response.MedicalHistoryUpdateResponse;

@Component
public class MedicalHistoryConverter {

public MedicalHistoryCreateResponse medicalHistory2MedicalHistoryResponse(MedicalHistory medicalHistory,
Long userId) {
return new MedicalHistoryCreateResponse(medicalHistory.getId(),
medicalHistory.getDisease(),
medicalHistory.getAllergy(),
medicalHistory.getBloodPressure(), medicalHistory.getBloodSugar(), medicalHistory.getMedicineHistory());
}

public List<DateAndDisease> medicalHistory2GetUsersAllMedicalHistoryResponse(
List<MedicalHistory> medicalHistory) {

List<DateAndDisease> list = new ArrayList<>();

for (MedicalHistory history : medicalHistory) {
list.add(new DateAndDisease(history.getId(), history.getUpdatedAt().toString(), history.getDisease()));
}

return list;
}

public GetMedicalHistoryResponse medicalHistory2GetMedicalHistoryResponse(MedicalHistory medicalHistory) {
return new GetMedicalHistoryResponse(medicalHistory.getId(), medicalHistory.getDisease(),
medicalHistory.getAllergy(), medicalHistory.getBloodPressure(), medicalHistory.getBloodSugar(),
medicalHistory.getMedicineHistory());
}

public MedicalHistoryUpdateResponse medicalHistory2MedicalHistoryUpdateResponse(MedicalHistory medicalHistory) {
return new MedicalHistoryUpdateResponse(medicalHistory.getId(), medicalHistory.getDisease(),
medicalHistory.getAllergy(), medicalHistory.getBloodPressure(), medicalHistory.getBloodSugar(),
medicalHistory.getMedicineHistory());
}

public MedicalHistory medicalHistoryCreateRequest2MedicalHistory(MedicalHistoryCreateRequest request, User user) {
return MedicalHistory.builder()
.user(user)
.disease(request.getDisease())
.allergy(request.getAllergy())
.bloodPressure(request.getBloodPressure())
.bloodSugar(request.getBloodSugar())
.medicineHistory(request.getMedicineHistory())
.build();
}

public MedicalHistory medicalHistoryUpdateRequest2MedicalHistory(MedicalHistory medicalHistory,
MedicalHistoryUpdateRequest request) {
return medicalHistory.toBuilder()
.disease(request.getDisease())
.allergy(request.getAllergy())
.bloodPressure(request.getBloodPressure())
.bloodSugar(request.getBloodSugar())
.medicineHistory(request.getMedicineHistory())
.build();
}
}
41 changes: 34 additions & 7 deletions src/main/java/com/onebridge/ouch/domain/MedicalHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,51 @@

import com.onebridge.ouch.domain.common.BaseEntity;

import jakarta.persistence.*;
import lombok.*;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@Builder(toBuilder = true)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class MedicalHistory extends BaseEntity {

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

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@MapsId // User의 id를 MedicalHistory의 기본 키로 매핑
@JoinColumn(name = "user_id") //외래 키 이름을 user_id로 설정
@ManyToOne(fetch = FetchType.LAZY) //cascade = CascadeType.ALL -> medicalHistory 삭제 시 user 도 삭제됨
// @JoinColumn(name = "user_id") //외래 키 이름을 user_id로 설정
@JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "fk_medical_history_user",
value = ConstraintMode.CONSTRAINT, foreignKeyDefinition = "ON DELETE CASCADE"))
private User user;

@Column(nullable = true, columnDefinition = "TEXT")
private String contents;
private String disease;

@Column(nullable = true, columnDefinition = "TEXT")
private String allergy;

@Column(nullable = true, columnDefinition = "TEXT")
private Long bloodPressure;

@Column(nullable = true, columnDefinition = "TEXT")
private Long bloodSugar;

@Column(nullable = true, columnDefinition = "TEXT")
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.onebridge.ouch.dto.medicalHistory.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

@Getter
public class MedicalHistoryCreateRequest {

@NotBlank(message = "Disease is required.")
private String disease;

@NotBlank(message = "Allergy is required.")
private String allergy;

@NotNull(message = "Blood pressure is required.")
private Long bloodPressure;

@NotNull(message = "Blood sugar level is required.")
private Long bloodSugar;

@NotBlank(message = "Medical History is required.")
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.onebridge.ouch.dto.medicalHistory.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

@Getter
public class MedicalHistoryUpdateRequest {

@NotBlank(message = "Disease is required.")
private String disease;

@NotBlank(message = "Allergy is required.")
private String allergy;

@NotNull(message = "Blood pressure is required.")
private Long bloodPressure;

@NotNull(message = "Blood sugar level is required.")
private Long bloodSugar;

@NotBlank(message = "Medical History is required.")
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.onebridge.ouch.dto.medicalHistory.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class DateAndDisease {

private Long id;
private String date;
private String disease;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.onebridge.ouch.dto.medicalHistory.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class GetMedicalHistoryResponse {

private Long id;
private String disease;
private String allergy;
private Long bloodPressure;
private Long bloodSugar;
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.onebridge.ouch.dto.medicalHistory.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class MedicalHistoryCreateResponse {
private Long id;
private String disease;
private String allergy;
private Long bloodPressure;
private Long bloodSugar;
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.onebridge.ouch.dto.medicalHistory.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class MedicalHistoryUpdateResponse {
private Long id;
private String disease;
private String allergy;
private Long bloodPressure;
private Long bloodSugar;
private String medicineHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.onebridge.ouch.repository.medicalHistory;

import java.util.List;

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

import com.onebridge.ouch.domain.MedicalHistory;

@Repository
public interface MedicalHistoryRepository extends JpaRepository<MedicalHistory, Long> {
List<MedicalHistory> findAllByUserId(Long userId);
}
Loading