package com.healthcare.ohctech.controller; import com.healthcare.ohctech.dto.PatientDietPlanDto; import com.healthcare.ohctech.entity.PatientDietPlan; import com.healthcare.ohctech.service.impl.AuthServiceImpl; import com.healthcare.ohctech.service.impl.PatientDietPlanServiceImpl; import com.healthcare.ohctech.util.PaginationUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import jakarta.validation.Valid; import java.util.*; @RestController @RequestMapping("/patient-diet-plans") public class PatientDietPlanController { @Autowired private PatientDietPlanServiceImpl patientDietPlanServiceImpl; @Autowired private AuthServiceImpl authServiceImpl; @GetMapping("/{consultationId}") public ResponseEntity getPatientDietPlansByConsultationId(@PathVariable Long consultationId) { List dietPlans = patientDietPlanServiceImpl.getPatientDietPlansByConsultationId(consultationId); return new ResponseEntity<>(dietPlans, HttpStatus.OK); } @GetMapping public ResponseEntity getAllPatientDietPlans(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size, @RequestParam(required = false) String sortBy, @RequestParam(required = false) String sortOrder) { Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder); List groupedPlans = patientDietPlanServiceImpl.getAllPatientDietPlans(); Page planDtoPage = PaginationUtil.getPageFromList(groupedPlans, pageable); Map response = PaginationUtil.getPageResponse(planDtoPage); return new ResponseEntity<>(response, HttpStatus.OK); } @PostMapping public ResponseEntity addPatientDietPlan(@Valid @RequestBody PatientDietPlanDto patientDietPlanDto) { Long userId = authServiceImpl.getCurrentUserId(); patientDietPlanServiceImpl.addPatientDietPlan(patientDietPlanDto, userId); return new ResponseEntity<>(HttpStatus.CREATED); } @PutMapping("/{consultationId}") public ResponseEntity updatePatientDietPlan(@PathVariable Long consultationId, @Valid @RequestBody PatientDietPlanDto patientDietPlanDto) { Long userId = authServiceImpl.getCurrentUserId(); patientDietPlanServiceImpl.updatePatientDietPlan(consultationId, patientDietPlanDto, userId); return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping("/{id}") public ResponseEntity deletePatientDietPlan(@PathVariable Long id) { patientDietPlanServiceImpl.deletePatientDietPlan(id); return new ResponseEntity<>(HttpStatus.OK); } }