Skip to content

Commit

Permalink
Add capability to export to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleybot committed Aug 29, 2024
1 parent b17dcd8 commit b03140f
Show file tree
Hide file tree
Showing 32 changed files with 550 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/UDS.Net.Forms.Tests/UDS.Net.Forms.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ReleaseVersion>4.1.0-preview.1</ReleaseVersion>
<ReleaseVersion>4.1.0-preview.2</ReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
Expand Down
61 changes: 61 additions & 0 deletions src/UDS.Net.Forms/Extensions/DomainToViewModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UDS.Net.Forms.Models.UDS4;
using UDS.Net.Services.DomainModels;
using UDS.Net.Services.DomainModels.Forms;
using UDS.Net.Services.DomainModels.Submission;
using UDS.Net.Services.Enums;
using UDS.Net.Services.LookupModels;

Expand Down Expand Up @@ -68,6 +69,66 @@ public static VisitModel ToVM(this Visit visit)
};
}

public static PacketSubmissionsModel ToVM(this IEnumerable<PacketSubmission> packetSubmissions)
{
PacketSubmissionsModel vm = new PacketSubmissionsModel();

if (packetSubmissions != null)
{
foreach (var packetSubmission in packetSubmissions)
{
vm.List.Add(packetSubmission.ToVM());
}
}

return vm;
}

public static PacketSubmissionModel ToVM(this PacketSubmission packetSubmission)
{
PacketSubmissionModel vm = new PacketSubmissionModel();

if (packetSubmission != null)
{
vm.Id = packetSubmission.Id;
vm.SubmissionDate = packetSubmission.SubmissionDate;
vm.CreatedAt = packetSubmission.CreatedAt;
vm.CreatedBy = packetSubmission.CreatedBy;
vm.ModifiedBy = packetSubmission.ModifiedBy;
vm.ErrorCount = packetSubmission.ErrorCount;

if (packetSubmission.Errors != null)
{
vm.Errors = packetSubmission.Errors.ToVM();
}
}

return vm;
}

public static PacketSubmissionErrorsPaginatedModel ToVM(this List<PacketSubmissionError> packetSubmissionErrors)
{
PacketSubmissionErrorsPaginatedModel vm = new PacketSubmissionErrorsPaginatedModel();

if (packetSubmissionErrors != null)
{
foreach (var error in packetSubmissionErrors)
{
vm.List.Add(error.ToVM());
}
}

return vm;
}

public static PacketSubmissionErrorModel ToVM(this PacketSubmissionError packetSubmissionError)
{
return new PacketSubmissionErrorModel
{
// TODO error model properties
};
}

