From 8d432a5e2628ad19a1e34e9e0e9d26fc6d8865ae Mon Sep 17 00:00:00 2001 From: pancakes Date: Sat, 22 Jun 2024 13:35:27 +1000 Subject: [PATCH] [backend/api] Add PATCH admin/emoji/{id} and UpdateEmojiRequest --- .../Controllers/AdminController.cs | 38 +++++++++++++++++++ .../Schemas/UpdateEmojiRequest.cs | 9 +++++ 2 files changed, 47 insertions(+) create mode 100644 Iceshrimp.Shared/Schemas/UpdateEmojiRequest.cs diff --git a/Iceshrimp.Backend/Controllers/AdminController.cs b/Iceshrimp.Backend/Controllers/AdminController.cs index 7d8da71a..a4bd1848 100644 --- a/Iceshrimp.Backend/Controllers/AdminController.cs +++ b/Iceshrimp.Backend/Controllers/AdminController.cs @@ -278,4 +278,42 @@ public class AdminController( return Ok(res); } + + [HttpPatch("emoji/{id}")] + [Consumes(MediaTypeNames.Application.Json)] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EmojiResponse))] + [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] + public async Task UpdateEmoji(string id, UpdateEmojiRequest request, [FromServices] IOptions config) + { + var emoji = await db.Emojis.FirstOrDefaultAsync(p => p.Id == id); + + if (emoji == null) return NotFound(); + + emoji.UpdatedAt = DateTime.UtcNow; + emoji.Name = request.Name ?? emoji.Name; + emoji.Aliases = request.Aliases ?? emoji.Aliases; + emoji.Uri = emoji.GetPublicUri(config.Value); + emoji.License = request.License ?? emoji.License; + + if (request.Category != null) + { + // Updating Category to an empty string will reset it to null + emoji.Category = string.IsNullOrWhiteSpace(request.Category) ? null : request.Category; + } + + await db.SaveChangesAsync(); + + var res = new EmojiResponse + { + Id = emoji.Id, + Name = emoji.Name, + Uri = emoji.Uri, + Aliases = emoji.Aliases, + Category = emoji.Category, + PublicUrl = emoji.PublicUrl, + License = emoji.License + }; + + return Ok(res); + } } diff --git a/Iceshrimp.Shared/Schemas/UpdateEmojiRequest.cs b/Iceshrimp.Shared/Schemas/UpdateEmojiRequest.cs new file mode 100644 index 00000000..0eb6510f --- /dev/null +++ b/Iceshrimp.Shared/Schemas/UpdateEmojiRequest.cs @@ -0,0 +1,9 @@ +namespace Iceshrimp.Shared.Schemas; + +public class UpdateEmojiRequest +{ + public string? Name { get; set; } + public List? Aliases { get; set; } + public string? Category { get; set; } + public string? License { get; set; } +} \ No newline at end of file