ohctechv3/.svn/pristine/42/427b16fed165523a7e91605f89b41b2530b2a833.svn-base
2024-10-28 15:03:36 +05:30

62 lines
2.5 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.UnitMasterDto;
import com.healthcare.ohctech.entity.UnitMaster;
import com.healthcare.ohctech.service.impl.UnitMasterServiceImpl;
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("/units")
public class UnitMasterController {
@Autowired
private UnitMasterServiceImpl unitMasterServiceImpl;
@GetMapping("/{unitId}")
public ResponseEntity<?> getUnitById(@PathVariable Long unitId) {
UnitMaster unitMaster = unitMasterServiceImpl.getUnitById(unitId);
if (unitMaster != null) {
return new ResponseEntity<>(unitMaster, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getAllUnits(@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<UnitMaster> unitPage = unitMasterServiceImpl.getAllUnits(pageable);
Map<String, Object> response = PaginationUtil.getPageResponse(unitPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addUnit(@Valid @RequestBody UnitMasterDto unitMasterDto) {
unitMasterServiceImpl.addUnit(unitMasterDto);
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
}
@PutMapping("/{unitId}")
public ResponseEntity<?> updateUnit(@Valid @RequestBody UnitMasterDto unitMasterDto) {
unitMasterServiceImpl.updateUnit(unitMasterDto);
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
}
@DeleteMapping("/{unitId}")
public ResponseEntity<?> deleteUnit(@PathVariable Long unitId) {
unitMasterServiceImpl.deleteUnit(unitId);
return new ResponseEntity<>(HttpStatus.OK);
}
}