using APITest2.Models; using APITest2.Services; using Microsoft.AspNetCore.Mvc; namespace APITest2.Controllers; [ApiController] [Route("api/[controller]")] public class BooksController : ControllerBase { private readonly BooksService _booksService; public BooksController(BooksService booksService) => _booksService = booksService; [HttpGet] public async Task> Get() => await _booksService.GetAsync(); [HttpGet("{id:length(24)}")] public async Task> Get(string id) { var book = await _booksService.GetAsync(id); if (book is null) { return NotFound(); } return book; } [HttpPost] public async Task Post(Book newBook) { await _booksService.CreateAsync(newBook); return CreatedAtAction(nameof(Get), new { id = newBook.Id }, newBook); } [HttpPut("{id:length(24)}")] public async Task Update(string id, Book updatedBook) { var book = await _booksService.GetAsync(id); if (book is null) { return NotFound(); } updatedBook.Id = book.Id; await _booksService.UpdateAsync(id, updatedBook); return NoContent(); } [HttpDelete("{id:length(24)}")] public async Task Delete(string id) { var book = await _booksService.GetAsync(id); if (book is null) { return NotFound(); } await _booksService.DeleteAsync(id); return NoContent(); } }