FInished auto crud. Switched to auto crud for coupons. Added gift code manage

This commit is contained in:
Marcel Baumgartner 2023-10-25 22:22:32 +02:00
parent b0d9837256
commit 7a3d61c659
9 changed files with 301 additions and 70 deletions

View file

@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms.Store;
namespace Moonlight.App.Models.Forms.Admin.Store;
public class AddCouponForm
{

View file

@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms.Admin.Store;
public class AddGiftCodeForm
{
[MinLength(5, ErrorMessage = "The code needs to be longer than 4")]
[MaxLength(15, ErrorMessage = "The code should not be longer than 15 characters")]
public string Code { get; set; } = "";
[Range(0, int.MaxValue, ErrorMessage = "The value needs to be equals or greater than 0")]
public double Value { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "The amount needs to be equals or greater than 0")]
public int Amount { get; set; }
}

View file

@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms.Admin.Store;
public class EditCouponForm
{
[MinLength(5, ErrorMessage = "The code needs to be longer than 4")]
[MaxLength(15, ErrorMessage = "The code should not be longer than 15 characters")]
public string Code { get; set; } = "";
[Range(1, 99, ErrorMessage = "The percent needs to be between 1 and 99")]
public int Percent { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "The amount needs to be equals or greater than 0")]
public int Amount { get; set; }
}

View file

@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.App.Models.Forms.Admin.Store;
public class EditGiftCodeForm
{
[MinLength(5, ErrorMessage = "The code needs to be longer than 4")]
[MaxLength(15, ErrorMessage = "The code should not be longer than 15 characters")]
public string Code { get; set; } = "";
[Range(0, int.MaxValue, ErrorMessage = "The value needs to be equals or greater than 0")]
public double Value { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "The amount needs to be equals or greater than 0")]
public int Amount { get; set; }
}

View file

