-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooksController.cs
110 lines (90 loc) · 3.19 KB
/
BooksController.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Microsoft.AspNetCore.Mvc;
using Library.Model;
using Library.BLL;
using Microsoft.AspNetCore.Authorization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authentication.JwtBearer;
namespace Library.MVC.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BooksController : ControllerBase
{
private readonly IBookService _bookService;
public BooksController(IBookService bookService)
{
_bookService = bookService;
}
// GET: api/Books
[HttpGet]
public IActionResult GetBooks([FromQuery(Name ="Title")] string? title = null)
{
IEnumerable<Book> books;
if(title == null)
books = _bookService.GetBooks();
else
books = _bookService.GetBooksFilter(title);
return Ok(books);
}
[HttpGet("Count")]
public IActionResult CountBooks()
{
return Ok(_bookService.CountBooks());
}
[HttpGet("Borrowed/{title}")]
public IActionResult GetBorrowedBooksByTitle(string title)
{
return Ok(_bookService.GetBorrowsByTitle(title));
}
// GET: api/Books/5
[HttpGet("{id}")]
public IActionResult GetBook(int id)
{
var book = _bookService.FindBook(id);
if (book == null)
return NotFound();
return Ok(book);
}
// PUT: api/Books/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult PutBook(int id, Book book)
{
var existingBook = _bookService.FindBook(id);
if (existingBook == null)
{
return NotFound();
}
existingBook.Title = book.Title;
existingBook.Authors = book.Authors;
existingBook.PublishingHouse = book.PublishingHouse;
existingBook.PublishingHouseId = book.PublishingHouseId;
_bookService.UpdateBook(existingBook);
return Ok();
}
// POST: api/Books
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost, Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult PostBook(params Book[] books)
{
foreach(var book in books)
{
_bookService.AddBook(book);
}
return CreatedAtAction("AddBooks", new { books });
}
// DELETE: api/Books/5
[HttpDelete("{id}"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult DeleteBook(int id)
{
var book = _bookService.GetBooks().Where(b=>b.Id == id).FirstOrDefault();
if (book == null)
{
return NotFound();
}
_bookService.DeleteBook(book.Id);
return Ok();
}
}
}