72 lines
1.6 KiB
C#
72 lines
1.6 KiB
C#
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<List<Book>> Get() =>
|
|
await _booksService.GetAsync();
|
|
|
|
[HttpGet("{id:length(24)}")]
|
|
public async Task<ActionResult<Book>> Get(string id)
|
|
{
|
|
var book = await _booksService.GetAsync(id);
|
|
|
|
if (book is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return book;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Post(Book newBook)
|
|
{
|
|
await _booksService.CreateAsync(newBook);
|
|
|
|
return CreatedAtAction(nameof(Get), new { id = newBook.Id }, newBook);
|
|
}
|
|
|
|
[HttpPut("{id:length(24)}")]
|
|
public async Task<IActionResult> 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<IActionResult> Delete(string id)
|
|
{
|
|
var book = await _booksService.GetAsync(id);
|
|
|
|
if (book is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
await _booksService.DeleteAsync(id);
|
|
|
|
return NoContent();
|
|
}
|
|
} |