|
| 1 | +@page "/" |
| 2 | +@rendermode InteractiveServer |
| 3 | +@using System.Collections |
| 4 | +@inject SubscriptionRepository SubscriptionService |
| 5 | + |
| 6 | +<div class="container-fluid p-4"> |
| 7 | + <h1 class="mb-4">Subscription Management System</h1> |
| 8 | + <p class="mb-3">Manage and view all subscription records from the MariaDB database.</p> |
| 9 | + |
| 10 | + <SfGrid TValue="SubscriptionModel" AllowSorting="true" AllowFiltering="true" AllowGrouping="true" AllowPaging="true" |
| 11 | + Height="500px" Width="100%" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })"> |
| 12 | + <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> |
| 13 | + <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings> |
| 14 | + <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings> |
| 15 | + <GridPageSettings PageSize="10"></GridPageSettings> |
| 16 | + <GridColumns> |
| 17 | + <GridColumn Field=@nameof(SubscriptionModel.Id) IsPrimaryKey="true" ShowInColumnChooser="false" ShowColumnMenu="false"></GridColumn> |
| 18 | + <GridColumn Field=@nameof(SubscriptionModel.PublicID) HeaderText="Public ID" Width="170" TextAlign="TextAlign.Left" AllowAdding="false" AllowEditing="false"> |
| 19 | + <Template> |
| 20 | + @{ |
| 21 | + var data = (SubscriptionModel)context; |
| 22 | + } |
| 23 | + <a class="status-text status-ticket-id"> |
| 24 | + @data.PublicID |
| 25 | + </a> |
| 26 | + </Template> |
| 27 | + </GridColumn> |
| 28 | + <GridColumn Field=@nameof(SubscriptionModel.CustomerId) HeaderText="Customer ID" Width="140" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.NumericEdit" /> |
| 29 | + <GridColumn Field=@nameof(SubscriptionModel.SubscriptionID) HeaderText="Subscription ID" Width="140" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.NumericEdit" /> |
| 30 | + <GridColumn Field=@nameof(SubscriptionModel.InvoiceNumber) HeaderText="Invoice No" Width="130" EditType="EditType.DefaultEdit" /> |
| 31 | + <GridColumn Field=@nameof(SubscriptionModel.Description) HeaderText="Description" Width="180" EditType="EditType.DefaultEdit" /> |
| 32 | + <GridColumn Field=@nameof(SubscriptionModel.Amount) HeaderText="Amount" Width="130" ValidationRules="@(new ValidationRules { Required = true })" Format="N2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit"> |
| 33 | + <Template> |
| 34 | + @{ |
| 35 | + var amount = (context as SubscriptionModel)?.Amount ?? 0; |
| 36 | + var amountClass = amount < 0 ? "amnt-negative" : "amnt-positive"; |
| 37 | + } |
| 38 | + <span class="@amountClass">@amount.ToString("N2")</span> |
| 39 | + </Template> |
| 40 | + </GridColumn> |
| 41 | + <GridColumn Field=@nameof(SubscriptionModel.CurrencyCode) HeaderText="Curr" Width="100" TextAlign="TextAlign.Center" EditType="EditType.DefaultEdit" /> |
| 42 | + <GridColumn Field=@nameof(SubscriptionModel.SubscriptionType) HeaderText="Type" Width="100" EditType="EditType.DropDownEdit" EditorSettings="@TypeDropDownParams"> |
| 43 | + <Template> |
| 44 | + @{ |
| 45 | + var data = (SubscriptionModel)context; |
| 46 | + } |
| 47 | + <span class="chip @GetCategoryClass(data)"> |
| 48 | + @data.SubscriptionType |
| 49 | + </span> |
| 50 | + </Template> |
| 51 | + </GridColumn> |
| 52 | + <GridColumn Field=@nameof(SubscriptionModel.PaymentGateway) HeaderText="Gateway" Width="130" EditType="EditType.DropDownEdit" EditorSettings="@GatewayDropDownParams" /> |
| 53 | + <GridColumn Field=@nameof(SubscriptionModel.SubscriptionStartDate) HeaderText="Start Date" Width="170" Format="dd-MMM-yy hh:mm tt" TextAlign="TextAlign.Right" Type="ColumnType.DateTime" AllowEditing="false" /> |
| 54 | + <GridColumn Field=@nameof(SubscriptionModel.SubscriptionEndDate) HeaderText="End Date" Width="170" Format="dd-MMM-yy hh:mm tt" TextAlign="TextAlign.Right" Type="ColumnType.DateTime" EditType="EditType.DateTimePickerEdit" /> |
| 55 | + |
| 56 | + <GridColumn Field=@nameof(SubscriptionModel.Status) HeaderText="Status" Width="110" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.DropDownEdit" EditorSettings="@StatusDropDownParams"> |
| 57 | + <Template> |
| 58 | + @{ |
| 59 | + var status = (context as SubscriptionModel)?.Status; |
| 60 | + var badgeClass = status?.ToLower() switch |
| 61 | + { |
| 62 | + "active" => "e-badge e-badge-success", |
| 63 | + "expired" => "e-badge e-badge-danger", |
| 64 | + "pending" => "e-badge e-badge-warning", |
| 65 | + "paused" => "e-badge e-badge-info", |
| 66 | + _ => "e-badge" |
| 67 | + }; |
| 68 | + } |
| 69 | + <span class="@badgeClass">@status</span> |
| 70 | + </Template> |
| 71 | + </GridColumn> |
| 72 | + </GridColumns> |
| 73 | + </SfGrid> |
| 74 | +</div> |
| 75 | + |
| 76 | +@code { |
| 77 | + private CustomAdaptor? _customAdaptor; |
| 78 | + |
| 79 | + protected override void OnInitialized() |
| 80 | + { |
| 81 | + // Initialize the CustomAdaptor with the injected SubscriptionRepository |
| 82 | + _customAdaptor = new CustomAdaptor { SubscriptionService = SubscriptionService }; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// CustomAdaptor class to handle grid data operations with MariaDB using Entity Framework |
| 87 | + /// </summary> |
| 88 | + public class CustomAdaptor : DataAdaptor |
| 89 | + { |
| 90 | + public static SubscriptionRepository? _subscriptionService { get; set; } |
| 91 | + public SubscriptionRepository? SubscriptionService |
| 92 | + { |
| 93 | + get => _subscriptionService; |
| 94 | + set => _subscriptionService = value; |
| 95 | + } |
| 96 | + |
| 97 | + public override async Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string? key = null) |
| 98 | + { |
| 99 | + IEnumerable dataSource = await _subscriptionService!.GetSubscriptionsAsync(); |
| 100 | + |
| 101 | + // Handling Search |
| 102 | + if (dataManagerRequest.Search != null && dataManagerRequest.Search.Count > 0) |
| 103 | + { |
| 104 | + dataSource = DataOperations.PerformSearching(dataSource, dataManagerRequest.Search); |
| 105 | + } |
| 106 | + |
| 107 | + // Handling Filtering |
| 108 | + if (dataManagerRequest.Where != null && dataManagerRequest.Where.Count > 0) |
| 109 | + { |
| 110 | + dataSource = DataOperations.PerformFiltering(dataSource, dataManagerRequest.Where, dataManagerRequest.Where[0].Operator); |
| 111 | + } |
| 112 | + |
| 113 | + // Handling Sorting |
| 114 | + if (dataManagerRequest.Sorted != null && dataManagerRequest.Sorted.Count > 0) |
| 115 | + { |
| 116 | + dataSource = DataOperations.PerformSorting(dataSource, dataManagerRequest.Sorted); |
| 117 | + } |
| 118 | + |
| 119 | + int totalRecordsCount = dataSource.Cast<SubscriptionModel>().Count(); |
| 120 | + // Handling Paging |
| 121 | + if (dataManagerRequest.Skip != 0) |
| 122 | + { |
| 123 | + dataSource = DataOperations.PerformSkip(dataSource, dataManagerRequest.Skip); |
| 124 | + } |
| 125 | + if (dataManagerRequest.Take != 0) |
| 126 | + { |
| 127 | + dataSource = DataOperations.PerformTake(dataSource, dataManagerRequest.Take); |
| 128 | + } |
| 129 | + |
| 130 | + // Handling Group operation in CustomAdaptor. |
| 131 | + if (dataManagerRequest.Group != null) |
| 132 | + { |
| 133 | + foreach (var group in dataManagerRequest.Group) |
| 134 | + { |
| 135 | + dataSource = DataUtil.Group<SubscriptionModel>(dataSource, group, dataManagerRequest.Aggregates, 0, dataManagerRequest.GroupByFormatter); |
| 136 | + //Add custom logic here if needed and remove above method |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return dataManagerRequest.RequiresCounts ? new DataResult() { Result = dataSource, Count = totalRecordsCount } : (object)dataSource; |
| 141 | + } |
| 142 | + |
| 143 | + public override async Task<object> InsertAsync(DataManager dataManager, object value, string? key) |
| 144 | + { |
| 145 | + await _subscriptionService!.AddSubscriptionAsync(value as SubscriptionModel); |
| 146 | + return value; |
| 147 | + } |
| 148 | + |
| 149 | + public override async Task<object> UpdateAsync(DataManager dataManager, object value, string? keyField, string key) |
| 150 | + { |
| 151 | + await _subscriptionService!.UpdateSubscriptionAsync(value as SubscriptionModel); |
| 152 | + return value; |
| 153 | + } |
| 154 | + |
| 155 | + public override async Task<object> RemoveAsync(DataManager dataManager, object value, string? keyField, string key) |
| 156 | + { |
| 157 | + await _subscriptionService!.RemoveSubscriptionAsync(value as int?); |
| 158 | + return value; |
| 159 | + } |
| 160 | + |
| 161 | + public override async Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string? keyField, string key, int? dropIndex) |
| 162 | + { |
| 163 | + if (changedRecords != null) |
| 164 | + { |
| 165 | + foreach (var record in (IEnumerable<SubscriptionModel>)changedRecords) |
| 166 | + { |
| 167 | + await _subscriptionService!.UpdateSubscriptionAsync(record as SubscriptionModel); |
| 168 | + } |
| 169 | + } |
| 170 | + if (addedRecords != null) |
| 171 | + { |
| 172 | + foreach (var record in (IEnumerable<SubscriptionModel>)addedRecords) |
| 173 | + { |
| 174 | + await _subscriptionService!.AddSubscriptionAsync(record as SubscriptionModel); |
| 175 | + } |
| 176 | + } |
| 177 | + if (deletedRecords != null) |
| 178 | + { |
| 179 | + foreach (var record in (IEnumerable<SubscriptionModel>)deletedRecords) |
| 180 | + { |
| 181 | + await _subscriptionService!.RemoveSubscriptionAsync((record as SubscriptionModel).Id); |
| 182 | + } |
| 183 | + } |
| 184 | + return key; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// Provides a list of payment gateway options used as a data source for the Gateway dropdown editor in the grid. |
| 190 | + /// </summary> |
| 191 | + private static List<SubscriptionModel> CustomGateway = new List<SubscriptionModel> { |
| 192 | + new SubscriptionModel() { PaymentGateway = "Razorpay" }, |
| 193 | + new SubscriptionModel() { PaymentGateway= "GooglePay" }, |
| 194 | + new SubscriptionModel() { PaymentGateway= "Stripe" }, |
| 195 | + new SubscriptionModel() { PaymentGateway= "PhonePe" }, |
| 196 | + new SubscriptionModel() { PaymentGateway= "Paytm" }, |
| 197 | + }; |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Provides a list of subscription types used as a data source for the Type dropdown editor in the grid. |
| 201 | + /// </summary> |
| 202 | + private static List<SubscriptionModel> CustomType = new List<SubscriptionModel> { |
| 203 | + new SubscriptionModel() { SubscriptionType = "ACTIVE" }, |
| 204 | + new SubscriptionModel() { SubscriptionType = "PAUSED" }, |
| 205 | + new SubscriptionModel() { SubscriptionType = "EXPIRED" }, |
| 206 | + }; |
| 207 | + |
| 208 | + /// <summary> |
| 209 | + /// Provides a list of subscription statuses used as a data source for the Status dropdown editor in the grid. |
| 210 | + /// </summary> |
| 211 | + private static List<SubscriptionModel> CustomStatus = new List<SubscriptionModel> { |
| 212 | + new SubscriptionModel() { Status = "ACTIVE" }, |
| 213 | + new SubscriptionModel() { Status = "PAUSED" }, |
| 214 | + new SubscriptionModel() { Status = "PENDING" }, |
| 215 | + new SubscriptionModel() { Status = "EXPIRED" }, |
| 216 | + }; |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// Dropdown editor settings configured with payment gateway options for the PaymentGateway column in grid edit mode. |
| 220 | + /// </summary> |
| 221 | + private IEditorSettings GatewayDropDownParams = new DropDownEditCellParams |
| 222 | + { |
| 223 | + Params = new DropDownListModel<object, object>() { DataSource = CustomGateway, Query = new Syncfusion.Blazor.Data.Query() }, |
| 224 | + }; |
| 225 | + |
| 226 | + /// <summary> |
| 227 | + /// Dropdown editor settings configured with subscription type options for the SubscriptionType column in grid edit mode. |
| 228 | + /// </summary> |
| 229 | + private IEditorSettings TypeDropDownParams = new DropDownEditCellParams |
| 230 | + { |
| 231 | + Params = new DropDownListModel<object, object>() { DataSource = CustomType, Query = new Syncfusion.Blazor.Data.Query() }, |
| 232 | + }; |
| 233 | + |
| 234 | + /// <summary> |
| 235 | + /// Dropdown editor settings configured with subscription status options for the Status column in grid edit mode. |
| 236 | + /// </summary> |
| 237 | + private IEditorSettings StatusDropDownParams = new DropDownEditCellParams |
| 238 | + { |
| 239 | + Params = new DropDownListModel<object, object>() { DataSource = CustomStatus, Query = new Syncfusion.Blazor.Data.Query() }, |
| 240 | + }; |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// Returns a CSS class name based on the subscription type to apply visual styling in the grid template. |
| 244 | + /// </summary> |
| 245 | + private string GetCategoryClass(SubscriptionModel data) |
| 246 | + { |
| 247 | + return data.SubscriptionType?.ToLower() switch |
| 248 | + { |
| 249 | + "active" => "category-active", |
| 250 | + "paused" => "category-paused", |
| 251 | + "expired" => "category-expired", |
| 252 | + _ => "" |
| 253 | + }; |
| 254 | + } |
| 255 | +} |
0 commit comments