public static MilestoneModel ToVM(this Milestone milestone)
{
return new MilestoneModel()
Expand Down
6 changes: 6 additions & 0 deletions src/UDS.Net.Forms/Extensions/ViewModelToDomainMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UDS.Net.Forms.Models.UDS4;
using UDS.Net.Services.DomainModels;
using UDS.Net.Services.DomainModels.Forms;
using UDS.Net.Services.DomainModels.Submission;

namespace UDS.Net.Forms.Extensions
{
Expand Down Expand Up @@ -75,6 +76,11 @@ public static Visit ToEntity(this VisitModel vm)
return new Visit(vm.Id, vm.VISITNUM, vm.ParticipationId, vm.FORMVER, vm.PACKET, vm.VISIT_DATE, vm.INITIALS, vm.Status, vm.CreatedAt, vm.CreatedBy, vm.ModifiedBy, vm.DeletedBy, vm.IsDeleted, vm.Forms.ToEntity());
}

public static PacketSubmission ToEntity(this PacketSubmissionModel vm)
{
return new PacketSubmission(vm.Id, vm.SubmissionDate, vm.VisitId, vm.CreatedAt, vm.CreatedBy, vm.ModifiedBy, "", false, 0);
}

public static List<Form> ToEntity(this IList<FormModel> vm)
{
return vm.Select(v => v.ToEntity()).ToList();
Expand Down
11 changes: 11 additions & 0 deletions src/UDS.Net.Forms/Models/PacketSubmissionErrorsPaginatedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
namespace UDS.Net.Forms.Models
{
public class PacketSubmissionErrorsPaginatedModel : PaginatedModel
{
public int PacketSubmissionId { get; set; }

public List<PacketSubmissionErrorModel> List { get; set; } = new List<PacketSubmissionErrorModel>();
}
}

28 changes: 26 additions & 2 deletions src/UDS.Net.Forms/Models/PacketSubmissionModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
using System;
using System.ComponentModel.DataAnnotations;
using UDS.Net.Services.DomainModels;
using UDS.Net.Services.DomainModels.Submission;

namespace UDS.Net.Forms.Models
{
public class PacketSubmissionModel
{
public int Id { get; set; }

public int VisitId { get; set; }

[Required]
[Display(Name = "Submission date")]
public DateTime SubmissionDate { get; set; }

[Required]
public DateTime CreatedAt { get; set; }

[Required]
public string CreatedBy { get; set; } = "";

public string? ModifiedBy { get; set; }

public int ErrorCount { get; set; } = 0;

public virtual IList<PacketSubmissionErrorModel> Errors { get; set; } = new List<PacketSubmissionErrorModel>();
public virtual PacketSubmissionErrorsPaginatedModel Errors { get; set; } = new PacketSubmissionErrorsPaginatedModel();
public string GetFileName(string participantLegacyId, DateTime visitDate)
{
return $"UDS_{participantLegacyId}_{visitDate.Year}_EXPORTED_{SubmissionDate.ToFileTime()}.csv";
}
}
}

10 changes: 10 additions & 0 deletions src/UDS.Net.Forms/Models/PacketSubmissionsPaginatedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace UDS.Net.Forms.Models
{
public class PacketSubmissionsModel
{
public bool CanCreateNewSubmission { get; set; } = false;
public List<PacketSubmissionModel> List { get; set; } = new List<PacketSubmissionModel>();
}
}

48 changes: 48 additions & 0 deletions src/UDS.Net.Forms/Models/PageModels/PacketSubmissionPageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using UDS.Net.Forms.Extensions;
using UDS.Net.Services;

namespace UDS.Net.Forms.Models.PageModels
{
public class PacketSubmissionPageModel : PageModel
{
protected readonly IVisitService _visitService;
protected readonly IPacketSubmissionService _packetSubmissionService;

[BindProperty]
public PacketSubmissionModel? PacketSubmission { get; set; }

public VisitModel? Visit { get; set; }

public PacketSubmissionPageModel(IVisitService visitService, IPacketSubmissionService packetSubmissionService) : base()
{
_visitService = visitService;
_packetSubmissionService = packetSubmissionService;
}

public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null || id == 0)
return NotFound();

var packetSubmission = await _packetSubmissionService.GetById("", id.Value);

if (packetSubmission == null)
return NotFound();

PacketSubmission = packetSubmission.ToVM();

var visit = await _visitService.GetById("", packetSubmission.VisitId);

if (visit == null)
return NotFound();

Visit = visit.ToVM();

return Page();
}
}
}

2 changes: 1 addition & 1 deletion src/UDS.Net.Forms/Models/PaginatedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int CurrentPageEnd
}
}

public string Search { get; set; }
public string Search { get; set; } = "";
public string Action { get; set; } = "Index";
public int? Id { get; set; } = null; // sometimes the paginated list is a child of the current object, do we might need an id
}
Expand Down
33 changes: 32 additions & 1 deletion src/UDS.Net.Forms/Pages/PacketSubmissions/Create.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
@page
@page "{id:int?}"
@model UDS.Net.Forms.Pages.PacketSubmissions.CreateModel
@{
ViewData["Title"] = "Create packet submission";
}

