blob: ad336f0b514100f733fdb0b8e4a1389d40d212f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using LibraryAPI.Models;
namespace LibraryAPI.Controllers {
[Route("api/books")]
[ApiController]
public class BooksController : ControllerBase {
private readonly LibraryContext _context;
public BooksController(LibraryContext context) {
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Book>>> GetBooks() {
return await _context.Books.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<Book>> GetBook(long id) {
if (_context.Books == null)
return NotFound();
var book = await _context.Books.FindAsync(id);
if (book == null)
return NotFound();
return book;
}
[HttpPut("{id:int}")]
public async Task<ActionResult> PutBook(long id, Book book) {
if (id != book.Id)
return BadRequest("Attempted to modify a different resource");
_context.Update(book);
await _context.SaveChangesAsync();
var updated = await _context.Books.FindAsync(book.Id);
return Ok();
}
[HttpPost]
public async Task<ActionResult> PostBook(Book book) {
var author_exists = await _context.Authors.FindAsync(book.AuthorId);
if(author_exists == null)
return BadRequest($"Author with id {book.AuthorId} doesn't exist");
_context.Books.Add(book);
await _context.SaveChangesAsync();
return Ok();
}
// DELETE: api/books/5
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteBook(long id) {
var book = await _context.Books.FindAsync(id);
if (book == null)
return NotFound();
_context.Books.Remove(book);
await _context.SaveChangesAsync();
return Ok();
}
}
}
|