feat(dal): WeekendDayService - метод проверки на рабочий день

This commit is contained in:
Mikhail Trubnikov
2024-09-16 17:01:04 +10:00
parent 4a47e194f8
commit 251e3a20b0
8 changed files with 103 additions and 3 deletions

View File

@@ -1,9 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.CacheServices;
using PARR.DAL.Context;
using PARR.DAL.Models;
using PARR.DAL.Services.Abstracts;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Settings;
namespace PARR.DAL.Services.Implementations
{
@@ -11,15 +13,24 @@ namespace PARR.DAL.Services.Implementations
{
private readonly DataContext dataContext;
private readonly ILogger<WeekendDayService> logger;
private readonly IRedisCacheService redisCacheService;
private readonly WeekendCacheSettings weekendCacheSettings;
protected override DbSet<WeekendDay> EntitySet => dataContext.WeekendDays;
protected override DataContext EntitiContext => dataContext;
public WeekendDayService(DataContext dataContext, ILogger<WeekendDayService> logger) : base(logger)
public WeekendDayService(
DataContext dataContext,
ILogger<WeekendDayService> logger,
IRedisCacheService redisCacheService,
WeekendCacheSettings weekendCacheSettings
) : base(logger)
{
this.dataContext = dataContext;
this.logger = logger;
this.redisCacheService = redisCacheService;
this.weekendCacheSettings = weekendCacheSettings;
}
@@ -28,5 +39,45 @@ namespace PARR.DAL.Services.Implementations
return EntitySet.Where(t => t.Date >= start && t.Date <= end).Select(t => t.Date);
}
public async Task<bool> IsWorkDayAsync(DateOnly date, bool useCache = false)
{
if (useCache)
{
// смотрим, есть ли в кэше рабочий день
var workdayInCache = await redisCacheService.GetCachedDataAsync<DateOnly?>(GetWorkDayKey(date));
if (workdayInCache != null)
return true;
// смотрим, есть ли в кэше выходной день
var weekendInCache = await redisCacheService.GetCachedDataAsync<DateOnly?>(GetWeekendKey(date));
if (weekendInCache != null)
return false;
}
var weekendInDb = await EntitySet.FirstOrDefaultAsync(t => t.Date == date);
var isWorkday = weekendInDb == null;
//сохраняем всегда в КЭШ значение выходного и рабочего дня
var cacheKey = isWorkday ? GetWorkDayKey(date) : GetWeekendKey(date);
//TODO:!!! Тут настройки вынести в DB settings
await redisCacheService.SetCachedDataAsync(cacheKey, date, weekendCacheSettings.WeekendTtl);
return isWorkday;
}
private string GetWeekendKey(DateOnly day)
{
return $"weekend_{day.ToString("yyyy-MM-dd")}";
}
private string GetWorkDayKey(DateOnly day)
{
return $"workday_{day.ToString("yyyy-MM-dd")}";
}
}
}