5.29
package com.example.baoli.controller;
import com.example.baoli.entity.FactoryReturnRecord;
import com.example.baoli.service.FactoryReturnService;
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/factoryreturn")
public class FactoryReturnController {
@Autowired
private FactoryReturnService factoryreturnService;
@GetMapping
public ResponseEntity<List<FactoryReturnRecord>> getAllRecords() {
return ResponseEntity.ok(factoryreturnService.getAllRecords());
}
@GetMapping("/{id}")
public ResponseEntity<FactoryReturnRecord> getRecordById(@PathVariable Long id) {
Optional<FactoryReturnRecord> record = factoryreturnService.getRecordById(id);
return record.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity<FactoryReturnRecord> createRecord(@RequestBody FactoryReturnRecord record) {
try {
FactoryReturnRecord created = factoryreturnService.createRecord(record);
return ResponseEntity.ok(created);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
@PutMapping("/{id}")
public ResponseEntity<FactoryReturnRecord> updateRecord(@PathVariable Long id, @RequestBody FactoryReturnRecord record) {
FactoryReturnRecord updated = factoryreturnService.updateRecord(id, record);
return updated != null ? ResponseEntity.ok(updated) : ResponseEntity.notFound().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteRecord(@PathVariable Long id) {
return factoryreturnService.deleteRecord(id) ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
}
}