/
zykovad
/
PIR_Lab1_E-Library
Обзор
Документация
Войти
/
zykovad
/
PIR_Lab1_E-Library
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Controllers/v2/AuthorController.cs
151 строка
6 KB
Darya
Done lab 2
17 окт 2025, 22:38
17 окт 2025, 22:38
38ce9ac
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Mvc; using libraryAPI.DataStorage.v2; using libraryAPI.Entities.v2; using libraryAPI.DTO; using Swashbuckle.AspNetCore.Annotations; namespace libraryAPI.Controllers.v2 { [ApiController] [ApiVersion("2.0")] [Route("api/v{version:apiVersion}/[controller]")] public class AuthorsController : ControllerBase { private readonly Data _data; public AuthorsController(Data data) { _data = data; } [HttpGet()] [SwaggerOperation(Summary = "�������� ������ ���� ������� (� ����������)", Description = "����� ������� ���� ����� �������� include (��������: ?include=name,surname). ������������ ����: name,surname, patronymic, bio.", OperationId = "GetAllAuthors")] [SwaggerResponse(200, "������ ������� ������� �������", typeof(PagedResult<Author_Response>))] public IResult GetAuthors(int page = 1, int pageSize = 10, [FromQuery] string? include = null) { // ����������� ������ include � ������ var includeFields = include?.Split(',').Select(f => f.Trim().ToLower()).ToHashSet() ?? new HashSet<string>(); var list = _data.Authors.ToList(); var total = list.Count; var items = list.Skip((page - 1) * pageSize).Take(pageSize) .Select(a => new Author_Response { Id = a.Id, Name = includeFields.Count == 0 || includeFields.Contains("name") ? a.Name : null, Surname = includeFields.Count == 0 || includeFields.Contains("surname") ? a.Surname : null, Patronymic = includeFields.Count == 0 || includeFields.Contains("patronymic") ? a.Patronymic : null, Bio = includeFields.Count == 0 || includeFields.Contains("bio") ? a.Bio : null }).ToList(); var response = new PagedResult<Author_Response> { Items = items, Page = page, PageSize = pageSize, TotalCount = total }; return Results.Json(response); } [HttpGet("{id}")] [SwaggerOperation(Summary = "�������� ������ �� id", Description = "����� ������� ���� ����� �������� include (��������: ?include=name,surname. ������������ ����: name,surname, patronymic, bio.)", OperationId = "GetAuthorById")] [SwaggerResponse(200, "����� ������", typeof(Author_Response))] public IResult GetAuthor(int id, [FromQuery] string? include = null) { // ����������� ������ include � ������ var includeFields = include?.Split(',').Select(f => f.Trim().ToLower()).ToHashSet() ?? new HashSet<string>(); var author = _data.Authors.FirstOrDefault(a => a.Id == id); if (author == null) return Results.NotFound(); var response = new Author_Response { Id = author.Id, Name = includeFields.Count == 0 || includeFields.Contains("name") ? author.Name : null, Surname = includeFields.Count == 0 || includeFields.Contains("surname") ? author.Surname : null, Patronymic = includeFields.Count == 0 || includeFields.Contains("patronymic") ? author.Patronymic : null, Bio = includeFields.Count == 0 || includeFields.Contains("bio") ? author.Bio : null }; return Results.Json(response); } [HttpPost] [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("{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("{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."); } } }