From 0e4419063897caa5d1fa86a2be4f59654a5ef118 Mon Sep 17 00:00:00 2001 From: Griffiths Lott Date: Sat, 12 Nov 2022 09:56:59 -0500 Subject: [PATCH] Working endopints for single contracts and new payments | no email --- .gitignore | 2 + Controllers/ContractController.cs | 23 ++++++ Controllers/NewPaymentsController.cs | 26 +++++++ Models/ContractData.cs | 34 +++++++++ Models/VendorPayment.cs | 18 +++++ PaymentServer.csproj | 17 +++++ PaymentServer.csproj.user | 7 ++ Program.cs | 27 +++++++ Properties/launchSettings.json | 31 ++++++++ README.md | 0 Services/PaymentDataService.cs | 101 +++++++++++++++++++++++++++ appsettings.Development.json | 8 +++ appsettings.json | 9 +++ 13 files changed, 303 insertions(+) create mode 100644 .gitignore create mode 100644 Controllers/ContractController.cs create mode 100644 Controllers/NewPaymentsController.cs create mode 100644 Models/ContractData.cs create mode 100644 Models/VendorPayment.cs create mode 100644 PaymentServer.csproj create mode 100644 PaymentServer.csproj.user create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 README.md create mode 100644 Services/PaymentDataService.cs create mode 100644 appsettings.Development.json create mode 100644 appsettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd42ee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ diff --git a/Controllers/ContractController.cs b/Controllers/ContractController.cs new file mode 100644 index 0000000..07d154e --- /dev/null +++ b/Controllers/ContractController.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using PaymentServer.Models; +using PaymentServer.Services; + + +namespace PaymentServer.Controllers +{ + [Route("[controller]")] + [ApiController] + public class ContractController : ControllerBase + { + [HttpGet("{contractNumber}")] + public ActionResult Get(string contractNumber) + { + // Validating the contract number is handled by the PaymentDataService + List? payments = PaymentDataService.GetContractData(contractNumber); + if (payments == null) + return NotFound(); + return Ok(payments); + } + } +} diff --git a/Controllers/NewPaymentsController.cs b/Controllers/NewPaymentsController.cs new file mode 100644 index 0000000..aae80db --- /dev/null +++ b/Controllers/NewPaymentsController.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using PaymentServer.Models; +using PaymentServer.Services; + +namespace PaymentServer.Controllers +{ + [Route("[controller]")] + [ApiController] + public class NewPaymentsController : ControllerBase + { + [HttpGet("{timestamp}")] + public ActionResult Get(string timestamp) + { + DateTime dateTime = DateTime.Parse(timestamp); + List? payments = PaymentDataService.GetNew(dateTime); + if (payments == null) + return NotFound(); + ContractData contractData = new ContractData(); + contractData.AddPayments(payments); + string contractJson = contractData.ToJson(); + Console.WriteLine(contractJson); + return Ok(contractJson); + } + } +} diff --git a/Models/ContractData.cs b/Models/ContractData.cs new file mode 100644 index 0000000..17bbedb --- /dev/null +++ b/Models/ContractData.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using PaymentServer.Models; +using Newtonsoft.Json; + +namespace PaymentServer.Models +{ + public class ContractData + { + public Dictionary> contracts = new Dictionary>(); + + public void AddPayments(List vendorPayments) + { + List existingContracts = new List(this.contracts.Keys); + + foreach (VendorPayment payment in vendorPayments) + { + if (existingContracts.Contains(payment.contractNumber)) + { + this.contracts[payment.contractNumber].Add(payment); + } + else + { + this.contracts.Add(payment.contractNumber, new List { payment }); + existingContracts.Add(payment.contractNumber); + } + } + Console.WriteLine(this.contracts.Count); + } + public string ToJson() + { + return JsonConvert.SerializeObject(this.contracts.Values); + } + } +} diff --git a/Models/VendorPayment.cs b/Models/VendorPayment.cs new file mode 100644 index 0000000..c0e07e4 --- /dev/null +++ b/Models/VendorPayment.cs @@ -0,0 +1,18 @@ +namespace PaymentServer.Models +{ + public class VendorPayment + { + public string contractNumber { get; set; } + public string bachNumber { get; set; } + public string vendorId { get; set; } + public string pordNumber { get; set; } + public decimal purchaseAmount { get; set; } + public DateTime docDate { get; set; } + public DateTime dexRowTimestamp { get; set; } + public int? customerKey { get; set; } + public decimal? totalListPrice { get; set; } + public decimal? equipmentCost { get; set; } + public string? salesRep { get; set; } + public string? salesRepEmail { get; set; } + } +} diff --git a/PaymentServer.csproj b/PaymentServer.csproj new file mode 100644 index 0000000..56f2b79 --- /dev/null +++ b/PaymentServer.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/PaymentServer.csproj.user b/PaymentServer.csproj.user new file mode 100644 index 0000000..efbdd2d --- /dev/null +++ b/PaymentServer.csproj.user @@ -0,0 +1,7 @@ + + + + ApiControllerEmptyScaffolder + root/Common/Api + + \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..4d026bf --- /dev/null +++ b/Program.cs @@ -0,0 +1,27 @@ + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); + +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); +app.Run(); + diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..d6b99f1 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:22575", + "sslPort": 44333 + } + }, + "profiles": { + "PaymentServer": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7100;http://localhost:5100", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Services/PaymentDataService.cs b/Services/PaymentDataService.cs new file mode 100644 index 0000000..078d445 --- /dev/null +++ b/Services/PaymentDataService.cs @@ -0,0 +1,101 @@ +using Microsoft.Data.SqlClient; +using PaymentServer.Models; +using System.Text.RegularExpressions; + + +namespace PaymentServer.Services +{ + public static class PaymentDataService + { + static string connectionString = "Server=LPP-SQL01;Database=NLCF;Trusted_Connection=True;TrustServerCertificate=True;"; + static string baseQuery = $@" + SELECT TOP (5000) + SUBSTRING(TRXDSCRN, PATINDEX('%[0-9]^3-[0-9]^7-[0-9]^3%', TRXDSCRN), 16) as ContractNumber + ,BACHNUMB, VENDORID, PORDNMBR, PRCHAMNT, DOCDATE, DEX_ROW_TS + ,app.CustomerKey + ,app.TotalListPrice + ,app.EquipmentCost + ,ps.PersonnelName + ,su.Email + FROM [NLCF].[dbo].[PM30200] as pif + LEFT JOIN[LCCPHL-PDSQL003].[LEAFCore].[dbo].[Application] as app + ON + app.ContractID = TRY_CAST(SUBSTRING(TRXDSCRN, PATINDEX('%[0-9]^3-[0-9]^7-[0-9]^3%', TRXDSCRN) + 5, 7) as int) + AND app.ScheduleID = TRY_CAST(SUBSTRING(TRXDSCRN, PATINDEX('%[0-9]^3-[0-9]^7-[0-9]^3%', TRXDSCRN) + 13, 3) as int) + AND app.LessorID = TRY_CAST(SUBSTRING(TRXDSCRN, PATINDEX('%[0-9]^3-[0-9]^7-[0-9]^3%', TRXDSCRN), 4) as int) + LEFT JOIN[LCCPHL-PDSQL003].[LEAFCore].[dbo].[Personnel] as ps + ON app.MarketingRepID = ps.PersonnelID + LEFT JOIN[LCCPHL-PDSQL003].[LEAFCore].[dbo].[SalesUser] as su + ON + (PARSENAME(REPLACE(ps.PersonnelName, ' ', '.'), 1) = su.FirstName AND REPLACE(REVERSE(PARSENAME(REVERSE(REPLACE(ps.PersonnelName, ' ', '.')), 1)), ',', '') = su.LastName) + WHERE DOCTYPE = 1 + AND(BACHNUMB LIKE '%ACH%' OR BACHNUMB LIKE '%CKS%' OR BACHNUMB LIKE '%WIRE%' OR BACHNUMB LIKE '%CHK%' OR BACHNUMB LIKE '%CK%')"; + static Regex contractNumberRegex = new Regex(@"\d{3}-\d{7}-\d{3}"); + + public static string? ValidContractNumber(string contractNumber) + { + Match match = contractNumberRegex.Match(contractNumber); + if (match.Success) + return match.Value; + else return null; + + } + + public static List? GetContractData(string contractNumber) + { + // Validate the contract number to make sure nothing strange get's added to the query + string query = baseQuery + $" AND TRXDSCRN = '{ValidContractNumber(contractNumber)}'"; + return PullPaymentData(query); + + } + + public static List? GetNew(DateTime minTimestamp) + { + string query = baseQuery +$@" AND DEX_ROW_TS >= '{minTimestamp.ToString("yyyy-MM-dd HH:mm:ss")} + AND TRXDSCRN LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]%''"; + return PullPaymentData(query); + } + + static List? PullPaymentData(string sqlQuery) + { + using (SqlConnection connection = new SqlConnection(connectionString)) + { + SqlCommand command = new SqlCommand(sqlQuery, connection); + connection.Open(); + SqlDataReader reader = command.ExecuteReader(); + List? payments = new List(); + try + { + while (reader.Read()) + { + VendorPayment paymentData = new VendorPayment + { + contractNumber = reader.GetString(0), + bachNumber = reader.GetString(1), + vendorId = reader.GetString(2), + pordNumber = reader.GetString(3), + purchaseAmount = reader.GetDecimal(4), + docDate = reader.GetDateTime(5), + dexRowTimestamp = reader.GetDateTime(6), + customerKey = reader.IsDBNull(7) ? null : reader.GetInt32(7), + totalListPrice = reader.IsDBNull(8) ? null : reader.GetDecimal(8), + equipmentCost = reader.IsDBNull(9) ? null : reader.GetDecimal(9), + salesRep = reader.IsDBNull(10) ? null : reader.GetString(10), + salesRepEmail = reader.IsDBNull(11) ? null : reader.GetString(11) + + }; + payments.Add(paymentData); + Console.WriteLine(String.Format("{0}", paymentData)); + } + } + finally + { + reader.Close(); + } + Console.WriteLine("Failed to get data..."); + return payments; + } + } + + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}