62 lines
2.4 KiB
Java
62 lines
2.4 KiB
Java
package github.benjamin.equipreservebackend.controller;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import github.benjamin.equipreservebackend.entity.Device;
|
|
import github.benjamin.equipreservebackend.response.ResponseResult;
|
|
import github.benjamin.equipreservebackend.service.DeviceService;
|
|
import github.benjamin.equipreservebackend.vo.DeviceUserVO;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.IOException;
|
|
|
|
@RestController
|
|
@RequiredArgsConstructor
|
|
@RequestMapping("/device")
|
|
public class DeviceController {
|
|
|
|
private final DeviceService deviceService;
|
|
|
|
@PreAuthorize("hasRole('USER')")
|
|
@GetMapping
|
|
public ResponseResult<Page<DeviceUserVO>> getDevices(@RequestParam(defaultValue = "1") Integer page,
|
|
@RequestParam(defaultValue = "10") Integer size) {
|
|
Page<Device> pageRequest = new Page<>(page, size);
|
|
Page<DeviceUserVO> res = deviceService.getDeviceVO(pageRequest);
|
|
return ResponseResult.success(res);
|
|
}
|
|
|
|
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
|
@PostMapping
|
|
public ResponseResult<?> addDevice(@RequestBody Device device){
|
|
deviceService.addDevice(device);
|
|
return ResponseResult.success(device);
|
|
}
|
|
|
|
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
|
@DeleteMapping("/{id}")
|
|
public ResponseResult<?> deleteDevice(@PathVariable("id") Long id) {
|
|
deviceService.deleteDevice(id);
|
|
return ResponseResult.success();
|
|
}
|
|
|
|
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
|
@PutMapping("/{id}")
|
|
public ResponseResult<?> updateDevice(@PathVariable("id") Long id,
|
|
@RequestBody Device device) {
|
|
device.setId(id);
|
|
Device updatedDevice = deviceService.updateDevice(device);
|
|
return ResponseResult.success(updatedDevice);
|
|
}
|
|
|
|
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
|
@PostMapping("/{id}/image")
|
|
public ResponseResult<?> uploadImage(@PathVariable("id") Long id,
|
|
@RequestParam("image") MultipartFile image) throws IOException {
|
|
String imagePath = deviceService.saveImage(id, image);
|
|
return ResponseResult.success(imagePath);
|
|
}
|
|
}
|