Skip to content

Commit

Permalink
fixed Lov dropdown, added test Lov data
Browse files Browse the repository at this point in the history
  • Loading branch information
suxrobGM committed Jul 24, 2024
1 parent 7fdbd0b commit 78c0c89
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 16 deletions.
4 changes: 4 additions & 0 deletions BlazorFormBuilder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{1CF9
ProjectSection(SolutionItems) = preProject
scripts\run-api.cmd = scripts\run-api.cmd
scripts\run-app.cmd = scripts\run-app.cmd
scripts\add-migration.cmd = scripts\add-migration.cmd
scripts\apply-migration.cmd = scripts\apply-migration.cmd
scripts\remove-migration.cmd = scripts\remove-migration.cmd
scripts\seed-database.cmd = scripts\seed-database.cmd
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FormBuilder.Shared", "src\FormBuilder.Shared\FormBuilder.Shared.csproj", "{A47F6C89-EEE4-4F2C-9CC0-4C40724DFDE5}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/add-migration.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if "%MigrationName%" == "" (
goto prompt
)

echo Running migration for PostgreSQL...
echo Running migration for SQL Server...
dotnet ef migrations add %MigrationName% --project ../src/FormBuilder.API

echo Migrations completed.
Expand Down
5 changes: 5 additions & 0 deletions scripts/seed-database.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
title Seed Database
cd ../src/FormBuilder.API
dotnet run --seed
pause
4 changes: 3 additions & 1 deletion src/FormBuilder.API/Controllers/FormController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public async Task<IActionResult> GetFormsPaged([FromQuery] PagedQuery pagedQuery
.Select(i => i.ToDto())
.ToArrayAsync();

var itemsCount = await _context.Forms.CountAsync();
var pagesCount = (int)Math.Ceiling(itemsCount / (double)pagedQuery.PageSize);

return Ok(PagedResult<FormDto>.Succeed(forms));
return Ok(PagedResult<FormDto>.Succeed(forms, pagedQuery.PageSize, pagesCount));
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion src/FormBuilder.API/Controllers/LovController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public async Task<IActionResult> GetListIdPaged([FromQuery] PagedQuery pagedQuer
.Take(pagedQuery.PageSize)
.ToArrayAsync();

return Ok(PagedResult<int?>.Succeed(listIds));
var itemsCount = await _context.LovMaster
.Select(i => i.ListId)
.Distinct()
.CountAsync();

var pagesCount = (int)Math.Ceiling(itemsCount / (double)pagedQuery.PageSize);

return Ok(PagedResult<int?>.Succeed(listIds, pagedQuery.PageSize, pagesCount));
}

/// <summary>
Expand Down
48 changes: 48 additions & 0 deletions src/FormBuilder.API/Data/SeedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FormBuilder.API.Entities;

namespace FormBuilder.API.Data;

public class SeedData
{
public static async Task InitializeAsync(IServiceProvider serviceProvider)
{
var logger = serviceProvider.GetRequiredService<ILogger<SeedData>>();
await using var context = serviceProvider.GetRequiredService<ApplicationDbContext>();

// Look for any existing data
if (context.LovMaster.Any())
{
logger.LogInformation("Database already seeded with LOV items");
return;
}

logger.LogInformation("Seeding database");
var lovData = GenerateLovData(200);

context.LovMaster.AddRange(lovData);
await context.SaveChangesAsync();
logger.LogInformation("Database seeded with {Count} LOV items", lovData.Count);
}

/// <summary>
/// Generates a list of LOV items with random ListId and ListValue
/// </summary>
/// <param name="count">The number of LOV items to generate</param>
/// <returns>A list of LOV items with random ListId and ListValue</returns>
private static List<Lov> GenerateLovData(int count)
{
var random = new Random();
var lovData = new List<Lov>();

for (var i = 0; i < count; i++)
{
lovData.Add(new Lov
{
ListId = random.Next(1, 10), // Random ListId between 1 and 10
ListValue = $"Value {i + 1}"
});
}

return lovData;
}
}
13 changes: 11 additions & 2 deletions src/FormBuilder.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FormBuilder.API;
using FormBuilder.API.Data;
using Serilog;

Log.Logger = new LoggerConfiguration()
Expand All @@ -12,8 +13,16 @@
var app = WebApplication.CreateBuilder(args)
.ConfigureServices()
.ConfigurePipeline();

app.Run();

if (args.Contains("--seed"))
{
using var serviceScope = app.Services.CreateScope();
await SeedData.InitializeAsync(serviceScope.ServiceProvider);
}
else
{
app.Run();
}
}
catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException")
{
Expand Down
10 changes: 7 additions & 3 deletions src/FormBuilder.Shared/Models/PagedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
/// </summary>
public class PagedQuery
{
public PagedQuery() : this(null, 1, 10)
{
}

public PagedQuery(
string? orderBy = null,
int page = 1,
int pageSize = 10)
string? orderBy,
int page,
int pageSize)
{
OrderBy = orderBy;
Page = page;
Expand Down
2 changes: 1 addition & 1 deletion src/FormBuilder.Shared/Models/PagedResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public PagedResult(IEnumerable<T>? data, int pageSize, int pagesCount)
/// <param name="items">The data to be returned in the PagedResult object</param>
/// <param name="pageSize">The number of items per page</param>
/// <param name="pagesCount">The total number of pages</param>
public static PagedResult<T> Succeed(IEnumerable<T>? items, int pageSize, int pagesCount) =>
public new static PagedResult<T> Succeed(IEnumerable<T>? items, int pageSize, int pagesCount) =>

Check warning on line 36 in src/FormBuilder.Shared/Models/PagedResult.cs

View workflow job for this annotation

GitHub Actions / deploy-gh-pages

The member 'PagedResult<T>.Succeed(IEnumerable<T>?, int, int)' does not hide an accessible member. The new keyword is not required.
new(items, pageSize, pagesCount);

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/FormBuilder/Components/FormBuilder.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</RadzenColumn>

<RadzenColumn Class="border bg-light px-1" Size="12" SizeMD="3" SizeLG="2">
<RadzenStack Class="pt-2 pb-2" Orientation="Orientation.Vertical">
<RadzenStack Class="pt-2" Orientation="Orientation.Vertical">
<RadzenText TextStyle="TextStyle.H5" TextAlign="TextAlign.Center">Fields</RadzenText>
<Dragable Data="() => AddField(FieldType.Text)" Zone="FieldsZone">
<RadzenButton Class="w-100" Text="Text Field" Click="@(() => AddField(FieldType.Text))"/>
Expand All @@ -67,7 +67,7 @@
</Dragable>
<hr/>
</RadzenStack>
<RadzenStack Class="mt-3" Orientation="Orientation.Vertical" Gap="1rem">
<RadzenStack Orientation="Orientation.Vertical" Gap="1rem">
<RadzenText TextStyle="TextStyle.H5" TextAlign="TextAlign.Center">Properties</RadzenText>
<PropertyEditor @bind-SelectedField="_selectedField" FieldTypeChanged="(e) => HandleFieldTypeChanged(e)"/>
<hr/>
Expand Down
2 changes: 0 additions & 2 deletions src/FormBuilder/Components/FormField.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
@bind-Value="selectField.Value"
Data="@selectField.Options"
Placeholder="@selectField.Placeholder"
TextProperty="Text"
ValueProperty="Value"
ReadOnly="Disabled">
</RadzenDropDown>
break;
Expand Down
2 changes: 2 additions & 0 deletions src/FormBuilder/Components/PropertyEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<RadzenDropDown
@bind-Value="SelectedListId"
Data="_listIds"
Count="@_listIdCount"
LoadData="LoadListIdValuesAsync"
AllowVirtualization="true"
AllowFiltering="true"
Expand All @@ -52,6 +53,7 @@
{
<RadzenFormField Text="List Values">
<RadzenDropDown
TValue="string"
Data="_listValues"
AllowFiltering="true"
AllowClear="true">
Expand Down
13 changes: 10 additions & 3 deletions src/FormBuilder/Components/PropertyEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace FormBuilder.Components;
public partial class PropertyEditor : ComponentBase
{
private DropDownEnumItem<FieldType>[] _inputTypes = DropDownEnumItem.CreateItems<FieldType>();
private int? _selectedListId;
private IEnumerable<int> _listIds = [];
private IEnumerable<string?> _listValues = [];
private IEnumerable<int> _listIds = [];
private int _listIdCount;

#region Injected Services

Expand Down Expand Up @@ -156,11 +156,18 @@ private int? SelectedListId
}

#endregion


protected override async Task OnInitializedAsync()
{
// Load the first 10 list IDs
await LoadListIdValuesAsync(new LoadDataArgs {Top = 10});
}

private async Task LoadListIdValuesAsync(LoadDataArgs args)
{
var pagedData = await FormService.GetListIdPagedAsync(args.ToPagedQuery());
_listIds = pagedData.Data ?? [];
_listIdCount = pagedData.PageSize * pagedData.PagesCount;
}

private async Task FetchListValuesAsync(int? listId)
Expand Down
7 changes: 7 additions & 0 deletions src/FormBuilder/Models/SelectField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public SelectField()
Type = FieldType.Select;
}

/// <summary>
/// The list ID that corresponds to the list of options for this field.
/// </summary>
public int? ListId { get; set; }

/// <summary>
/// Associated list values for the particular list ID.
/// </summary>
public IEnumerable<string> Options { get; set; } = [];
}

0 comments on commit 78c0c89

Please sign in to comment.