Add scheme APIs and application services
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Distribution.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Distribution.Features.Scheme;
|
||||
|
||||
public class SchemeRepository(AppDBContext context) : ISchemeRepository
|
||||
{
|
||||
public async Task<Models.Scheme?> GetByIdAsync(Guid id) =>
|
||||
await context.Schemes
|
||||
.Include(s => s.Region)
|
||||
.Include(s => s.TriggerItem)
|
||||
.Include(s => s.FreeItem)
|
||||
.Include(s => s.BundleComponents).ThenInclude(c => c.Item)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
|
||||
public async Task<List<Models.Scheme>> GetAllAsync(Guid? regionId = null)
|
||||
{
|
||||
var query = context.Schemes
|
||||
.Include(s => s.Region)
|
||||
.Include(s => s.TriggerItem)
|
||||
.Include(s => s.FreeItem)
|
||||
.Include(s => s.BundleComponents).ThenInclude(c => c.Item)
|
||||
.Where(s => s.IsActive);
|
||||
|
||||
if (regionId.HasValue) query = query.Where(s => s.FK_RegionId == regionId);
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task AddAsync(Models.Scheme entity) =>
|
||||
await context.Schemes.AddAsync(entity);
|
||||
|
||||
public void Update(Models.Scheme entity) =>
|
||||
context.Schemes.Update(entity);
|
||||
|
||||
public void Remove(Models.Scheme entity)
|
||||
{
|
||||
entity.IsActive = false;
|
||||
context.Schemes.Update(entity);
|
||||
}
|
||||
|
||||
public async Task<List<Models.SchemeRedemption>> GetRedemptionsAsync(Guid schemeId) =>
|
||||
await context.SchemeRedemptions
|
||||
.Include(r => r.FreeItem)
|
||||
.Include(r => r.Order)
|
||||
.Where(r => r.FK_SchemeId == schemeId)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task AddRedemptionAsync(Models.SchemeRedemption entity) =>
|
||||
await context.SchemeRedemptions.AddAsync(entity);
|
||||
|
||||
public async Task<bool> HasRedemptionAsync(Guid schemeId, Guid orderId) =>
|
||||
await context.SchemeRedemptions.AnyAsync(r => r.FK_SchemeId == schemeId && r.FK_OrderId == orderId);
|
||||
|
||||
public async Task<Models.SchemeBundleComponent?> GetBundleComponentAsync(Guid schemeId, Guid itemId) =>
|
||||
await context.SchemeBundleComponents
|
||||
.FirstOrDefaultAsync(c => c.FK_SchemeId == schemeId && c.FK_ItemId == itemId);
|
||||
|
||||
public async Task AddBundleComponentAsync(Models.SchemeBundleComponent entity) =>
|
||||
await context.SchemeBundleComponents.AddAsync(entity);
|
||||
|
||||
public void UpdateBundleComponent(Models.SchemeBundleComponent entity) =>
|
||||
context.SchemeBundleComponents.Update(entity);
|
||||
|
||||
public void RemoveBundleComponent(Models.SchemeBundleComponent entity) =>
|
||||
context.SchemeBundleComponents.Remove(entity);
|
||||
}
|
||||
Reference in New Issue
Block a user