| | | 1 | | using System.Text.Json; |
| | | 2 | | using GistBackend.Handlers.ChromaDbHandler; |
| | | 3 | | using GistBackend.Handlers.MariaDbHandler; |
| | | 4 | | using GistBackend.Types; |
| | | 5 | | using GistBackend.Utils; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using static GistBackend.Utils.LogEvents; |
| | | 10 | | |
| | | 11 | | namespace GistBackend.Controllers; |
| | | 12 | | |
| | | 13 | | public static class RoutingConstants |
| | | 14 | | { |
| | | 15 | | public const string GistsRoute = "/api/v1/gists"; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | [ApiController] |
| | | 19 | | [Route(RoutingConstants.GistsRoute)] |
| | 10 | 20 | | public class GistsController( |
| | 10 | 21 | | [FromKeyedServices(StartUp.GistsControllerMariaDbHandlerOptionsName)] IMariaDbHandler mariaDbHandler, |
| | 10 | 22 | | IChromaDbHandler chromaDbHandler, |
| | 10 | 23 | | ILogger<GistsController>? logger) : ControllerBase |
| | | 24 | | { |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> GetGistsQueryAsync( |
| | | 27 | | [FromQuery] int take = 20, |
| | | 28 | | [FromQuery] int? lastGist = null, |
| | | 29 | | [FromQuery] string? tags = null, |
| | | 30 | | [FromQuery] string? q = null, |
| | | 31 | | [FromQuery] string? disabledFeeds = null, |
| | | 32 | | [FromQuery] LanguageMode? languageMode = null, |
| | | 33 | | [FromQuery] bool? includeSponsoredContent = null, |
| | | 34 | | CancellationToken ct = default) |
| | | 35 | | { |
| | | 36 | | try |
| | | 37 | | { |
| | 5 | 38 | | var gists = await mariaDbHandler.GetPreviousConstructedGistsAsync(take, lastGist, ParseTags(tags), q, |
| | 5 | 39 | | ParseDisabledFeeds(disabledFeeds), languageMode, includeSponsoredContent, ct); |
| | 5 | 40 | | return Ok(gists); |
| | | 41 | | } |
| | 0 | 42 | | catch (Exception e) |
| | | 43 | | { |
| | | 44 | | const string message = "Could not get gists from the database"; |
| | 0 | 45 | | logger?.LogError(ErrorInHttpRequest, e, message); |
| | 0 | 46 | | return Problem(message); |
| | | 47 | | } |
| | 5 | 48 | | } |
| | | 49 | | |
| | | 50 | | [HttpGet("health")] |
| | | 51 | | public Task<IActionResult> GetHealthAsync(CancellationToken ct = default) => |
| | 2 | 52 | | GetGistsQueryAsync(1, null, null, null, null, null, null, ct); |
| | | 53 | | |
| | | 54 | | [HttpGet("{id:int}")] |
| | | 55 | | public async Task<IActionResult> GetGistWithFeedByIdAsync( |
| | | 56 | | int id, |
| | | 57 | | [FromQuery] LanguageMode? languageMode = null, |
| | | 58 | | CancellationToken ct = default) |
| | | 59 | | { |
| | | 60 | | try |
| | | 61 | | { |
| | 0 | 62 | | var gist = await mariaDbHandler.GetConstructedGistByIdAsync(id, languageMode, ct); |
| | 0 | 63 | | if (gist is null) return NotFound(); |
| | 0 | 64 | | return Ok(gist); |
| | | 65 | | } |
| | 0 | 66 | | catch (Exception e) |
| | | 67 | | { |
| | | 68 | | const string message = "Could not get gist with feed by ID from the database"; |
| | 0 | 69 | | logger?.LogError(ErrorInHttpRequest, e, message); |
| | 0 | 70 | | return Problem(message); |
| | | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | [HttpGet("{id:int}/similar")] |
| | | 75 | | public async Task<IActionResult> GetSimilarGistsWithFeedAsync( |
| | | 76 | | int id, |
| | | 77 | | [FromQuery] string? disabledFeeds = null, |
| | | 78 | | [FromQuery] LanguageMode? languageMode = null, |
| | | 79 | | CancellationToken ct = default) |
| | | 80 | | { |
| | | 81 | | try |
| | | 82 | | { |
| | 3 | 83 | | var gistWithFeed = await mariaDbHandler.GetConstructedGistByIdAsync(id, languageMode, ct); |
| | 4 | 84 | | if (gistWithFeed is null) return NotFound(); |
| | 2 | 85 | | var similarityResults = |
| | 2 | 86 | | await chromaDbHandler.GetReferenceAndScoreOfSimilarEntriesAsync(gistWithFeed.Reference, 5, |
| | 2 | 87 | | ParseDisabledFeeds(disabledFeeds), ct); |
| | 2 | 88 | | var gistsWithFeed = new List<SimilarGistWithFeed>(); |
| | 10 | 89 | | foreach (var similarityResult in similarityResults) |
| | | 90 | | { |
| | 3 | 91 | | gistsWithFeed.Add(await GetSimilarGistWithFeedFromDatabaseAsync(similarityResult, languageMode, ct)); |
| | | 92 | | } |
| | 2 | 93 | | return Ok(gistsWithFeed); |
| | | 94 | | } |
| | 0 | 95 | | catch (Exception e) |
| | | 96 | | { |
| | | 97 | | const string message = "Could not get similar gists from the database"; |
| | 0 | 98 | | logger?.LogError(ErrorInHttpRequest, e, message); |
| | 0 | 99 | | return Problem(message); |
| | | 100 | | } |
| | 3 | 101 | | } |
| | | 102 | | |
| | | 103 | | private async Task<SimilarGistWithFeed> GetSimilarGistWithFeedFromDatabaseAsync(SimilarDocument similarDocument, |
| | | 104 | | LanguageMode? languageMode, CancellationToken ct) |
| | | 105 | | { |
| | 3 | 106 | | var gistWithFeed = await mariaDbHandler.GetConstructedGistByReference(similarDocument.Reference, languageMode, c |
| | 3 | 107 | | if (gistWithFeed is null) |
| | | 108 | | { |
| | 0 | 109 | | throw new KeyNotFoundException( |
| | 0 | 110 | | $"Similar gist with reference {similarDocument.Reference} not found in database"); |
| | | 111 | | } |
| | 3 | 112 | | return new SimilarGistWithFeed(gistWithFeed, similarDocument.Similarity); |
| | 3 | 113 | | } |
| | | 114 | | |
| | | 115 | | [HttpGet("feeds")] |
| | | 116 | | public async Task<IActionResult> GetAllFeedsAsync(CancellationToken ct) |
| | | 117 | | { |
| | | 118 | | try |
| | | 119 | | { |
| | 2 | 120 | | var feeds = await mariaDbHandler.GetAllFeedInfosAsync(ct); |
| | 2 | 121 | | return Ok(feeds); |
| | | 122 | | } |
| | 0 | 123 | | catch (Exception e) |
| | | 124 | | { |
| | | 125 | | const string message = "Could not get all feeds from the database"; |
| | 0 | 126 | | logger?.LogError(ErrorInHttpRequest, e, message); |
| | 0 | 127 | | return Problem(message); |
| | | 128 | | } |
| | 2 | 129 | | } |
| | | 130 | | |
| | | 131 | | [HttpGet("recap/daily")] |
| | | 132 | | public Task<IActionResult> GetDailyRecapAsync([FromQuery] LanguageMode languageMode, CancellationToken ct) => |
| | 0 | 133 | | GetRecapAsync(RecapType.Daily, languageMode, ct); |
| | | 134 | | |
| | | 135 | | [HttpGet("recap/weekly")] |
| | | 136 | | public Task<IActionResult> GetWeeklyRecapAsync([FromQuery] LanguageMode languageMode, CancellationToken ct) => |
| | 0 | 137 | | GetRecapAsync(RecapType.Weekly, languageMode, ct); |
| | | 138 | | |
| | | 139 | | private async Task<IActionResult> GetRecapAsync(RecapType type, LanguageMode languageMode, CancellationToken ct) |
| | | 140 | | { |
| | | 141 | | try |
| | | 142 | | { |
| | 0 | 143 | | if (languageMode == LanguageMode.Original) |
| | | 144 | | { |
| | 0 | 145 | | throw new ArgumentException("Language mode 'Original' is not supported for recaps"); |
| | | 146 | | } |
| | 0 | 147 | | var serializedRecap = await mariaDbHandler.GetLatestRecapAsync(type, ct); |
| | 0 | 148 | | if (serializedRecap is null) return NotFound("No recap found in the database"); |
| | 0 | 149 | | var serializedRecapSections = |
| | 0 | 150 | | languageMode == LanguageMode.En ? serializedRecap.RecapEn : serializedRecap.RecapDe; |
| | 0 | 151 | | var recapSections = |
| | 0 | 152 | | JsonSerializer.Deserialize<IEnumerable<RecapSection>>(serializedRecapSections, |
| | 0 | 153 | | SerializerDefaults.JsonOptions); |
| | 0 | 154 | | if (recapSections is null) |
| | | 155 | | { |
| | 0 | 156 | | throw new JsonException($"Could not deserialize recap from this JSON: {serializedRecapSections}"); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | recapSections = recapSections.ToList(); |
| | 0 | 160 | | var gistTitlesById = new Dictionary<int, string>(); |
| | 0 | 161 | | var gistIds = recapSections.SelectMany(s => s.Related).Distinct().ToList(); |
| | 0 | 162 | | foreach (var gistId in gistIds) |
| | | 163 | | { |
| | 0 | 164 | | var gistWithFeed = await mariaDbHandler.GetConstructedGistByIdAsync(gistId, LanguageMode.Original, ct); |
| | 0 | 165 | | if (gistWithFeed is not null) gistTitlesById[gistId] = gistWithFeed.Title; |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | var deserializedRecapSections = recapSections.Select(s => new DeserializedRecapSection(s.Heading, |
| | 0 | 169 | | s.Recap, |
| | 0 | 170 | | s.Related.Where(r => gistTitlesById.ContainsKey(r)) |
| | 0 | 171 | | .Select(r => new RelatedGistInfo(r, gistTitlesById[r])))); |
| | 0 | 172 | | var deserializedRecap = new DeserializedRecap(serializedRecap.Created, deserializedRecapSections, |
| | 0 | 173 | | serializedRecap.Id ?? -1); |
| | 0 | 174 | | return Ok(deserializedRecap); |
| | | 175 | | } |
| | 0 | 176 | | catch (Exception e) |
| | | 177 | | { |
| | | 178 | | const string message = "Could not get recap from the database"; |
| | 0 | 179 | | logger?.LogError(CouldNotGetRecap, e, message); |
| | 0 | 180 | | return Problem(message); |
| | | 181 | | } |
| | 0 | 182 | | } |
| | | 183 | | |
| | 5 | 184 | | private static List<string> ParseTags(string? tags) => string.IsNullOrWhiteSpace(tags) |
| | 5 | 185 | | ? [] |
| | 5 | 186 | | : tags |
| | 5 | 187 | | .Split(";;", StringSplitOptions.RemoveEmptyEntries) |
| | 2 | 188 | | .Where(id => !string.IsNullOrWhiteSpace(id)) |
| | 7 | 189 | | .Select(tag => tag.Trim()).ToList(); |
| | | 190 | | |
| | 7 | 191 | | private static List<int> ParseDisabledFeeds(string? disabledFeeds) => string.IsNullOrWhiteSpace(disabledFeeds) |
| | 7 | 192 | | ? [] |
| | 7 | 193 | | : disabledFeeds |
| | 7 | 194 | | .Split(',', StringSplitOptions.RemoveEmptyEntries) |
| | 8 | 195 | | .Where(id => !string.IsNullOrWhiteSpace(id)) |
| | 8 | 196 | | .Select(id => id.Trim()) |
| | 7 | 197 | | .Select(int.Parse) |
| | 7 | 198 | | .ToList(); |
| | | 199 | | } |