@ -1,61 +1,147 @@
@using BlazorTable
@using Moonlight.App.Repositories
@using System.Linq.Expressions
@using Mappy.Net
@typeparam TItem where TItem : class
@typeparam TCreateForm
@typeparam TUpdateForm
@inject Repository<TItem> ItemRepository
@inject ToastService ToastService
<div class="card">
<div class="card-header">
<h3 class="card-title">@(Title)</h3>
<div class="card-toolbar">
<button class="btn btn-icon btn-success">
<button @onclick="StartCreate" class="btn btn-icon btn-success">
<i class="bx bx-sm bx-plus"></i>
</button>
</div>
</div>
<div class="card-body">
<LazyLoader Load="LoadItems">
<LazyLoader @ref="LazyLoader" Load="LoadItems">
<Table TableItem="TItem"
Items="Items"
PageSize="50"
TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3 fs-6"
TableHeadClass="fw-bold text-muted">
@ChildContent
<Column TableItem="TItem" Field="IdExpression" Title="" Sortable="false" Filterable="false">
<Template>
<div class="text-end">
<div class="btn-group">
<button @onclick="() => StartUpdate(context)" class="btn btn-icon btn-warning">
<i class="bx bx-sm bx-slider"></i>
</button>
<button @onclick="() => StartDelete(context)" class="btn btn-icon btn-danger">
<i class="bx bx-sm bx-trash"></i>
</button>
</div>
</div>
</Template>
</Column>
</Table>
</LazyLoader>
</div>
</div>
<SmartModal @ref="CreateModal" CssClasses="modal-dialog-centered modal-lg">
<div class="modal-header">
<h5 class="modal-title">Create</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<SmartForm Model="CreateForm" OnValidSubmit="FinishCreate">
<div class="modal-body">
<div class="row">
<AutoForm Columns="6" Model="CreateForm"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</SmartForm>
</SmartModal>
<SmartModal @ref="UpdateModal" CssClasses="modal-dialog-centered modal-lg">
<div class="modal-header">
<h5 class="modal-title">Update</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<SmartForm Model="UpdateForm" OnValidSubmit="FinishUpdate">
<div class="modal-body">
<div class="row">
<AutoForm Columns="6" Model="UpdateForm"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</SmartForm>
</SmartModal>
<SmartModal @ref="DeleteModal" CssClasses="modal-dialog-centered">
<div class="modal-header">
<h5 class="modal-title">Do you want to delete this item?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="text-gray-400 fs-5 fw-semibold">
This action cannot be undone. The data will be deleted and cannot be restored
</p>
</div>
<div class="modal-footer p-3">
<div class="btn-group w-100">
<WButton OnClick="FinishDelete" Text="Delete" CssClasses="btn btn-danger w-50 me-3"/>
<button class="btn btn-secondary w-50" data-bs-dismiss="modal">Cancel</button>
</div>
</div>
</SmartModal>
@code
{
[Parameter]
public string Title { get; set; } = "";
[Parameter]
public Type CreateForm { get; set; }
[Parameter]
public Type UpdateForm { get; set; }
[Parameter]
public Func<Repository<TItem>, TItem[]> Load { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public Func<TItem, Task>? ValidateAdd { get; set; }
[Parameter]
public Func<TItem, Task>? ValidateUpdate { get; set; }
[Parameter]
public Func<TItem, Task>? ValidateDelete { get; set; }
private TItem[] Items;
private TCreateForm CreateForm;
private TUpdateForm UpdateForm;
private TItem ItemToUpdate;
private TItem ItemToDelete;
private SmartModal CreateModal;
private SmartModal UpdateModal;
private SmartModal DeleteModal;
private Expression<Func<TItem, object>> IdExpression;
private LazyLoader LazyLoader;
protected override void OnInitialized()
{
if (CreateForm == null)
throw new ArgumentNullException(nameof(CreateForm));
if (UpdateForm == null)
throw new ArgumentNullException(nameof(UpdateForm));
if (Load == null)
throw new ArgumentNullException(nameof(Load));
CreateForm = Activator.CreateInstance<TCreateForm>()!;
UpdateForm = Activator.CreateInstance<TUpdateForm>()!;
IdExpression = CreateExpression();
}
private Task LoadItems(LazyLoader _)
@ -64,4 +150,86 @@
return Task.CompletedTask;
}
private async Task StartUpdate(TItem item)
{
UpdateForm = Mapper.Map<TUpdateForm>(item);
ItemToUpdate = item;
await UpdateModal.Show();
}
private async Task FinishUpdate()
{
var item = Mapper.Map(ItemToUpdate, UpdateForm!);
if (ValidateUpdate != null) // Optional additional validation
await ValidateUpdate.Invoke(item);
ItemRepository.Update(item);
// Reset
await UpdateModal.Hide();
await LazyLoader.Reload();
await ToastService.Success("Successfully updated item");
}
private async Task StartCreate()
{
CreateForm = Activator.CreateInstance<TCreateForm>()!;
await CreateModal.Show();
}
private async Task FinishCreate()
{
var item = Mapper.Map<TItem>(CreateForm!);
if (ValidateAdd != null) // Optional additional validation
await ValidateAdd.Invoke(item);
ItemRepository.Add(item);
// Reset
await CreateModal.Hide();
await LazyLoader.Reload();
await ToastService.Success("Successfully added item");
}
private async Task StartDelete(TItem item)
{
ItemToDelete = item;
await DeleteModal.Show();
}
private async Task FinishDelete()
{
if (ValidateDelete != null) // Optional additional validation
await ValidateDelete.Invoke(ItemToDelete);
ItemRepository.Delete(ItemToDelete);
// Reset
await DeleteModal.Hide();
await LazyLoader.Reload();
await ToastService.Success("Successfully deleted item");
}
private Expression<Func<TItem, object>> CreateExpression()
{
// Parameter expression for the input object
var inputParam = Expression.Parameter(typeof(TItem), "input");
// Convert the input object to the actual model type (MyModel in this example)
var castedInput = Expression.Convert(inputParam, typeof(TItem));
// Create a property access expression using the property name
var propertyAccess = Expression.Property(castedInput, "Id");
// Convert the property value to an object
var castedResult = Expression.Convert(propertyAccess, typeof(object));
// Create a lambda expression
var lambda = Expression.Lambda<Func<TItem, object>>(castedResult, inputParam);
return lambda;
}
}

View file

@ -2,6 +2,7 @@
@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities.Store
@using Mappy.Net
@using Moonlight.App.Models.Forms.Admin.Store
@inject Repository<Coupon> CouponRepository
@inject ToastService ToastService

View file

@ -117,7 +117,10 @@
await StoreService.Gift.Redeem(IdentityService.CurrentUser, GiftCode);
// Reset
GiftCode = "";
await InvokeAsync(StateHasChanged);
await ToastService.Success("Successfully applied gift code");
await LazyLoader.Reload();
}

View file

