-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorsController.cs
91 lines (75 loc) · 2.6 KB
/
AuthorsController.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
using Library.BLL;
using Library.Model;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NuGet.Protocol;
namespace Library.MVC.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthorsController : ControllerBase
{
private readonly IAuthorService _authorService;
private readonly IBookService _bookService;
public AuthorsController(IAuthorService authorService, IBookService bookService)
{
_authorService = authorService;
_bookService = bookService;
}
[HttpGet("Books")]
public IActionResult GetAuthorBooks([FromQuery(Name = "Key")] string? key = null)
{
IEnumerable<Author> authors;
if(key == null)
{
authors = _authorService.GetAuthors();
var data = new List<object>();
foreach(var author in authors)
{
data.Add(new { author, author.WrittenBooks });
}
return Ok(data);
}
{
key = key.ToUpper();
authors =
_authorService.GetAuthors()
.Where(a =>
a.Name.ToUpper().Contains(key) ||
a.Surname.ToUpper().Contains(key)
);
var data = new List<object>();
foreach (var author in authors)
{
data.Add(new { author, author.WrittenBooks });
}
return Ok(data);
}
}
[HttpGet]
public IActionResult GetAuthors()
{
return Ok(_authorService.GetAuthors());
}
[HttpPost, /*Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)*/]
public void AddAuthor(Author author)
{
_authorService.AddAuthor(author);
}
[HttpDelete("{id}"), /*Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)*/]
public ActionResult DeleteAuthor(int id)
{
if(_authorService.FindAuthor(id) == null)
return NotFound();
_authorService.DeleteAuthor(id);
return Ok();
}
[HttpPut, /*Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)*/]
public void UpdateAuthor(Author author)
{
_authorService.UpdateAuthor(author);
}
}
}