From e6a90d31b7584391c24d41a4d3bf3adda8f7a147 Mon Sep 17 00:00:00 2001 From: ilitirit Date: Mon, 24 Apr 2023 17:19:57 +0200 Subject: [PATCH] Add DutyCompletionRecords API --- .../Controllers/ExpedienceController.cs | 23 +++++++-- Expedience.Api/Program.cs | 1 + .../ExpedienceContext.cs | 5 ++ .../ExpedienceRepository.cs | 48 +++++++++++++++++++ .../Models/DutyCompletionRecord.cs | 18 +++++++ 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 Expedience.Infrastructure/ExpedienceRepository.cs create mode 100644 Expedience.Infrastructure/Models/DutyCompletionRecord.cs diff --git a/Expedience.Api/Controllers/ExpedienceController.cs b/Expedience.Api/Controllers/ExpedienceController.cs index 067f574..dcd3837 100644 --- a/Expedience.Api/Controllers/ExpedienceController.cs +++ b/Expedience.Api/Controllers/ExpedienceController.cs @@ -2,7 +2,6 @@ using System.Text.Json; using Expedience.Api.Encryption; using Expedience.Infrastructure; -using Expedience.Infrastructure.Concurrency; using MassTransit; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -16,19 +15,16 @@ namespace Expedience.Api.Controllers private readonly ILogger _logger; private readonly IPublishEndpoint _publisher; private readonly IDecryptor _decrypytor; - private readonly IDistributedLock _distributedLock; private readonly IServiceScopeFactory _serviceScopeFactory; public ExpedienceController(ILogger logger, IPublishEndpoint publisher, IDecryptor decrypytor, - IDistributedLock distributedLock, IServiceScopeFactory serviceScopeFactory) { _logger = logger; _publisher = publisher; _decrypytor = decrypytor; - _distributedLock = distributedLock; _serviceScopeFactory = serviceScopeFactory; } @@ -54,6 +50,25 @@ namespace Expedience.Api.Controllers return Ok(); } + [HttpGet("DutyCompletionRecords/{expac}")] + public async Task GetDutyCompletionRecords(string expac, CancellationToken cancellationToken) + { + try + { + using var scope = _serviceScopeFactory.CreateScope(); + using var repository = scope.ServiceProvider.GetService(); + + var results = repository.GetDutyCompletionRecords(expac, CancellationToken.None); + return Ok(results); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error getting Duty Completion Records for {expac}: {errorMessage}", expac, ex.Message); + } + + return Ok(); + } + [HttpGet("UserName/{worldId}/{userHash}")] public async Task GetUserName(int worldId, string userHash, CancellationToken cancellationToken) { diff --git a/Expedience.Api/Program.cs b/Expedience.Api/Program.cs index 8001867..f2991ac 100644 --- a/Expedience.Api/Program.cs +++ b/Expedience.Api/Program.cs @@ -35,6 +35,7 @@ builder.Services.AddMassTransit(opt => }); builder.Services.AddEnyimMemcached(); +builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/Expedience.Infrastructure/ExpedienceContext.cs b/Expedience.Infrastructure/ExpedienceContext.cs index 2b0f46b..4d76ec7 100644 --- a/Expedience.Infrastructure/ExpedienceContext.cs +++ b/Expedience.Infrastructure/ExpedienceContext.cs @@ -17,6 +17,7 @@ namespace Expedience.Infrastructure public DbSet Territories { get; set; } public DbSet Users { get; set; } + public DbSet DutyCompletionRecords { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -66,6 +67,10 @@ namespace Expedience.Infrastructure cfg.HasKey(e => new { e.TerritoryId }); }); + modelBuilder.Entity() + .HasNoKey() + .ToTable(t => t.ExcludeFromMigrations()); + base.OnModelCreating(modelBuilder); } } diff --git a/Expedience.Infrastructure/ExpedienceRepository.cs b/Expedience.Infrastructure/ExpedienceRepository.cs new file mode 100644 index 0000000..d14f56a --- /dev/null +++ b/Expedience.Infrastructure/ExpedienceRepository.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using Enyim.Caching; +using Expedience.Infrastructure.Models; +using Microsoft.EntityFrameworkCore; + +namespace Expedience.Infrastructure +{ + public interface IRepository : IDisposable + { + Task> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken); + } + + public class ExpedienceRepository : IRepository + { + private readonly ExpedienceContext _dbContext; + private readonly IMemcachedClient _memcachedClient; + + public ExpedienceRepository(ExpedienceContext dbContext, IMemcachedClient memcachedClient) + { + _dbContext = dbContext; + _memcachedClient = memcachedClient; + } + + public async Task> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken) + { + var cacheKey = $"xpd-dcr-{expac}"; + if (_memcachedClient.TryGet>(cacheKey, out var cachedValue)) + { + return cachedValue; + } + else + { + var records = await _dbContext.DutyCompletionRecords.FromSqlInterpolated($"CALL get_dutycompletionrecords {expac}") + .ToListAsync(); + + await _memcachedClient.AddAsync(cacheKey, records, TimeSpan.FromMinutes(10)); + + return records; + } + } + + public void Dispose() + { + _dbContext.Dispose(); + } + } +} diff --git a/Expedience.Infrastructure/Models/DutyCompletionRecord.cs b/Expedience.Infrastructure/Models/DutyCompletionRecord.cs new file mode 100644 index 0000000..f90b25a --- /dev/null +++ b/Expedience.Infrastructure/Models/DutyCompletionRecord.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Expedience.Infrastructure.Models +{ + public class DutyCompletionRecord + { + public int TerritoryId { get; set; } + public string ContentName { get; set; } + public string ContentType { get; set; } + public int Level { get; set; } + public TimeSpan? MinTime { get; set; } + public TimeSpan? AverageTime { get; set; } + } +}