Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 {

MEDICAL_HISTORY_NOT_FOUND(HttpStatus.NOT_FOUND, "HEALTH-STATUS401", "건강상태가 존재하지 않습니다.");

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,88 @@
package com.onebridge.ouch.controller.medicalHistory;

import java.util.List;

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.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.security.authorization.UserId;
import com.onebridge.ouch.service.medicalHistory.MedicalHistoryService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Tag(name = "건강 상태 API", description = "건강 상태 API")
@RestController
@RequestMapping("/health-status")
@RequiredArgsConstructor
public class MedicalHistoryController {

private final MedicalHistoryService medicalHistoryService;

//건강상태 생성
@Operation(summary = "건강상태 생성 API", description = "건강상태 생성 API 입니다.")
@PostMapping
public ResponseEntity<ApiResponse<Void>> createMedicalHistory(
@RequestBody @Valid MedicalHistoryCreateRequest request,
@UserId Long userId
) {
medicalHistoryService.createMedicalHistory(request, userId);
return ResponseEntity.ok(ApiResponse.successWithNoData());
}

//특정 건강상태 조회
@Operation(summary = "건강상태 조회 API", description = "건강상태 조회 API 입니다.")
@GetMapping("/{healthStatusId}")
public ResponseEntity<ApiResponse<GetMedicalHistoryResponse>> getMedicalHistory(@PathVariable Long healthStatusId,
@UserId Long userId
) {
GetMedicalHistoryResponse response = medicalHistoryService.getMedicalHistory(healthStatusId, userId);
return ResponseEntity.ok(ApiResponse.success(response));
}

//특정 사용자의 모든 건강상태 조회
@Operation(summary = "특정 사용자의 모든 건강상태 조회 API", description = "특정 사용자의 모든 건강상태 조회 API 입니다.")
@GetMapping
public ResponseEntity<ApiResponse<List<DateAndDisease>>> getUsersAllMedicalHistory(
@UserId Long userId
) {
List<DateAndDisease> list = medicalHistoryService.getUsersAllMedicalHistory(userId);
return ResponseEntity.ok(ApiResponse.success(list));
}

//특정 건강상태 수정
@Operation(summary = "건강상태 수정 API", description = "건강상태 수정 API 입니다.")
@PutMapping("/{healthStatusId}")
public ResponseEntity<ApiResponse<Void>> updateMedicalHistory(
@RequestBody @Valid MedicalHistoryUpdateRequest request,
@PathVariable Long healthStatusId,
@UserId Long userId
) {
medicalHistoryService.updateMedicalHistory(request, healthStatusId, userId);
return ResponseEntity.ok(ApiResponse.successWithNoData());
}
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


//특정 건강상태 삭제
@Operation(summary = "건강상태 삭제 API", description = "건강상태 삭제 API 입니다.")
@DeleteMapping("/{healthStatusId}")
public ResponseEntity<ApiResponse<Void>> deleteMedicalHistory(@PathVariable Long healthStatusId,
@UserId Long userId
) {
medicalHistoryService.deleteMedicalHistory(healthStatusId, userId);
return ResponseEntity.ok(ApiResponse.successWithNoData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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;

@Component
public class MedicalHistoryConverter {

public List<DateAndDisease> medicalHistoryToGetUsersAllMedicalHistoryResponse(
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 medicalHistoryToGetMedicalHistoryResponse(MedicalHistory medicalHistory) {
return new GetMedicalHistoryResponse(medicalHistory.getId(), medicalHistory.getDisease(),
medicalHistory.getAllergy(), medicalHistory.getBloodPressure(), medicalHistory.getBloodSugar(),
medicalHistory.getMedicineHistory());
}

public MedicalHistory medicalHistoryCreateRequestToMedicalHistory(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 medicalHistoryUpdateRequestToMedicalHistory(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,16 @@
package com.onebridge.ouch.repository.medicalHistory;

import java.util.List;
import java.util.Optional;

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);

Optional<MedicalHistory> findByIdAndUserId(Long medicalHistoryId, Long userId);
}
Loading