74 lines
2.1 KiB
Java
74 lines
2.1 KiB
Java
package pl.edu.amu.wmi.bookapi.api;
|
|
|
|
import org.apache.coyote.Response;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import pl.edu.amu.wmi.bookapi.api.dto.BookDto;
|
|
import pl.edu.amu.wmi.bookapi.service.BookService;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/books")
|
|
public class BookController {
|
|
|
|
BookService bookService;
|
|
|
|
@Autowired
|
|
public BookController(BookService bookService) {
|
|
this.bookService = bookService;
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity getBooks() {
|
|
return ResponseEntity.ok().body(bookService.findAllForUser(
|
|
getUserName()
|
|
));
|
|
}
|
|
|
|
@GetMapping("/public")
|
|
public ResponseEntity getAllBooks() {
|
|
return ResponseEntity.ok().body(
|
|
bookService.findAll()
|
|
);
|
|
}
|
|
|
|
@PatchMapping("/{bookId}")
|
|
public ResponseEntity updateBook(@RequestBody BookDto bookDto, @PathVariable String bookId) {
|
|
String username = getUserName();
|
|
return ResponseEntity.ok(
|
|
bookService.updateBook(bookId ,username, bookDto)
|
|
);
|
|
}
|
|
|
|
@DeleteMapping("/{bookId}")
|
|
public ResponseEntity deleteBook(@PathVariable String bookId) {
|
|
bookService.deleteBook(
|
|
getUserName(),
|
|
bookId
|
|
);
|
|
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity addBook(@RequestBody BookDto bookDto) {
|
|
System.out.println("Save book");
|
|
bookService.saveBook(getUserName(), bookDto);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@PostMapping("/image")
|
|
public ResponseEntity addBookAsImage(
|
|
@RequestParam("file") MultipartFile imageFile,
|
|
@RequestParam("author") String author,
|
|
@RequestParam("title") String title) throws Exception {
|
|
bookService.handleImageUpload(imageFile, author, title);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
private String getUserName() {
|
|
return "admin";
|
|
}
|
|
}
|