package com.healthcare.ohctech.controller; import com.healthcare.ohctech.dto.PatientCategoryDto; import com.healthcare.ohctech.entity.PatientCategory; import com.healthcare.ohctech.service.impl.AuthServiceImpl; import com.healthcare.ohctech.service.impl.PatientCategoryServiceImpl; import com.healthcare.ohctech.util.PaginationUtil; import jakarta.validation.Valid; 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 java.util.Map; @RestController @RequestMapping("/patient-categories") public class PatientCategoryController { @Autowired private PatientCategoryServiceImpl patientCategoryServiceImpl; @Autowired private AuthServiceImpl authServiceImpl; @GetMapping("/{patientCatId}") public ResponseEntity getPatientCategoryById(@PathVariable Long patientCatId) { PatientCategory patientCategory = patientCategoryServiceImpl.getPatientCategoryById(patientCatId); PatientCategoryDto patientCategoryDto = new PatientCategoryDto( patientCategory.getId(), patientCategory.getPatientCatName() ); return new ResponseEntity<>(patientCategoryDto, HttpStatus.OK); } @GetMapping public ResponseEntity getAllPatientCategories(@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); Page patientCategoryPage = patientCategoryServiceImpl.getAllPatientCategories(pageable); Page patientCategoryDtoPage = patientCategoryPage.map(patientCategory -> new PatientCategoryDto( patientCategory.getId(), patientCategory.getPatientCatName() )); Map response = PaginationUtil.getPageResponse(patientCategoryDtoPage); return new ResponseEntity<>(response, HttpStatus.OK); } @PostMapping public ResponseEntity addPatientCategory(@Valid @RequestBody PatientCategoryDto patientCategoryDto) { Long userId = authServiceImpl.getCurrentUserId(); patientCategoryServiceImpl.addPatientCategory(patientCategoryDto, userId); return new ResponseEntity<>(HttpStatus.CREATED); } @PutMapping("/{patientCatId}") public ResponseEntity updatePatientCategory(@Valid @RequestBody PatientCategoryDto patientCategoryDto) { Long userId = authServiceImpl.getCurrentUserId(); patientCategoryServiceImpl.updatePatientCategory(patientCategoryDto, userId); return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping("/{patientCatId}") public ResponseEntity deletePatientCategory(@PathVariable Long patientCatId) { patientCategoryServiceImpl.deletePatientCategory(patientCatId); return new ResponseEntity<>(HttpStatus.OK); } }