From 40e97763803a824870467b1a98a0734ae23c0334 Mon Sep 17 00:00:00 2001 From: ilitirit Date: Fri, 5 May 2023 00:57:55 +0200 Subject: [PATCH] Add DeepDungeon stuff --- .../Controllers/ExpedienceController.cs | 42 ++++++------ .../ExpedienceContext.cs | 5 ++ .../ExpedienceRepository.cs | 64 ++++++++++--------- .../Models/DeepDungeonRecord.cs | 16 +++++ 4 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 Expedience.Infrastructure/Models/DeepDungeonRecord.cs diff --git a/Expedience.Api/Controllers/ExpedienceController.cs b/Expedience.Api/Controllers/ExpedienceController.cs index 2e7eacc..9e68c09 100644 --- a/Expedience.Api/Controllers/ExpedienceController.cs +++ b/Expedience.Api/Controllers/ExpedienceController.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore; namespace Expedience.Api.Controllers { - [Route("api/[controller]")] + [Route("api")] [ApiController] public class ExpedienceController : ControllerBase { @@ -50,25 +50,6 @@ 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 = await repository.GetDutyCompletionRecords(expac, cancellationToken); - return Ok(results); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error getting Duty Completion Records for {expac}: {errorMessage}", expac, ex.Message); - } - - return StatusCode(500); - } - [HttpGet("UserName/{worldId}/{userHash}")] public async Task GetUserName(int worldId, string userHash, CancellationToken cancellationToken) { @@ -90,5 +71,24 @@ namespace Expedience.Api.Controllers return StatusCode(500); } - } + + [HttpGet("DeepDungeonRecords")] + public async Task GetDeepDungeonRecords(CancellationToken cancellationToken) + { + try + { + using var scope = _serviceScopeFactory.CreateScope(); + var repository = scope.ServiceProvider.GetService(); + + var results = await repository.GetDeepDungeonRecords(cancellationToken); + return Ok(results); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error getting Deep Dungeon Records: {errorMessage}", ex.Message); + } + + return StatusCode(500); + } + } } diff --git a/Expedience.Infrastructure/ExpedienceContext.cs b/Expedience.Infrastructure/ExpedienceContext.cs index 4d76ec7..28e962b 100644 --- a/Expedience.Infrastructure/ExpedienceContext.cs +++ b/Expedience.Infrastructure/ExpedienceContext.cs @@ -18,6 +18,7 @@ namespace Expedience.Infrastructure public DbSet Users { get; set; } public DbSet DutyCompletionRecords { get; set; } + public DbSet DeepDungeonRecords { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -71,6 +72,10 @@ namespace Expedience.Infrastructure .HasNoKey() .ToTable(t => t.ExcludeFromMigrations()); + modelBuilder.Entity() + .HasNoKey() + .ToTable(t => t.ExcludeFromMigrations()); + base.OnModelCreating(modelBuilder); } } diff --git a/Expedience.Infrastructure/ExpedienceRepository.cs b/Expedience.Infrastructure/ExpedienceRepository.cs index 6f5b6bd..45bf645 100644 --- a/Expedience.Infrastructure/ExpedienceRepository.cs +++ b/Expedience.Infrastructure/ExpedienceRepository.cs @@ -1,42 +1,46 @@ using System; - using Enyim.Caching; using Expedience.Infrastructure.Models; using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json; -namespace Expedience.Infrastructure +namespace Expedience.Infrastructure; + +public interface IExpedienceRepository +{ + ValueTask> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken); + ValueTask> GetDeepDungeonRecords(CancellationToken cancellationToken); +} + +public class ExpedienceRepository : IExpedienceRepository { - public interface IExpedienceRepository : IDisposable + private readonly ExpedienceContext _dbContext; + private readonly IMemcachedClient _memcachedClient; + + public ExpedienceRepository(ExpedienceContext dbContext, IMemcachedClient memcachedClient) { - Task> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken); + _dbContext = dbContext; + _memcachedClient = memcachedClient; } - public class ExpedienceRepository : IExpedienceRepository + public async ValueTask> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken) { - 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}"; - var cacheSeconds = 600; - var records = await _memcachedClient.GetValueOrCreateAsync(cacheKey, cacheSeconds, - async () => await _dbContext.DutyCompletionRecords.FromSqlInterpolated($"SELECT * FROM public.get_dutycompletionrecords({expac})") - .ToListAsync()); - - return records; - } - - public void Dispose() - { - //_memcachedClient.Dispose(); - } + var cacheKey = $"xpd-dcr-{expac}"; + var cacheSeconds = 600; + var records = await _memcachedClient.GetValueOrCreateAsync(cacheKey, cacheSeconds, + async () => await _dbContext.DutyCompletionRecords.FromSqlInterpolated($"SELECT * FROM public.get_dutycompletionrecords({expac})") + .ToListAsync(cancellationToken)); + + return records; + } + + public async ValueTask> GetDeepDungeonRecords(CancellationToken cancellationToken) + { + var cacheKey = $"xpd-dd"; + var cacheSeconds = 600; + var records = await _memcachedClient.GetValueOrCreateAsync(cacheKey, cacheSeconds, + async () => await _dbContext.DeepDungeonRecords.FromSqlInterpolated($"SELECT * FROM public.get_deepdungeonresults()") + .ToListAsync(cancellationToken)); + + return records; } } diff --git a/Expedience.Infrastructure/Models/DeepDungeonRecord.cs b/Expedience.Infrastructure/Models/DeepDungeonRecord.cs new file mode 100644 index 0000000..9975f3b --- /dev/null +++ b/Expedience.Infrastructure/Models/DeepDungeonRecord.cs @@ -0,0 +1,16 @@ +using System; + +namespace Expedience.Infrastructure.Models; + +public class DeepDungeonRecord +{ + public int TerritoryId { get; set; } + public string DeepDungeon { get; set; } + public string Floor { get; set; } + public int Level { get; set; } + public int SortKey { get; set; } + public TimeSpan? MinParty { get; set; } + public TimeSpan? AvgParty { get; set; } + public TimeSpan? MinSolo { get; set; } + public TimeSpan? AvgSolo { get; set; } +}