139 lines
6.0 KiB
C#
139 lines
6.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using LCU.API.Internal.LoanRateLib.Configurations;
|
|
using LCU.API.Internal.LoanRateLib.Extensions.Queryables;
|
|
using LCU.API.Internal.LoanRateLib.Persistence;
|
|
using LCU.Library.Internal.LcuCaching.Services;
|
|
using LCU.Library.Internal.LcuEvaluationModels.Enums;
|
|
|
|
namespace LCU.API.Internal.LoanRateLib.Repositories;
|
|
|
|
public class RateRepository : BaseRepository, IRateRepository
|
|
{
|
|
private readonly RateCacheConfiguration _rateCacheConfiguration;
|
|
private readonly ICacheService _cacheService;
|
|
|
|
public RateRepository(AppDbContext context,
|
|
RateCacheConfiguration rateCacheConfiguration,
|
|
ICacheService cacheService) : base(context)
|
|
{
|
|
_rateCacheConfiguration = rateCacheConfiguration;
|
|
_cacheService = cacheService;
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindAsync()
|
|
{
|
|
return await _context.Rates.IncludeAll().ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindWithRateEvaluationsAsync()
|
|
{
|
|
return await _context.Rates.IncludeAllRatesEvaluations().ToListAsync();
|
|
}
|
|
|
|
public async Task<Rate?> FindByIdAsync(int id)
|
|
{
|
|
return await _context.Rates.IncludeAll().FirstOrDefaultAsync(rate => rate.RateId == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindByClosingRateUploadInfoIdAsync(int id)
|
|
{
|
|
return await _context.Rates.Where(rate => rate.ClosingRateUploadInfoId == id)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindByDateAsync(DateTime date)
|
|
{
|
|
return await _context.Rates.Where(rate => date >= rate.RateStartDate &&
|
|
(rate.RateEndDate == null || date < rate.RateEndDate))
|
|
.IncludeAll()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindRateHistoryAsync(Rate rate)
|
|
{
|
|
return await _context.Rates.Where(x => x.Row == rate.Row
|
|
&& x.Column == rate.Column
|
|
&& x.RateSheetId == rate.RateSheetId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>?> FindRangeByIdAsync(List<int> ids)
|
|
{
|
|
return await _context.Rates
|
|
.Where(rate => ids.Contains(rate.RateId))
|
|
.IncludeAll()
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Will get Rates that:
|
|
/// - Are between the RateStartDate-RateEndDate(if not null).
|
|
/// - Match the evaluation type
|
|
/// - Match All the EvaluationIds
|
|
/// - Contains ALL the evaluationids and the count of RateEvaluation.EvaluationDetailIds match the count of evaluationIds
|
|
/// </summary>
|
|
/// <param name="date"></param>
|
|
/// <param name="evaluationIds"></param>
|
|
/// <param name="evaluationType"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<Rate>?> FindByDateEvaluationIdsAndEvaluationTypeAsync(DateTime date,
|
|
List<int> evaluationIds,
|
|
EvaluationTypes evaluationType)
|
|
{
|
|
return await _context.Rates
|
|
.Where(
|
|
rate => date >= rate.RateStartDate &&
|
|
(!rate.RateEndDate.HasValue || date < rate.RateEndDate) &&
|
|
(rate.RateEvaluations.Any() &&
|
|
rate.RateEvaluations
|
|
.Where(rateEvaluation =>
|
|
(date >= rateEvaluation.EvaluationDetail.StartDate &&
|
|
(!rateEvaluation.EvaluationDetail.EndDate.HasValue || date < rateEvaluation.EvaluationDetail.EndDate)) &&
|
|
rateEvaluation.EvaluationDetail.EvaluationType == evaluationType &&
|
|
rateEvaluation.EvaluationDetail.Active == true)
|
|
.All(rateEvaluation => evaluationIds.Contains(rateEvaluation.EvaluationDetailId)) &&
|
|
rate.RateEvaluations
|
|
.Where(rateEvaluation =>
|
|
(date >= rateEvaluation.EvaluationDetail.StartDate &&
|
|
(!rateEvaluation.EvaluationDetail.EndDate.HasValue || date < rateEvaluation.EvaluationDetail.EndDate)) &&
|
|
rateEvaluation.EvaluationDetail.EvaluationType == evaluationType &&
|
|
rateEvaluation.EvaluationDetail.Active == true &&
|
|
evaluationIds.Contains(rateEvaluation.EvaluationDetailId))
|
|
.Select(rateEvaluation => rateEvaluation.EvaluationDetailId).Count() == evaluationIds.Count))
|
|
.IncludeAllRatesEvaluations()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Rate?> AddAsync(Rate rate)
|
|
{
|
|
return (await _context.Rates.AddAsync(rate)).Entity;
|
|
}
|
|
|
|
public async Task AddRangeAsync(IEnumerable<Rate> rates)
|
|
{
|
|
await _context.Rates.AddRangeAsync(rates);
|
|
}
|
|
|
|
public void UpdateRange(IEnumerable<Rate> rates)
|
|
{
|
|
_context.Rates.UpdateRange(rates);
|
|
}
|
|
|
|
public void Remove(Rate rate)
|
|
{
|
|
_context.Rates.Remove(rate);
|
|
}
|
|
|
|
public void RemoveRange(IEnumerable<Rate> rates)
|
|
{
|
|
_context.Rates.RemoveRange(rates);
|
|
}
|
|
|
|
public async Task<IEnumerable<Rate>> FindCacheAsync()
|
|
=> await _cacheService.FindCacheAsync(FindAsync,
|
|
_rateCacheConfiguration.CacheName,
|
|
_rateCacheConfiguration.ExpirationMinutes);
|
|
|
|
public void ClearCache()
|
|
=> _cacheService.ClearCache(_rateCacheConfiguration.CacheName);
|
|
} |