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

81 lines
3.3 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.AuthServiceImpl;
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;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{unitId}")
public ResponseEntity<?> getUnitById(@PathVariable Long unitId) {
UnitMaster unitMaster = unitMasterServiceImpl.getUnitById(unitId);
UnitMasterDto unitMasterDto = new UnitMasterDto(
unitMaster.getId(),
unitMaster.getUnitName(),
unitMaster.getUnitCode(),
unitMaster.getRemarks(),
unitMaster.getModifiedBy(),
unitMaster.getLastModified()
);
return new ResponseEntity<>(unitMasterDto,HttpStatus.OK);
}
@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> unitMasterPage = unitMasterServiceImpl.getAllUnits(pageable);
Page<UnitMasterDto> unitMasterDtoPage = unitMasterPage.map(unitMaster -> new UnitMasterDto(
unitMaster.getId(),
unitMaster.getUnitName(),
unitMaster.getUnitCode(),
unitMaster.getRemarks(),
unitMaster.getModifiedBy(),
unitMaster.getLastModified()
));
Map<String, Object> response = PaginationUtil.getPageResponse(unitMasterDtoPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addUnit(@Valid @RequestBody UnitMasterDto unitMasterDto) {
Long userId = authServiceImpl.getCurrentUserId();
unitMasterServiceImpl.addUnit(unitMasterDto,userId);
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
}
@PutMapping("/{unitId}")
public ResponseEntity<?> updateUnit(@PathVariable Long unitId,@Valid @RequestBody UnitMasterDto unitMasterDto) {
Long userId = authServiceImpl.getCurrentUserId();
unitMasterServiceImpl.updateUnit(unitId,unitMasterDto,userId);
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
}
@DeleteMapping("/{unitId}")
public ResponseEntity<?> deleteUnit(@PathVariable Long unitId) {
unitMasterServiceImpl.deleteUnit(unitId);
return new ResponseEntity<>(HttpStatus.OK);
}
}