Skip to content

Commit

Permalink
added Lov controller, added ListID selection in the property editor, …
Browse files Browse the repository at this point in the history
…added pagination Lov data
  • Loading branch information
suxrobGM committed Jul 24, 2024
1 parent d8fc7ef commit 7fdbd0b
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 42 deletions.
58 changes: 39 additions & 19 deletions src/FormBuilder.API/Controllers/FormController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FormBuilder.API.Data;
using FormBuilder.API.Entities;
using FormBuilder.API.Mappers;
using FormBuilder.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand All @@ -22,24 +23,26 @@ public FormController(ApplicationDbContext context)
/// </summary>
/// <param name="pagedQuery">Paged query</param>
[HttpGet]
[ProducesResponseType(typeof(PagedResult<Form>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(PagedResult<FormDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetFormsPaged([FromQuery] PagedQuery pagedQuery)
{
var forms = await _context.Forms
.Skip((pagedQuery.Page - 1) * pagedQuery.PageSize)
.Take(pagedQuery.PageSize)
.ToListAsync();
.Select(i => i.ToDto())
.ToArrayAsync();

return Ok(forms);

return Ok(PagedResult<FormDto>.Succeed(forms));
}

/// <summary>
/// Gets a form by its id.
/// </summary>
/// <param name="id">Form ID</param>
[HttpGet("{id}")]
[ProducesResponseType(typeof(Result<Form>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result<FormDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetFormById(string id)
{
Expand All @@ -50,48 +53,48 @@ public async Task<IActionResult> GetFormById(string id)
return BadRequest(Result.Fail($"Form with id {id} not found"));
}

return Ok(Result<Form>.Succeed(form));
return Ok(Result<FormDto>.Succeed(form.ToDto()));
}

/// <summary>
/// Creates a new form.
/// </summary>
/// <param name="formDto">The form data.</param>
/// <param name="formCommand">The form data.</param>
[HttpPost]
[ProducesResponseType(typeof(Result<Form>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result<FormDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateForm(CreateFormDto formDto)
public async Task<IActionResult> CreateForm(CreateFormCommand formCommand)
{
if (string.IsNullOrEmpty(formDto.FormName))
if (string.IsNullOrEmpty(formCommand.FormName))
{
return BadRequest(Result.Fail("Form name is required"));
}

if (string.IsNullOrEmpty(formDto.FormDesign))
if (string.IsNullOrEmpty(formCommand.FormDesign))
{
return BadRequest(Result.Fail("Form design is required"));
}

var newForm = new Form
{
FormName = formDto.FormName,
FormDesign = formDto.FormDesign
FormName = formCommand.FormName,
FormDesign = formCommand.FormDesign
};

_context.Forms.Add(newForm);
await _context.SaveChangesAsync();
return Ok(Result<Form>.Succeed(newForm));
return Ok(Result<FormDto>.Succeed(newForm.ToDto()));
}

/// <summary>
/// Updates a form by its id.
/// </summary>
/// <param name="id">Existing form ID</param>
/// <param name="formDto">The form data.</param>
/// <param name="formCommand">The form data.</param>
[HttpPut("{id}")]
[ProducesResponseType(typeof(Result), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateForm(string id, UpdateFormDto formDto)
public async Task<IActionResult> UpdateForm(string id, UpdateFormCommand formCommand)
{
var existingForm = await _context.Forms.FindAsync(id);

Expand All @@ -100,18 +103,35 @@ public async Task<IActionResult> UpdateForm(string id, UpdateFormDto formDto)
return BadRequest(Result<Form>.Fail($"Form with ID {id} not found"));
}

if (!string.IsNullOrEmpty(formDto.FormName) && formDto.FormName != existingForm.FormName)
if (!string.IsNullOrEmpty(formCommand.FormName) && formCommand.FormName != existingForm.FormName)
{
existingForm.FormName = formDto.FormName;
existingForm.FormName = formCommand.FormName;
}

if (!string.IsNullOrEmpty(formDto.FormDesign) && formDto.FormDesign != existingForm.FormDesign)
if (!string.IsNullOrEmpty(formCommand.FormDesign) && formCommand.FormDesign != existingForm.FormDesign)
{
existingForm.FormDesign = formDto.FormDesign;
existingForm.FormDesign = formCommand.FormDesign;
}

_context.Update(existingForm);
await _context.SaveChangesAsync();
return Ok(Result.Succeed());
}

[HttpDelete("{id}")]
[ProducesResponseType(typeof(Result), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteForm(string id)
{
var existingForm = await _context.Forms.FindAsync(id);

if (existingForm is null)
{
return BadRequest(Result.Fail($"Form with ID {id} not found"));
}

_context.Forms.Remove(existingForm);
await _context.SaveChangesAsync();
return Ok(Result.Succeed());
}
}
70 changes: 70 additions & 0 deletions src/FormBuilder.API/Controllers/LovController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using FormBuilder.API.Data;
using FormBuilder.API.Mappers;
using FormBuilder.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace FormBuilder.API.Controllers;

[Route("api/lov")]
[ApiController]
public class LovController : ControllerBase
{
private readonly ApplicationDbContext _context;

public LovController(ApplicationDbContext context)
{
_context = context;
}

/// <summary>
/// Gets a paged list of List IDs.
/// </summary>
/// <param name="pagedQuery">Paged query</param>
[HttpGet]
[ProducesResponseType(typeof(PagedResult<int?>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetListIdPaged([FromQuery] PagedQuery pagedQuery)
{
var listIds = await _context.LovMaster
.Select(i => i.ListId)
.Distinct()
.Skip((pagedQuery.Page - 1) * pagedQuery.PageSize)
.Take(pagedQuery.PageSize)
.ToArrayAsync();

return Ok(PagedResult<int?>.Succeed(listIds));
}

/// <summary>
/// Gets list of values filtered by ListId
/// </summary>
[HttpGet("{listId:int}")]
[ProducesResponseType(typeof(Result<LovDto[]>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetLov(int listId)
{
var lovs = await _context.LovMaster
.Where(i => i.ListId == listId)
.Select(i => i.ToDto())
.ToArrayAsync();

return Ok(Result<LovDto[]>.Succeed(lovs));
}

/// <summary>
/// Batch add list of values
/// </summary>
/// <param name="batchAddLovCommand">The form data.</param>
[HttpPost]
[ProducesResponseType(typeof(Result), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Result), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> AddListOfValues(BatchAddLovCommand batchAddLovCommand)
{
var lovs = batchAddLovCommand.Lovs.Select(i => i.ToEntity());

await _context.LovMaster.AddRangeAsync(lovs);
await _context.SaveChangesAsync();
return Ok(Result.Succeed());
}
}
4 changes: 2 additions & 2 deletions src/FormBuilder.API/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ApplicationDbContext : DbContext
private readonly ApplicationDbContextOptions _dbContextOptions;

public DbSet<Form> Forms { get; set; }
public DbSet<LovMaster> LovMaster { get; set; }
public DbSet<Lov> LovMaster { get; set; }

public ApplicationDbContext(ApplicationDbContextOptions dbContextOptions)
{
Expand All @@ -30,6 +30,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new FormConfiguration());
modelBuilder.ApplyConfiguration(new LovMasterConfiguration());
modelBuilder.ApplyConfiguration(new LovConfiguration());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace FormBuilder.API.Entities;

public class LovMaster
/// <summary>
/// Entity class for list of values.
/// </summary>
public class Lov
{
public int Id { get; set; }
public int? ListId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace FormBuilder.API.ModelConfigurations;

public class LovMasterConfiguration : IEntityTypeConfiguration<LovMaster>
public class LovConfiguration : IEntityTypeConfiguration<Lov>
{
public void Configure(EntityTypeBuilder<LovMaster> builder)
public void Configure(EntityTypeBuilder<Lov> builder)
{
builder.ToTable("LovMaster");
builder.HasKey(e => e.Id);
Expand Down
17 changes: 17 additions & 0 deletions src/FormBuilder.API/Mappers/FormMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using FormBuilder.API.Entities;
using FormBuilder.Shared.Models;

namespace FormBuilder.API.Mappers;

public static class FormMapper
{
public static FormDto ToDto(this Form entity)
{
return new FormDto
{
Id = entity.Id,
FormName = entity.FormName,
FormDesign = entity.FormDesign
};
}
}
25 changes: 25 additions & 0 deletions src/FormBuilder.API/Mappers/LovMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FormBuilder.API.Entities;
using FormBuilder.Shared.Models;

namespace FormBuilder.API.Mappers;

public static class LovMapper
{
public static LovDto ToDto(this Lov entity)
{
return new LovDto
{
ListId = entity.ListId,
ListValue = entity.ListValue
};
}

public static Lov ToEntity(this LovDto dto)
{
return new Lov
{
ListId = dto.ListId,
ListValue = dto.ListValue
};
}
}
6 changes: 6 additions & 0 deletions src/FormBuilder.Shared/Models/BatchAddLovCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FormBuilder.Shared.Models;

public record BatchAddLovCommand
{
public IEnumerable<LovDto> Lovs { get; set; } = [];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FormBuilder.Shared.Models;

public record CreateFormDto
public record CreateFormCommand
{
public string? FormName { get; set; }
public string? FormDesign { get; set; }
Expand Down
7 changes: 7 additions & 0 deletions src/FormBuilder.Shared/Models/LovDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FormBuilder.Shared.Models;

public record LovDto
{
public int? ListId { get; set; }
public string? ListValue { get; set; }
}
5 changes: 5 additions & 0 deletions src/FormBuilder.Shared/Models/PagedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public virtual IDictionary<string, string> ToDictionary()

return queryDict;
}

public string ToQueryString()
{
return string.Join("&", ToDictionary().Select(i => $"{i.Key}={i.Value}"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FormBuilder.Shared.Models;

public record UpdateFormDto
public record UpdateFormCommand
{
public string? FormName { get; set; }
public string? FormDesign { get; set; }
Expand Down
21 changes: 17 additions & 4 deletions src/FormBuilder/Components/PropertyEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@
{
<RadzenFormField Text="List ID">
<RadzenDropDown
@bind-Value="SelectedListValue"
Data="selectField.Options"
TextProperty="Text"
ValueProperty="Value">
@bind-Value="SelectedListId"
Data="_listIds"
LoadData="LoadListIdValuesAsync"
AllowVirtualization="true"
AllowFiltering="true"
AllowClear="true">
</RadzenDropDown>
</RadzenFormField>

@if (selectField.Options.Any())
{
<RadzenFormField Text="List Values">
<RadzenDropDown

Check failure on line 54 in src/FormBuilder/Components/PropertyEditor.razor

View workflow job for this annotation

GitHub Actions / deploy-gh-pages

The type of component 'RadzenDropDown' cannot be inferred based on the values provided. Consider specifying the type arguments directly using the following attributes: 'TValue'.
Data="_listValues"
AllowFiltering="true"
AllowClear="true">
</RadzenDropDown>
</RadzenFormField>
}
}
</RadzenStack>
</RadzenTemplateForm>
Expand Down
Loading

0 comments on commit 7fdbd0b

Please sign in to comment.