@ -1,70 +1,43 @@
@page "/admin/store/coupons"
@using BlazorTable
@using Moonlight.App.Database.Entities.Store
@using Moonlight.App.Repositories
@using Moonlight.Shared.Components.Modals.Store
@using BlazorTable
@using Moonlight.App.Models.Forms.Admin.Store
@inject Repository<Coupon> CouponRepository
@inject Repository<CouponUse> CouponUseRepository
@inject ToastService ToastService
<AdminStoreNavigation Index="1" />
<AdminStoreNavigation Index="1"/>
<div class="card">
<div class="card-header">
<h3 class="card-title">Coupons</h3>
<div class="card-toolbar">
<button @onclick="() => AddCouponModal.Show()" class="btn btn-icon btn-success"><i class="bx bx-sm bx-plus"></i></button>
</div>
</div>
<div class="card-body">
<LazyLoader @ref="LazyLoader" Load="Load">
<Table TableItem="Coupon"
Items="AllCoupons"
PageSize="50"
TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3 fs-6"
TableHeadClass="fw-bold text-muted">
<Column TableItem="Coupon" Title="Id" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Code" Field="@(x => x.Code)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Amount" Field="@(x => x.Amount)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Percent" Field="@(x => x.Percent)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false">
<Template>
<a @onclick="() => Remove(context)" @onclick:preventDefault href="#" class="text-danger">Remove</a>
</Template>
</Column>
</Table>
</LazyLoader>
</div>
<div class="mt-5">
<AutoCrud TItem="Coupon"
TCreateForm="AddCouponForm"
TUpdateForm="EditCouponForm"
Title="Manage coupons"
Load="LoadData"
ValidateAdd="Validate"
ValidateUpdate="Validate">
<Column TableItem="Coupon" Title="Id" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Code" Field="@(x => x.Code)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Amount" Field="@(x => x.Amount)" Sortable="true" Filterable="true"/>
<Column TableItem="Coupon" Title="Percent" Field="@(x => x.Percent)" Sortable="true" Filterable="true"/>
</AutoCrud>
</div>
<AddCouponModal @ref="AddCouponModal" OnUpdate="() => LazyLoader.Reload()" />
@code
{
private Coupon[] AllCoupons;
private LazyLoader LazyLoader;
private AddCouponModal AddCouponModal;
private Task Load(LazyLoader _)
private Coupon[] LoadData(Repository<Coupon> repository)
{
AllCoupons = CouponRepository
return repository
.Get()
.ToArray();
}
private Task Validate(Coupon coupon)
{
if (CouponRepository.Get().Any(x => x.Code == coupon.Code && x.Id != coupon.Id))
throw new DisplayException("A coupon with that code does already exist");
return Task.CompletedTask;
}
private async Task Remove(Coupon coupon)
{
if (CouponUseRepository.Get().Any(x => x.Coupon.Id == coupon.Id))
throw new DisplayException("The coupon has been used so it cannot be deleted");
CouponRepository.Delete(coupon);
await ToastService.Success("Successfully deleted coupon");
await LazyLoader.Reload();
}
}
}

View file

@ -1,3 +1,41 @@
@page "/admin/store/gifts"
<AdminStoreNavigation Index="2" />
@using Moonlight.App.Database.Entities.Store
@using Moonlight.App.Models.Forms.Admin.Store
@using Moonlight.App.Repositories
@using BlazorTable
@inject Repository<GiftCode> GiftCodeRepository
<AdminStoreNavigation Index="2"/>
<div class="mt-5">
<AutoCrud TItem="GiftCode"
TCreateForm="AddGiftCodeForm"
TUpdateForm="EditGiftCodeForm"
Title="Manage gift codes"
Load="LoadData"
ValidateAdd="Validate">
<Column TableItem="GiftCode" Field="@(x => x.Code)" Title="Code" Sortable="false" Filterable="true" />
<Column TableItem="GiftCode" Field="@(x => x.Amount)" Title="Amount" Sortable="true" Filterable="true" />
<Column TableItem="GiftCode" Field="@(x => x.Value)" Title="Value" Sortable="true" Filterable="true" />
</AutoCrud>
</div>
@code
{
private GiftCode[] LoadData(Repository<GiftCode> repository)
{
return repository
.Get()
.ToArray();
}
private Task Validate(GiftCode giftCode)
{
if (GiftCodeRepository.Get().Any(x => x.Code == giftCode.Code && x.Id != giftCode.Id))
throw new DisplayException("A gift code with that code does already exist");
return Task.CompletedTask;
}
}