first implementation
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.aiteacher.book;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/books")
|
||||
public class BookController {
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
public BookController(BookService bookService) {
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
@PostMapping(consumes = "multipart/form-data")
|
||||
public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) throws IOException {
|
||||
Book book = bookService.upload(file);
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(toSummaryResponse(book));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Map<String, Object>>> list() {
|
||||
List<Map<String, Object>> books = bookService.listAll().stream()
|
||||
.map(this::toFullResponse)
|
||||
.toList();
|
||||
return ResponseEntity.ok(books);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> get(@PathVariable UUID id) {
|
||||
Book book = bookService.getById(id);
|
||||
return ResponseEntity.ok(toFullResponse(book));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable UUID id) {
|
||||
bookService.delete(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
private Map<String, Object> toSummaryResponse(Book book) {
|
||||
return Map.of(
|
||||
"id", book.getId(),
|
||||
"title", book.getTitle(),
|
||||
"fileName", book.getFileName(),
|
||||
"status", book.getStatus().name(),
|
||||
"uploadedAt", book.getUploadedAt()
|
||||
);
|
||||
}
|
||||
|
||||
private Map<String, Object> toFullResponse(Book book) {
|
||||
var map = new java.util.LinkedHashMap<String, Object>();
|
||||
map.put("id", book.getId());
|
||||
map.put("title", book.getTitle());
|
||||
map.put("fileName", book.getFileName());
|
||||
map.put("fileSizeBytes", book.getFileSizeBytes());
|
||||
map.put("pageCount", book.getPageCount());
|
||||
map.put("status", book.getStatus().name());
|
||||
map.put("uploadedAt", book.getUploadedAt());
|
||||
map.put("processedAt", book.getProcessedAt());
|
||||
if (book.getErrorMessage() != null) {
|
||||
map.put("errorMessage", book.getErrorMessage());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user