feat: add points rule repository implementations
This commit is contained in:
parent
4830c9b2ae
commit
1485981d90
@ -12,6 +12,8 @@ public partial class ApplicationDbContext(DbContextOptions<ApplicationDbContext>
|
|||||||
public DbSet<Fengling.Member.Domain.Aggregates.Users.MemberTag> MemberTags => Set<Fengling.Member.Domain.Aggregates.Users.MemberTag>();
|
public DbSet<Fengling.Member.Domain.Aggregates.Users.MemberTag> MemberTags => Set<Fengling.Member.Domain.Aggregates.Users.MemberTag>();
|
||||||
public DbSet<Fengling.Member.Domain.Aggregates.Users.WechatAuthorization> WechatAuthorizations => Set<Fengling.Member.Domain.Aggregates.Users.WechatAuthorization>();
|
public DbSet<Fengling.Member.Domain.Aggregates.Users.WechatAuthorization> WechatAuthorizations => Set<Fengling.Member.Domain.Aggregates.Users.WechatAuthorization>();
|
||||||
public DbSet<Fengling.Member.Domain.Aggregates.PointsModel.PointsAccount> PointsAccounts => Set<Fengling.Member.Domain.Aggregates.PointsModel.PointsAccount>();
|
public DbSet<Fengling.Member.Domain.Aggregates.PointsModel.PointsAccount> PointsAccounts => Set<Fengling.Member.Domain.Aggregates.PointsModel.PointsAccount>();
|
||||||
|
public DbSet<Fengling.Member.Domain.Aggregates.PointsRuleModel.PointsRule> PointsRules => Set<Fengling.Member.Domain.Aggregates.PointsRuleModel.PointsRule>();
|
||||||
|
public DbSet<Fengling.Member.Domain.Aggregates.PointsRuleModel.PointsRuleCondition> PointsRuleConditions => Set<Fengling.Member.Domain.Aggregates.PointsRuleModel.PointsRuleCondition>();
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -0,0 +1,32 @@
|
|||||||
|
using Fengling.Member.Domain.Aggregates.PointsRuleModel;
|
||||||
|
using Fengling.Member.Domain.Repositories;
|
||||||
|
|
||||||
|
namespace Fengling.Member.Infrastructure.Repositories;
|
||||||
|
|
||||||
|
public class PointsRuleConditionRepository : IPointsRuleConditionRepository
|
||||||
|
{
|
||||||
|
private readonly ApplicationDbContext _context;
|
||||||
|
|
||||||
|
public PointsRuleConditionRepository(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<PointsRuleCondition>> GetByRuleIdAsync(PointsRuleId ruleId)
|
||||||
|
{
|
||||||
|
return await _context.PointsRuleConditions
|
||||||
|
.Where(x => x.RuleId == ruleId)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddAsync(PointsRuleCondition condition)
|
||||||
|
{
|
||||||
|
await _context.PointsRuleConditions.AddAsync(condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteByRuleIdAsync(PointsRuleId ruleId)
|
||||||
|
{
|
||||||
|
var conditions = await GetByRuleIdAsync(ruleId);
|
||||||
|
_context.PointsRuleConditions.RemoveRange(conditions);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,77 @@
|
|||||||
|
using Fengling.Member.Domain.Aggregates.PointsRuleModel;
|
||||||
|
using Fengling.Member.Domain.Aggregates.PointsRuleModel.Enums;
|
||||||
|
using Fengling.Member.Domain.Repositories;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Fengling.Member.Infrastructure.Repositories;
|
||||||
|
|
||||||
|
public class PointsRuleRepository : IPointsRuleRepository
|
||||||
|
{
|
||||||
|
private readonly ApplicationDbContext _context;
|
||||||
|
|
||||||
|
public PointsRuleRepository(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PointsRule?> GetByIdAsync(PointsRuleId id)
|
||||||
|
{
|
||||||
|
return await _context.PointsRules
|
||||||
|
.Include(x => x.Conditions)
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PointsRule?> GetByCodeAsync(string code)
|
||||||
|
{
|
||||||
|
return await _context.PointsRules
|
||||||
|
.Include(x => x.Conditions)
|
||||||
|
.FirstOrDefaultAsync(x => x.Code == code);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<PointsRule>> GetActiveRulesAsync()
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
return await _context.PointsRules
|
||||||
|
.Include(x => x.Conditions)
|
||||||
|
.Where(x => x.IsActive
|
||||||
|
&& x.EffectiveFrom <= now
|
||||||
|
&& (x.EffectiveTo == null || x.EffectiveTo > now))
|
||||||
|
.OrderByDescending(x => x.Priority)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<PointsRule>> GetActiveRulesByDimensionAsync(
|
||||||
|
DimensionType dimensionType,
|
||||||
|
string dimensionValue)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
return await _context.PointsRules
|
||||||
|
.Include(x => x.Conditions)
|
||||||
|
.Where(x => x.IsActive
|
||||||
|
&& x.EffectiveFrom <= now
|
||||||
|
&& (x.EffectiveTo == null || x.EffectiveTo > now)
|
||||||
|
&& x.Conditions.Any(c => c.DimensionType == dimensionType))
|
||||||
|
.OrderByDescending(x => x.Priority)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddAsync(PointsRule rule)
|
||||||
|
{
|
||||||
|
await _context.PointsRules.AddAsync(rule);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateAsync(PointsRule rule)
|
||||||
|
{
|
||||||
|
_context.PointsRules.Update(rule);
|
||||||
|
await Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(PointsRuleId id)
|
||||||
|
{
|
||||||
|
var rule = await GetByIdAsync(id);
|
||||||
|
if (rule != null)
|
||||||
|
{
|
||||||
|
_context.PointsRules.Remove(rule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user