package com.healthcare.ohctech.controller; import com.healthcare.ohctech.dto.FoodMasterDto; import com.healthcare.ohctech.entity.FoodMaster; import com.healthcare.ohctech.service.impl.FoodMasterServiceImpl; 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("/foods") public class FoodMasterController { @Autowired private FoodMasterServiceImpl foodMasterServiceImpl; @GetMapping("/{foodId}") public ResponseEntity getFoodById(@PathVariable Long foodId) { FoodMaster foodMaster = foodMasterServiceImpl.getFoodById(foodId); return new ResponseEntity<>(foodMaster, HttpStatus.OK); } @GetMapping public ResponseEntity getAllFoods(@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 foodMasterPage = foodMasterServiceImpl.getAllFoods(pageable); Map response = PaginationUtil.getPageResponse(foodMasterPage); return new ResponseEntity<>(response, HttpStatus.OK); } @PostMapping public ResponseEntity addFood(@Valid @RequestBody FoodMasterDto foodMasterDto) { foodMasterServiceImpl.addFood(foodMasterDto); return new ResponseEntity<>(HttpStatus.CREATED); } @PutMapping("/{foodId}") public ResponseEntity updateFood(@Valid @RequestBody FoodMasterDto foodMasterDto) { foodMasterServiceImpl.updateFood(foodMasterDto); return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping("/{foodId}") public ResponseEntity deleteFood(@PathVariable Long foodId) { foodMasterServiceImpl.deleteFood(foodId); return new ResponseEntity<>(HttpStatus.OK); } }