62 lines
2.4 KiB
Plaintext
62 lines
2.4 KiB
Plaintext
package com.healthcare.ohctech.controller;
|
|
|
|
import com.healthcare.ohctech.dto.DeviceDto;
|
|
import com.healthcare.ohctech.entity.DeviceMaster;
|
|
import com.healthcare.ohctech.service.DeviceMasterService;
|
|
import com.healthcare.ohctech.util.PaginationUtil;
|
|
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 jakarta.validation.Valid;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/devices")
|
|
public class DeviceMasterController {
|
|
|
|
@Autowired
|
|
private DeviceMasterService deviceMasterService;
|
|
|
|
@GetMapping("/{id}")
|
|
public ResponseEntity<?> getDeviceById(@PathVariable Long id) {
|
|
DeviceMaster deviceMaster = deviceMasterService.getDeviceById(id);
|
|
if (deviceMaster != null) {
|
|
return new ResponseEntity<>(deviceMaster, HttpStatus.OK);
|
|
}
|
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity<?> getAllDevices(@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<DeviceMaster> devicePage = deviceMasterService.getAllDevices(pageable);
|
|
Map<String, Object> response = PaginationUtil.getPageResponse(devicePage);
|
|
return new ResponseEntity<>(response, HttpStatus.OK);
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity<?> addDevice(@Valid @RequestBody DeviceDto deviceDto) {
|
|
deviceMasterService.addDevice(deviceDto);
|
|
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<?> updateDevice(@Valid @RequestBody DeviceDto deviceDto) {
|
|
deviceMasterService.updateDevice(deviceDto);
|
|
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<?> deleteDevice(@PathVariable Long id) {
|
|
deviceMasterService.deleteDevice(id);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
}
|