5.12
package com.example.baoli.controller;
import com.example.baoli.entity.TransferRecord;
import com.example.baoli.service.TransferService;
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/transfer")
public class TransferController {
@Autowired
private TransferService transferService;
@GetMapping
public ResponseEntity<List<TransferRecord>> getAllRecords() {
return ResponseEntity.ok(transferService.getAllRecords());
}
@GetMapping("/{id}")
public ResponseEntity<TransferRecord> getRecordById(@PathVariable Long id) {
Optional<TransferRecord> record = transferService.getRecordById(id);
return record.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity<TransferRecord> createRecord(@RequestBody TransferRecord record) {
try {
TransferRecord created = transferService.createRecord(record);
return ResponseEntity.ok(created);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
@PutMapping("/{id}")
public ResponseEntity<TransferRecord> updateRecord(@PathVariable Long id, @RequestBody TransferRecord record) {
TransferRecord updated = transferService.updateRecord(id, record);
return updated != null ? ResponseEntity.ok(updated) : ResponseEntity.notFound().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteRecord(@PathVariable Long id) {
return transferService.deleteRecord(id) ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
}
}
浙公网安备 33010602011771号