From 4e757fccc39d6e5e55666b172903f32d837420cc Mon Sep 17 00:00:00 2001 From: jojo aquino Date: Mon, 16 Dec 2024 17:31:30 +0000 Subject: [PATCH] Identification Document Editor --- .../Models/IdentificationDocumentModel.cs | 34 +++++ .../IdentificationDocument.cshtml | 25 ++++ .../IdentificationDocument.cshtml.cs | 139 ++++++++++++++++++ .../IdentificationDocumentModel.cshtml | 66 +++++++++ .../wwwroot/js/identification-document.js | 58 ++++++++ 5 files changed, 322 insertions(+) create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Models/IdentificationDocumentModel.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml create mode 100644 EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Pages/Shared/EditorTemplates/IdentificationDocumentModel.cshtml create mode 100644 EnotaryoPH/EnotaryoPH.Web/wwwroot/js/identification-document.js diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Models/IdentificationDocumentModel.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Models/IdentificationDocumentModel.cs new file mode 100644 index 0000000..8580a6b --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Models/IdentificationDocumentModel.cs @@ -0,0 +1,34 @@ +using EnotaryoPH.Data.Entities; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace EnotaryoPH.Web.Common.Models +{ + public class IdentificationDocumentModel + { + public IdentificationDocumentModel() + { } + + public IdentificationDocumentModel(IdentificationDocument document) + { + using var stream = new MemoryStream(document.File); + File = new FormFile(stream, 0, stream.Length, document.Filename, document.Filename); + ExpirationDate = document.ExpirationDate; + DateIssued = document.DateIssued; + PlaceIssued = document.PlaceIssued; + IdNumber = document.IdNumber; + Filename = document.Filename; + ImageBase64Url = File.ToBase64StringUrl(); + IdentificationType = document.Type; + } + + public DateTime? DateIssued { get; set; } + public DateTime? ExpirationDate { get; set; } + public IFormFile File { get; set; } + public string Filename { get; set; } + public string IdentificationType { get; set; } + public List? IdentificationTypes { get; set; } + public string IdNumber { get; set; } + public string? ImageBase64Url { get; set; } + public string PlaceIssued { get; set; } + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml b/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml new file mode 100644 index 0000000..1e0de82 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml @@ -0,0 +1,25 @@ +@page "{IdentificationDocument_UID?}" +@model EnotaryoPH.Web.Pages.Principal.IdentificationDocument.IdentificationDocumentModel + +@section Head { + +} + +
+
+

Identification Document

+
+ @Html.EditorFor(m => m.IdentificationDocument) +
+ +
+
+
+
+ +@section Scripts { + +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml.cs b/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml.cs new file mode 100644 index 0000000..3213da8 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Pages/Principal/IdentificationDocument/IdentificationDocument.cshtml.cs @@ -0,0 +1,139 @@ +using EnotaryoPH.Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using NsEntities = EnotaryoPH.Data.Entities; + +namespace EnotaryoPH.Web.Pages.Principal.IdentificationDocument +{ + public class IdentificationDocumentModel : PageModel + { + private readonly ICurrentUserService _currentUserService; + private readonly NotaryoDBContext _notaryoDBContext; + private readonly IWebHostEnvironment _webHostEnvironment; + + public IdentificationDocumentModel(IWebHostEnvironment webHostEnvironment, NotaryoDBContext notaryoDBContext, ICurrentUserService currentUserService) + { + _webHostEnvironment = webHostEnvironment; + _notaryoDBContext = notaryoDBContext; + _currentUserService = currentUserService; + } + + public IActionResult OnGet() + { + if (IdentificationDocument_UID != Guid.Empty) + { + var user = _notaryoDBContext.Users.AsNoTracking().FirstOrDefault(u => u.User_UID == _currentUserService.GetUser_UID()); + + var document = _notaryoDBContext.IdentificationDocuments.AsNoTracking().FirstOrDefault(d => d.IdentificationDocument_UID == IdentificationDocument_UID && d.UserID == user.UserID); + if (document == null) + { + return NotFound(); + } + + IdentificationDocument = new Common.Models.IdentificationDocumentModel(document); + } + else + { + var defaultImageFilename = $"{_webHostEnvironment.WebRootPath}/images/image-white-on-gray-400x300.jpg"; + using var stream = new MemoryStream(System.IO.File.ReadAllBytes(defaultImageFilename)); + var formFile = new FormFile(stream, 0, stream.Length, "Untitled.jpg", "Untitled.jpg"); + IdentificationDocument = new Common.Models.IdentificationDocumentModel + { + File = formFile, + ImageBase64Url = formFile.ToBase64StringUrl() + }; + } + LoadIdentificationDocumentTypes(); + return Page(); + } + + public async Task OnPostAsync() + { + if (!ModelState.IsValid) + { + return PostbackPage(); + } + + if (IdentificationDocument.ExpirationDate <= DateTime.Now) + { + ModelState.AddModelError(FullName.Of(IdentificationDocument.ExpirationDate), "Your Identification Document has expired"); + return PostbackPage(); + } + + if (IdentificationDocument.DateIssued > DateTime.Now) + { + ModelState.AddModelError(FullName.Of(IdentificationDocument.DateIssued), "Your Identification Document has not yet been issued"); + return PostbackPage(); + } + + var user = _notaryoDBContext.Users.AsNoTracking().FirstOrDefault(u => u.User_UID == _currentUserService.GetUser_UID()); + var entity = new NsEntities.IdentificationDocument(); + if (IdentificationDocument_UID != Guid.Empty) + { + entity = _notaryoDBContext.IdentificationDocuments.FirstOrDefault(e => e.IdentificationDocument_UID == IdentificationDocument_UID && e.UserID == user.UserID); + if (entity == null) + { + return BadRequest(); + } + } + entity.ExpirationDate = IdentificationDocument.ExpirationDate.ToUTC(); + entity.DateIssued = IdentificationDocument.DateIssued.ToUTC(); + entity.PlaceIssued = IdentificationDocument.PlaceIssued; + entity.IdNumber = IdentificationDocument.IdNumber; + entity.Type = IdentificationDocument.IdentificationType; + entity.UploadedOn = DateTime.UtcNow; + entity.UserID = user.UserID; + + var file = IdentificationDocument.File; + var stream = new MemoryStream((int)file.Length); + file.CopyTo(stream); + entity.Filename = file.FileName; + entity.File = stream.ToArray(); + + if (entity.IdentificationDocumentID > 0) + { + _notaryoDBContext.Update(entity); + } + else + { + entity.CreatedOn = DateTime.UtcNow; + entity.IdentificationDocument_UID = Guid.NewGuid(); + _notaryoDBContext.Add(entity); + } + _notaryoDBContext.SaveChanges(); + return RedirectToPage("/Principal/Dashboard/Dashboard"); + } + + private IActionResult PostbackPage() + { + if (IdentificationDocument.File != null) + { + IdentificationDocument.ImageBase64Url = IdentificationDocument.File.ToBase64StringUrl(); + //using var stream = IdentificationDocument.File.OpenReadStream(); + //IdentificationDocument.File = new FormFile(stream, 0, stream.Length, IdentificationDocument.File.Name, IdentificationDocument.File.Name); + } + + LoadIdentificationDocumentTypes(); + return Page(); + } + + private void LoadIdentificationDocumentTypes() + { + var lookupIdentificationTypes = _notaryoDBContext.LookupData.AsNoTracking().Include(e => e.LookupDataValues).FirstOrDefault(e => e.Name == "Identification Types"); + IdentificationDocument.IdentificationTypes = lookupIdentificationTypes.LookupDataValues + .ConvertAll(m => new SelectListItem + { + Text = m.Title.DefaultIfEmpty(m.Value), + Value = m.Value + }); + } + + [BindProperty] + public Common.Models.IdentificationDocumentModel IdentificationDocument { get; set; } + + [BindProperty(SupportsGet = true)] + public Guid IdentificationDocument_UID { get; set; } + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Pages/Shared/EditorTemplates/IdentificationDocumentModel.cshtml b/EnotaryoPH/EnotaryoPH.Web/Pages/Shared/EditorTemplates/IdentificationDocumentModel.cshtml new file mode 100644 index 0000000..844c466 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Pages/Shared/EditorTemplates/IdentificationDocumentModel.cshtml @@ -0,0 +1,66 @@ +@model EnotaryoPH.Web.Common.Models.IdentificationDocumentModel +@section Head { + +} + +
+
+
+ +
+ + +
+ + @Html.ValidationMessageFor(m => m.File) + +
+
+
+
+
+
+
+
+ + + @Html.ValidationMessageFor(m => m.IdentificationType) +
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + + @Html.ValidationMessageFor(m => m.ExpirationDate) +
+
+
+
+ + + @Html.ValidationMessageFor(m => m.DateIssued) +
+
+
+
+
+
+
\ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/wwwroot/js/identification-document.js b/EnotaryoPH/EnotaryoPH.Web/wwwroot/js/identification-document.js new file mode 100644 index 0000000..9924fbc --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/wwwroot/js/identification-document.js @@ -0,0 +1,58 @@ +"use strict"; +(function () { + let + control_fileInput = document.querySelector(".identification-document__upload__file"), + control_uploadButton = document.querySelector(".identification-document__upload__button"), + control_filename = document.querySelector(".identification-document__filename"), + control_imageLink = document.querySelector(".identification-document__a"), + control_image = document.querySelector(".identification-document__a__img"), + control_imageBase64Url = document.querySelector(".identification-document__base64"), + x = 1; + + function _bindEvents() { + control_uploadButton.addEventListener("click", function () { + control_fileInput.click(); + }); + + control_fileInput.addEventListener("change", function (event) { + const file = event.target.files[0]; + if(file) { + console.log('File name:', file.name); + console.log('File size:', file.size); + console.log('File type:', file.type); + control_filename.value = file.name; + const reader = new FileReader(); + reader.onload = function (e) { + debugger; + const base64StringUrl = e.target.result; + control_imageLink.href = base64StringUrl; + control_image.src = base64StringUrl; + console.log('Base64 String:', base64String); + }; + reader.readAsDataURL(file); + } else { + console.log('No file selected'); + } + }); + } + + function _init() { + _bindEvents(); + _initFiles(); + } + + function _initFiles() { + if (control_imageBase64Url.value) { + debugger; + const fileContent = control_imageBase64Url.value; + const filename = control_filename.value; + const file = new File([fileContent], filename); + const dataTransfer = new DataTransfer(); + dataTransfer.items.add(file); + const fileList = dataTransfer.files; + control_fileInput.files = fileList; + } + } + + _init(); +})(); \ No newline at end of file