ohctechv3/.svn/pristine/d4/d43866a9c67e2a3139e5bf8a0ec69a041509203a.svn-base
2024-10-28 15:03:36 +05:30

59 lines
2.3 KiB
Plaintext

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<FoodMaster> foodMasterPage = foodMasterServiceImpl.getAllFoods(pageable);
Map<String, Object> 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);
}
}