<div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<form method="post">
<div class="relative transform overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6">
<div>
<div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
<svg class="h-6 w-6 text-green-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
</svg>
</div>
<div class="mt-3 text-center sm:mt-5">
<h3 class="text-base font-semibold leading-6 text-gray-900" id="modal-title">Are you sure?</h3>
<div class="mt-2">
<p class="text-sm text-gray-500">Clicking "Yes" will download the .csv file to your computer.</p>
</div>
</div>
</div>
<div class="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
<input asp-for="PacketSubmission.VisitId" type="hidden" />
<input asp-for="PacketSubmission.SubmissionDate" type="hidden" />
<input asp-for="PacketSubmission.CreatedAt" type="hidden" />
<input asp-for="PacketSubmission.CreatedBy" type="hidden" />
<button type="submit" class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 sm:col-start-2">Yes, download</button>
<a asp-page="Index" asp-route-visitId="@Model.Visit.Id" class="mt-3 inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 sm:col-start-1 sm:mt-0">Cancel</a>
</div>
</div>


</form>
</div>
82 changes: 80 additions & 2 deletions src/UDS.Net.Forms/Pages/PacketSubmissions/Create.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,88 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using UDS.Net.Forms.Extensions;
using UDS.Net.Forms.Models;
using UDS.Net.Forms.Models.PageModels;
using UDS.Net.Services;
using UDS.Net.Services.DomainModels;

namespace UDS.Net.Forms.Pages.PacketSubmissions
{
public class Create
public class CreateModel : PageModel
{
public Create()
protected readonly IVisitService _visitService;
protected readonly IParticipationService _participationService;
protected readonly IPacketSubmissionService _packetSubmissionService;

[BindProperty]
public PacketSubmissionModel? PacketSubmission { get; set; }

public VisitModel? Visit { get; set; }

public string PageTitle
{
get
{
if (Visit != null)
{
return $"Participant {Visit.Participation.LegacyId} Visit {Visit.VISITNUM} Packet Submissions";
}
return "";
}
}

public CreateModel(IVisitService visitService, IParticipationService participationService, IPacketSubmissionService packetSubmissionService)
{
_visitService = visitService;
_participationService = participationService;
_packetSubmissionService = packetSubmissionService;
}

public async Task<IActionResult> OnGetAsync(int? visitId)
{
if (visitId == null || visitId == 0)
return NotFound();

var visit = await _visitService.GetById("", visitId.Value);

if (visit == null)
return NotFound();

var participation = await _participationService.GetById("", visit.ParticipationId);

if (participation == null)
return NotFound();

Visit = visit.ToVM();

Visit.Participation = participation.ToVM();

PacketSubmission = new PacketSubmissionModel
{
VisitId = visit.Id,
SubmissionDate = DateTime.Now,
CreatedAt = DateTime.UtcNow,
CreatedBy = User.Identity.IsAuthenticated ? User.Identity.Name : "Username"
};

return Page();
}

public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();

try
{
// TODO add isn't working yet
await _packetSubmissionService.Add(User.Identity.IsAuthenticated ? User.Identity.Name : "Username", PacketSubmission.ToEntity());
}
catch (Exception ex)
{
}
return RedirectToPage("./Index", new { VisitId = PacketSubmission.VisitId });
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/UDS.Net.Forms/Pages/PacketSubmissions/Details.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@page
@model UDS.Net.Forms.Pages.PacketSubmissions.DetailsModel
@{
}

18 changes: 18 additions & 0 deletions src/UDS.Net.Forms/Pages/PacketSubmissions/Details.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using UDS.Net.Forms.Models.PageModels;
using UDS.Net.Services;

namespace UDS.Net.Forms.Pages.PacketSubmissions
{
public class DetailsModel : PacketSubmissionPageModel
{
public DetailsModel(IVisitService visitService, IPacketSubmissionService packetSubmissionService) : base(visitService, packetSubmissionService)
{
}
}
}
4 changes: 3 additions & 1 deletion src/UDS.Net.Forms/Pages/PacketSubmissions/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@page
@page "{id:int?}"
@model UDS.Net.Forms.Pages.PacketSubmissions.EditModel
@{
ViewData["Title"] = "Edit packet submission";
}

Loading

0 comments on commit b03140f

Please sign in to comment.