/
Frozin
/
DIS-3
Обзор
Документация
Войти
/
Frozin
/
DIS-3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
API_PIR/Controllers/V1/NotesController.cs
128 строк
4 KB
Dobryk
Исправление опциональных полей
30 окт 2025, 12:58
30 окт 2025, 12:58
4d5f896
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Mvc; using API_PIR.Interfaces; using API_PIR.Models; namespace API_PIR.Controllers.V1; [ApiController] [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[controller]/[action]")] public class NotesController : ControllerBase { private readonly INoteService _noteService; public NotesController(INoteService noteService) { _noteService = noteService; } [HttpGet] public async Task<ActionResult<PaginatedList<Dictionary<string, string>>>> GetNotes( [FromQuery] PaginationParameters paginationParameters, [FromQuery] string? fields = null) { var notes = await _noteService.GetNotesPaginatedAsync( paginationParameters.PageNumber, paginationParameters.PageSize); if (string.IsNullOrEmpty(fields)) return Ok(notes); var selectedFields = fields.Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(f => f.Trim().ToLower()) .ToHashSet(); var response = new PaginatedList<Dictionary<string, string>>() { Items = notes.Items.Select(note => { var res = new Dictionary<string, string>(); res.Add("id", note.Id.ToString()); if (selectedFields.Contains("title")) { res.Add("title", note.Title.ToString()); } if (selectedFields.Contains("text")) { res.Add("text", note.Text.ToString()); } res.Add("createdAt", note.CreatedAt.ToString()); if (selectedFields.Contains("author")) { res.Add("author", note.Id.ToString()); } return res;}), PageNumber = notes.PageNumber, PageSize = notes.PageSize, TotalCount = notes.TotalCount }; return Ok(response); } [HttpGet("{id:guid}")] public async Task<ActionResult<Dictionary<string, string>>> GetNote(Guid id, [FromQuery] string? fields = null) { var note = await _noteService.GetNoteByIdAsync(id); if (note == null) { return NotFound(); } if (string.IsNullOrEmpty(fields)) return Ok(note); var selectedFields = fields.Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(f => f.Trim().ToLower()) .ToHashSet(); var response = new Dictionary<string, string>(); response.Add("id", note.Id.ToString()); if (selectedFields.Contains("title")) { response.Add("title", note.Title.ToString()); } if (selectedFields.Contains("text")) { response.Add("text", note.Text.ToString()); } response.Add("createdAt", note.CreatedAt.ToString()); if (selectedFields.Contains("author")) { response.Add("author", note.Id.ToString()); } return Ok(response); } [HttpPost] public async Task<ActionResult<NoteModel>> CreateNote([FromBody] NoteModel note) { var createdNote = await _noteService.CreateNoteAsync(note); return CreatedAtAction(nameof(GetNote), new { id = createdNote.Id }, createdNote); } [HttpPut("{id:guid}")] public async Task<ActionResult<NoteModel>> UpdateNote(Guid id, [FromBody] NoteModel note) { var updatedNote = await _noteService.UpdateNoteAsync(id, note); if (updatedNote == null) { return NotFound(); } return Ok(updatedNote); } [HttpDelete("{id:guid}")] public async Task<ActionResult> DeleteNote(Guid id) { var result = await _noteService.DeleteNoteAsync(id); if (!result) { return NotFound(); } return NoContent(); } }