using Neo.LegitimateLicences.Api.Helpers; using Neo.LegitimateLicences.Common; using Neo.LegitimateLicences.Data.Models; using System; using System.Linq; using System.Web.Http; using Newtonsoft.Json; namespace Neo.LegitimateLicences.Api { public partial class LegitimateLicencesApiController { #region GetPrintoutData [Route("~/1.0/Printing/GetPrintoutData")] [HttpGet] public JsonBaseResult GetPrintoutData(string verificationToken) { var response = new JsonBaseResult(); if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } var db = new LegitimateDatabase(); var printout = db.GetVerifiedPrintoutByToken(verificationToken, UserID); var desktopAppVersion = db.GetSystemSetting("DesktopAppVersion"); if (printout != null) { if (printout.EntityID == (short)EntityEnum.ReceiptDetails) { response.Result = new { printout, data = db.GetReceipt(printout.ItemID, UserID), desktopAppVersion }; } else if (printout.EntityID == (short)EntityEnum.OperatingLicences) { response.Result = new { printout , data = db.GetVerifiedOperatingLicence(printout.ItemID, printout.DocumentNumber), desktopAppVersion }; } else if (printout.EntityID == (short)EntityEnum.Accreditation) { response.Result = new { printout , data = db.GetVerifiedAccreditation(printout.ItemID), desktopAppVersion }; } else if (printout.EntityID == (short)EntityEnum.PlatformProviders) { // Materialize SP to a simple dictionary so JSON contains fields instead of {} var dict = new System.Collections.Generic.Dictionary(); using (var conn = new System.Data.SqlClient.SqlConnection(db.Context.Database.Connection.ConnectionString)) using (var cmd = new System.Data.SqlClient.SqlCommand("Verified.Pr_ProviderCertificatePrintData", conn)) using (var da = new System.Data.SqlClient.SqlDataAdapter(cmd)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@PlatformProviderID", printout.ItemID); cmd.Parameters.AddWithValue("@Reprint", printout.IsReprint); cmd.Parameters.AddWithValue("@TestPrint", printout.IsTestPrint); var dt = new System.Data.DataTable(); conn.Open(); da.Fill(dt); if (dt.Rows.Count > 0) { var row = dt.Rows[0]; foreach (System.Data.DataColumn col in dt.Columns) { dict[col.ColumnName] = row[col] == System.DBNull.Value ? null : row[col]; } } } // Avoid recursive/nested PrintData by clearing it before persisting printout.PrintData = null; response.Result = new { printout, data = dict, desktopAppVersion }; } else if (printout.EntityID == (short)EntityEnum.ApplicationLicenceTemporaryReplacement) { response.Result = new { printout, data = db.GetApplicationLicenceTemporaryReplacement(printout.ItemID, UserID), desktopAppVersion }; } response.Success = true; } else { response.Success = false; } var json = Newtonsoft.Json.JsonConvert.SerializeObject(response.Result, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore}); db.SavePrintoutData(verificationToken, json, UserID); return response; } #endregion } // DTO to materialize Verified.Pr_ProviderCertificatePrintData cleanly public class ProviderCertificateData { public long PlatformProviderID { get; set; } public string LegalName { get; set; } public string TradingName { get; set; } public string BusinessRegNumber { get; set; } public string RegisteredAddressLine1 { get; set; } public string RegisteredAddressLine2 { get; set; } public string RegisteredAddressSuburb { get; set; } public string RegisteredAddressCity { get; set; } public short? RegisteredAddressProvinceID { get; set; } public string RegisteredAddressPostalCode { get; set; } public long? ProviderCertificateID { get; set; } public string CertificateNumber { get; set; } public DateTime? IssuedAt { get; set; } public DateTime? ExpiresAt { get; set; } public int IssueNumber { get; set; } public bool IsReprint { get; set; } public bool IsTestPrint { get; set; } public string ReceiptNumber { get; set; } public string RegistrarName { get; set; } public string TransactionTitle { get; set; } } }