5.20
package com.example.baoli.controller;
import com.example.baoli.entity.OutboundRecord;
import com.example.baoli.service.OutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/outbound")
public class OutboundController {
@Autowired
private OutboundService outboundService;
@GetMapping
public ResponseEntity<List<OutboundRecord>> getAllOutboundRecords() {
return ResponseEntity.ok(outboundService.getAllOutboundRecords());
}
@GetMapping("/{id}")
public ResponseEntity<OutboundRecord> getOutboundRecordById(@PathVariable Long id) {
Optional<OutboundRecord> record = outboundService.getOutboundRecordById(id);
return record.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity<OutboundRecord> createOutboundRecord(@RequestBody OutboundRecord outboundRecord) {
try {
OutboundRecord created = outboundService.saveOutboundRecord(outboundRecord);
return ResponseEntity.ok(created);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
@PutMapping("/{id}")
public ResponseEntity<OutboundRecord> updateOutboundRecord(@PathVariable Long id, @RequestBody OutboundRecord outboundRecord) {
Optional<OutboundRecord> existingRecord = outboundService.getOutboundRecordById(id);
if (existingRecord.isPresent()) {
outboundRecord.setId(id);
OutboundRecord updated = outboundService.saveOutboundRecord(outboundRecord);
return ResponseEntity.ok(updated);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteOutboundRecord(@PathVariable Long id) {
outboundService.deleteOutboundRecordById(id);
return ResponseEntity.ok().build();
}
@GetMapping("/search")
public ResponseEntity<List<OutboundRecord>> searchOutboundRecords(
@RequestParam(required = false) String keyword) {
return ResponseEntity.ok(outboundService.searchOutboundRecords(keyword));
}
@PostMapping("/batch-approve")
public ResponseEntity<List<OutboundRecord>> batchApproveOutbound(@RequestBody List<Long> ids, @RequestParam String approver) {
List<OutboundRecord> approvedRecords = outboundService.batchApproveOutbound(ids, approver);
return ResponseEntity.ok(approvedRecords);
}
@PostMapping("/return-borrowed/{id}")
public ResponseEntity<OutboundRecord> returnBorrowedPart(@PathVariable Long id, @RequestParam String returnPerson) {
OutboundRecord returnedRecord = outboundService.returnBorrowedPart(id, returnPerson);
return returnedRecord != null ? ResponseEntity.ok(returnedRecord) : ResponseEntity.notFound().build();
}
}