/
zykovad
/
PIR_Lab1_E-Library
Обзор
Документация
Войти
/
zykovad
/
PIR_Lab1_E-Library
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Controllers/v1/AuthorController.cs
133 строки
4 KB
Darya
Done Lab 1
09 окт 2025, 18:04
09 окт 2025, 18:04
48965dd
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Mvc; using libraryAPI.DataStorage.v1; using libraryAPI.Entities.v1; using libraryAPI.DTO; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.RateLimiting; namespace libraryAPI.Controllers.v1 { [ApiController] [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[controller]")] [EnableRateLimiting("UserPolicy")] public class AuthorsController : ControllerBase { private readonly Data _data; public AuthorsController(Data data) { _data = data; } [HttpGet("GetAllAuthors")] [SwaggerOperation(Summary = "�������� ������ ���� �������", OperationId = "GetAllAuthors")] [SwaggerResponse(200, "������ ������� ������� �������", typeof(IEnumerable<Author_Response>))] public IResult GetAuthors() { var authors = _data.Authors.Select(a => new Author_Response { Id = a.Id, Name = a.Name, Surname = a.Surname, Patronymic = a.Patronymic, Bio = a.Bio }).ToList(); return Results.Json(authors); } [HttpGet("GetAuthorById/{id}")] [SwaggerOperation(Summary = "�������� ������ �� id", OperationId = "GetAuthorById")] [SwaggerResponse(200, "����� ������", typeof(Author_Response))] public IResult GetAuthor(int id) { var author = _data.Authors.FirstOrDefault(a => a.Id == id); if (author == null) return Results.NotFound(); var response = new Author_Response { Id = author.Id, Name = author.Name, Surname = author.Surname, Patronymic = author.Patronymic, Bio = author.Bio }; return Results.Json(response); } [HttpPost("CreateAuthor")] [SwaggerOperation(Summary = "������� ������", OperationId = "CreateAuthor")] [SwaggerResponse(200, "����� ������� ������", typeof(Author_Response))] public IResult CreateAuthor(Author_Request dto, [FromHeader(Name = "Idempotency-Key")] string idempotencyKey) { if (string.IsNullOrEmpty(idempotencyKey)) return Results.BadRequest("Missing Idempotency-Key header."); if (_data.IdempotencyCache.ContainsKey(idempotencyKey)) return Results.Json(_data.IdempotencyCache[idempotencyKey]); var author = new Author { Id = _data.Authors.Max(a => a.Id) + 1, Name = dto.Name, Surname = dto.Surname, Patronymic = dto.Patronymic, Bio = dto.Bio }; _data.Authors.Add(author); _data.IdempotencyCache[idempotencyKey] = author; var response = new Author_Response { Id = author.Id, Name = author.Name, Surname = author.Surname, Patronymic = author.Patronymic, Bio = author.Bio }; return Results.Json(response); } [HttpPut("UpdateAuthorById/{id}")] [SwaggerOperation(Summary = "�������� ������ �� id", OperationId = "UpdateAuthorById")] [SwaggerResponse(200, "����� ������� ��������", typeof(Author_Response))] public IResult UpdateAuthor(int id, Author_Request dto) { var author = _data.Authors.FirstOrDefault(a => a.Id == id); if (author == null) return Results.NotFound(); author.Name = dto.Name; author.Surname = dto.Surname; author.Patronymic = dto.Patronymic; author.Bio = dto.Bio; var response = new Author_Response { Id = author.Id, Name = author.Name, Surname = author.Surname, Patronymic = author.Patronymic, Bio = author.Bio }; return Results.Json(response); } [HttpDelete("DeleteAuthorById/{id}")] [SwaggerOperation(Summary = "������� ������ �� id", OperationId = "DeleteAuthorById")] [SwaggerResponse(200, "����� ������� ������")] public IResult DeleteAuthor(int id) { var author = _data.Authors.FirstOrDefault(a => a.Id == id); if (author == null) return Results.NotFound(); _data.Authors.Remove(author); return Results.Ok($"Author with ID {id} deleted."); } } }