From 8c280213e9e9cea9a7db76082ab19eff2c286f31 Mon Sep 17 00:00:00 2001 From: Don Oerkfitz Date: Thu, 20 May 2021 22:16:31 -0500 Subject: [PATCH] project reorg. removed OSI-API.Business project added SQL backend --- OSI-API.Business/OSI-API.Business.csproj | 13 -- .../Objects/InvoiceRequestResponse.cs | 9 - OSI-API/Business/Database/Base/SQLBase.cs | 100 ++++++++ OSI-API/Business/Database/MySQL/MySQL.cs | 218 ++++++++++++++++++ OSI-API/Business/Objects/ClientObjects.cs | 9 + OSI-API/Business/Objects/InvoiceObjects.cs | 38 +++ OSI-API/Controllers/InvoicesController.cs | 7 +- OSI-API/Controllers/_EndpointBase.cs | 4 +- OSI-API/OSI-API.csproj | 5 +- 9 files changed, 372 insertions(+), 31 deletions(-) delete mode 100644 OSI-API.Business/OSI-API.Business.csproj delete mode 100644 OSI-API.Business/Objects/InvoiceRequestResponse.cs create mode 100644 OSI-API/Business/Database/Base/SQLBase.cs create mode 100644 OSI-API/Business/Database/MySQL/MySQL.cs create mode 100644 OSI-API/Business/Objects/ClientObjects.cs create mode 100644 OSI-API/Business/Objects/InvoiceObjects.cs diff --git a/OSI-API.Business/OSI-API.Business.csproj b/OSI-API.Business/OSI-API.Business.csproj deleted file mode 100644 index 0943e83..0000000 --- a/OSI-API.Business/OSI-API.Business.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net5.0 - OSI_API.Business - - - - - - - - diff --git a/OSI-API.Business/Objects/InvoiceRequestResponse.cs b/OSI-API.Business/Objects/InvoiceRequestResponse.cs deleted file mode 100644 index 3c96618..0000000 --- a/OSI-API.Business/Objects/InvoiceRequestResponse.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace OSI.API.Business.Objects -{ - public class InvoiceObject - { - public int InvoiceNumber { get; set; } - public float InvoiceAmount { get; set; } - public float Discount { get; set; } - } -} \ No newline at end of file diff --git a/OSI-API/Business/Database/Base/SQLBase.cs b/OSI-API/Business/Database/Base/SQLBase.cs new file mode 100644 index 0000000..b803263 --- /dev/null +++ b/OSI-API/Business/Database/Base/SQLBase.cs @@ -0,0 +1,100 @@ +using System; +using System.Text; + +namespace OSI.API.Business.Database.Base +{ + public abstract class SQLBase : IDisposable + { + protected DBConnectionInformation DBConnectionInfo; + + public enum DBType + { + MSSQL, + MYSQL + } + public struct DBConnectionInformation + { + public string DBHost { get; set; } + public string DBName { get; set; } + public string DBUserName { get; set; } + public string DBPassword { get; set; } + public DBType DBType { get; set; } + + public string GetConnectionString() + { + string rv = string.Empty; + + switch (DBType) + { + case DBType.MSSQL: + break; + case DBType.MYSQL: + rv = string.Format("Server={0};Database={1};Uid={2};Pwd={3}", DBHost, DBName, DBUserName, DBPassword); + break; + } + + return rv; + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + // free managed resources + } + // free native resources if there are any. + } + + protected string FormatNewLineForHTML(string input) + { + return input.Replace("\\r\\n", "
"); + } + + protected string FormatNewLineForHTML(string input, int numberOfBreaksToAdd) + { + StringBuilder breaks = new(); + for (int x = 0; x < numberOfBreaksToAdd; x++) + { + breaks.Append("
"); + } + + return input.Replace("\\r\\n", breaks.ToString()); + } + + protected string FormatBoldForHTML(string input) + { + return input.Replace("[b]", "").Replace("[/b]", ""); + } + + protected abstract void InitConnection(); + protected abstract void DisposeConnection(); + + protected abstract int GetOrdinal(string columnName); + protected abstract int? GetInt(int columnID); + protected abstract int? GetInt(string columnName); + protected abstract byte? GetByte(int columnID); + protected abstract byte? GetByte(string columnName); + protected abstract byte[] GetBytes(int columnID); + protected abstract byte[] GetBytes(string columnName); + protected abstract decimal? GetDecimal(int columnID); + protected abstract decimal? GetDecimal(string columnName); + protected abstract long? GetLong(int columnID); + protected abstract long? GetLong(string columnName); + protected abstract short? GetShort(int columnID); + protected abstract short? GetShort(string columnName); + protected abstract string GetString(int columnID); + protected abstract string GetString(string columnName); + protected abstract bool? GetBool(int columnID); + protected abstract bool? GetBool(string columnName); + protected abstract DateTime? GetDateTime(int columnID); + protected abstract DateTime? GetDateTime(string columnName); + + } +} diff --git a/OSI-API/Business/Database/MySQL/MySQL.cs b/OSI-API/Business/Database/MySQL/MySQL.cs new file mode 100644 index 0000000..ecd1e8d --- /dev/null +++ b/OSI-API/Business/Database/MySQL/MySQL.cs @@ -0,0 +1,218 @@ +using System; +using System.Data; +using System.IO; +using OSI.API.Business.Database.Base; +using MySql.Data.MySqlClient; + +namespace OSI.API.Business.Database +{ + public class MySQL : SQLBase + { + protected MySqlConnection DBConnection { get; set; } + protected MySqlCommand DBCommand { get; set; } + protected MySqlDataReader DBReader { get; set; } + + public MySQL(string server, string database, string username, string password) + { + DBConnectionInfo = new DBConnectionInformation() + { + DBHost = server, + DBUserName = username, + DBName = database, + DBPassword = password, + DBType = DBType.MYSQL + }; + } + + public MySQL(DBConnectionInformation connectionInformation) + { + DBConnectionInfo = connectionInformation; + } + + protected override void InitConnection() + { + DBConnection = new MySqlConnection(DBConnectionInfo.GetConnectionString()); + DBConnection.Open(); + DBCommand = DBConnection.CreateCommand(); + } + + protected override void DisposeConnection() + { + if (DBConnection.State == ConnectionState.Open) + { + DBConnection.Close(); + } + } + + protected override int GetOrdinal(string columnName) + { + return DBReader.GetOrdinal(columnName); + } + + protected override int? GetInt(int columnID) + { + int? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetInt32(columnID); + } + + return rv; + } + + protected override int? GetInt(string columnName) + { + return GetInt(GetOrdinal(columnName)); + } + + protected override byte? GetByte(int columnID) + { + byte? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetByte(columnID); + } + + return rv; + } + + protected override byte? GetByte(string columnName) + { + return GetByte(GetOrdinal(columnName)); + } + + protected override byte[] GetBytes(int columnID) + { + byte[] rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + const int CHUNK_SIZE = 2 * 1024; + byte[] buffer = new byte[CHUNK_SIZE]; + long bytesRead; + long fieldOffset = 0; + + using (var stream = new MemoryStream()) + { + while ((bytesRead = DBReader.GetBytes(columnID, fieldOffset, buffer, 0, buffer.Length)) == buffer.Length) + { + stream.Write(buffer, 0, (int)bytesRead); + fieldOffset += bytesRead; + } + rv = stream.ToArray(); + } + } + + return rv; + } + + protected override byte[] GetBytes(string columnName) + { + return GetBytes(GetOrdinal(columnName)); + } + + protected override decimal? GetDecimal(int columnID) + { + decimal? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetDecimal(columnID); + } + + return rv; + } + + protected override decimal? GetDecimal(string columnName) + { + return GetDecimal(GetOrdinal(columnName)); + } + + protected override long? GetLong(int columnID) + { + long? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetInt64(columnID); + } + + return rv; + } + + protected override long? GetLong(string columnName) + { + return GetLong(GetOrdinal(columnName)); + } + + protected override short? GetShort(int columnID) + { + short? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetInt16(columnID); + } + + return rv; + } + + protected override short? GetShort(string columnName) + { + return GetShort(GetOrdinal(columnName)); + } + + protected override string GetString(int columnID) + { + string rv = string.Empty; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetString(columnID); + } + + return rv; + } + + protected override string GetString(string columnName) + { + return GetString(GetOrdinal(columnName)); + } + + protected override bool? GetBool(int columnID) + { + bool? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = DBReader.GetBoolean(columnID); + } + + return rv; + } + + protected override bool? GetBool(string columnName) + { + return GetBool(GetOrdinal(columnName)); + } + + protected override DateTime? GetDateTime(int columnID) + { + DateTime? rv = null; + + if (!DBReader.IsDBNull(columnID)) + { + rv = GetDateTime(columnID); + } + + return rv; + } + + protected override DateTime? GetDateTime(string columnName) + { + return GetDateTime(GetOrdinal(columnName)); + } + } +} diff --git a/OSI-API/Business/Objects/ClientObjects.cs b/OSI-API/Business/Objects/ClientObjects.cs new file mode 100644 index 0000000..0a64eb5 --- /dev/null +++ b/OSI-API/Business/Objects/ClientObjects.cs @@ -0,0 +1,9 @@ +namespace OSI.API.Business.Objects +{ + public class ClientObject + { + public int ClientID { get; set; } + public string ClientName { get; set; } + + } +} \ No newline at end of file diff --git a/OSI-API/Business/Objects/InvoiceObjects.cs b/OSI-API/Business/Objects/InvoiceObjects.cs new file mode 100644 index 0000000..15881c1 --- /dev/null +++ b/OSI-API/Business/Objects/InvoiceObjects.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System; + +namespace OSI.API.Business.Objects +{ + public class InvoiceObject + { + public int InvoiceNumber { get; set; } + public float InvoiceAmount { get; set; } + public float Discount { get; set; } + public float Tax { get; set; } + public List InvoiceLineItems { get; set; } + public Notes Notes { get; set; } + public DateTime InvoiceDate { get; set; } + public DateTime DueDate { get; set; } + public ClientObject Client { get; set; } + } + + public class InvoiceObjectLineItems + { + public string Item { get; set; } + public string Description { get; set; } + public float Cost { get; set; } + public float Quantity { get; set; } + public float Tax { get; set; } + //public float Total => ((Cost * Quantity) + ((Cost * Quantity) * (Tax / 100))); + public float Total { get; set; } + + } + + public class Notes + { + public string PublicNotes { get; set; } + public string PrivateNotes { get; set; } + public string Terms { get; set; } + + } +} \ No newline at end of file diff --git a/OSI-API/Controllers/InvoicesController.cs b/OSI-API/Controllers/InvoicesController.cs index f6b8239..cbc7eef 100644 --- a/OSI-API/Controllers/InvoicesController.cs +++ b/OSI-API/Controllers/InvoicesController.cs @@ -19,6 +19,7 @@ namespace OSI.API.Controllers public override IActionResult Get() { + //> get all invoices as a list of InvoiceObjects throw new NotImplementedException(); } @@ -27,17 +28,17 @@ namespace OSI.API.Controllers return new JsonResult(id); } - public override IActionResult Post([FromBody] InvoiceObject input) + public override IActionResult Create([FromBody] InvoiceObject input) { throw new NotImplementedException(); } - public override IActionResult Put(int id, [FromBody] InvoiceObject input) + public override IActionResult Update([FromBody] InvoiceObject input) { return CreatedAtAction( "Get", routeValues: new { id = input.InvoiceAmount }, - value: Get(id) + value: input ); } } diff --git a/OSI-API/Controllers/_EndpointBase.cs b/OSI-API/Controllers/_EndpointBase.cs index 64d8270..1ca6576 100644 --- a/OSI-API/Controllers/_EndpointBase.cs +++ b/OSI-API/Controllers/_EndpointBase.cs @@ -15,11 +15,11 @@ namespace OSI.API.Controllers // POST: api/Clients [HttpPost] - public abstract IActionResult Post([FromBody] A input); + public abstract IActionResult Create([FromBody] A input); // PUT: api/Clients/5 [HttpPut("{id}")] - public abstract IActionResult Put(int id, [FromBody] A input); + public abstract IActionResult Update([FromBody] A input); // DELETE: api/Clients/5 [HttpDelete("{id}")] diff --git a/OSI-API/OSI-API.csproj b/OSI-API/OSI-API.csproj index 601b417..1b73b87 100644 --- a/OSI-API/OSI-API.csproj +++ b/OSI-API/OSI-API.csproj @@ -8,11 +8,8 @@ + - - - -