Advertisement
Guest User

Model

a guest
May 22nd, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Drawing;
  10. using static Enums;
  11. using System.Text;
  12.  
  13. namespace MaterialSolutions.Models
  14. {
  15.     public class Forklift
  16.     {
  17.         public int ForkliftID { get; set; }
  18.         [Required(ErrorMessage = "Serial number is required.")]
  19.         [Display(Name = "Serial")]
  20.         public string SerialNumber { get; set; }
  21.         [Required(ErrorMessage = "Model number is required.")]
  22.         [Display(Name = "Model")]
  23.         public string ModelNumber { get; set; }
  24.         [Required(ErrorMessage = "Manufacturer is required.")]
  25.         [Display(Name = "ManufacturerID")]
  26.         public int ManufacturerID { get; set; }
  27.         [Required(ErrorMessage = "Power type is required.")]
  28.         [Display(Name = "Power")]
  29.         public Power Power { get; set; }
  30.         [Required(ErrorMessage = "Capacity is required.")]
  31.         [Display(Name = "Capacity")]
  32.         public int Capacity { get; set; }
  33.         [Display(Name = "Year")]
  34.         public int Year { get; set; }
  35.         [Display(Name = "Lowered")]
  36.         public int LoweredHeight { get; set; }
  37.         [Required(ErrorMessage = "Raised height is required.")]
  38.         [Display(Name = "Raised")]
  39.         public int RaisedHeight { get; set; }
  40.         [Display(Name = "Purchased")]
  41.         public decimal PurchasedPrice { get; set; }
  42.         [Display(Name = "Wholesale")]
  43.         public decimal WholesalePrice { get; set; }
  44.         [Display(Name = "Retail")]
  45.         public decimal RetailPrice { get; set; }
  46.         [Display(Name = "Comments")]
  47.         public string Comments { get; set; }
  48.         [Required(ErrorMessage = "Location is required.")]
  49.         [Display(Name = "LocationID")]
  50.         public int LocationID { get; set; }
  51.         [Display(Name = "Created")]
  52.         public DateTime Created { get; set; }
  53.         [Display(Name = "Updated")]
  54.         public DateTime Updated { get; set; }
  55.  
  56.         //Object properties.
  57.         [Display(Name = "Manufacturer")]
  58.         public Manufacturer Manufacturer { get; set; }
  59.         [Display(Name = "Location")]
  60.         public Client Location { get; set; }
  61.  
  62.         public static Forklift Setup(DataRow dr)
  63.         {
  64.             return new Forklift
  65.             {
  66.                 ForkliftID = Convert.ToInt32(dr["ForkliftID"]),
  67.                 SerialNumber = dr["SerialNumber"].ToString(),
  68.                 ModelNumber = dr["ModelNumber"].ToString(),
  69.                 ManufacturerID = Convert.ToInt32(dr["ManufacturerID"]),
  70.                 Power = (Power)Convert.ToInt32(dr["PowerID"]),
  71.                 Capacity = Convert.ToInt32(dr["Capacity"]),
  72.                 Year = Convert.ToInt32(dr["Year"]),
  73.                 LoweredHeight = Convert.ToInt32(dr["LoweredHeight"]),
  74.                 RaisedHeight = Convert.ToInt32(dr["RaisedHeight"]),
  75.                 PurchasedPrice = Convert.ToDecimal(dr["PurchasedPrice"]),
  76.                 WholesalePrice = Convert.ToDecimal(dr["WholesalePrice"]),
  77.                 RetailPrice = Convert.ToDecimal(dr["RetailPrice"]),
  78.                 Comments = dr["Comments"].ToString(),
  79.                 LocationID = Convert.ToInt32(dr["LocationID"]),
  80.                 Created = Convert.ToDateTime(dr["Created"]),
  81.                 Updated = Convert.ToDateTime(dr["Updated"]),
  82.                 Manufacturer = Manufacturer.GetForkliftManufacturerByID(Convert.ToInt32(dr["ManufacturerID"])),
  83.                 Location = Client.GetClientByID(Convert.ToInt32(dr["LocationID"]))
  84.             };
  85.         }
  86.  
  87.         public Forklift()
  88.         {
  89.             //Null constructor for internal class use only.
  90.         }
  91.  
  92.         public Forklift(int id)
  93.         {
  94.             GetForkliftByID(id);
  95.         }
  96.  
  97.         public static bool RemoveForkliftByID(int id)
  98.         {
  99.             try
  100.             {
  101.                 DBConnection.InitConnection();
  102.                 DBConnection.DBContext.SetSqlString("Inventory..RemoveForkliftByID", CommandType.StoredProcedure);
  103.                 DBConnection.DBContext.AddParameter("@ForkliftID", id, SqlDbType.Int);
  104.                 DBConnection.DBContext.GetResultSet();
  105.                 return true;
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 Console.WriteLine(ex.ToString());
  110.                 return false;
  111.             }
  112.         }
  113.  
  114.         public static Forklift UpdateForklift(Forklift forklift)
  115.         {
  116.             try
  117.             {
  118.                 DBConnection.InitConnection();
  119.                 DBConnection.DBContext.SetSqlString("Inventory..UpdateForklift", CommandType.StoredProcedure);
  120.                 DBConnection.DBContext.AddParameter("@ForkliftID", forklift.ForkliftID, SqlDbType.Int);
  121.                 DBConnection.DBContext.AddParameter("@SerialNumber", forklift.SerialNumber, SqlDbType.VarChar);
  122.                 DBConnection.DBContext.AddParameter("@ModelNumber", forklift.ModelNumber, SqlDbType.VarChar);
  123.                 DBConnection.DBContext.AddParameter("@ManufacturerID", forklift.ManufacturerID, SqlDbType.SmallInt);
  124.                 DBConnection.DBContext.AddParameter("@PowerID", forklift.Power, SqlDbType.Int);
  125.                 DBConnection.DBContext.AddParameter("@Capacity", forklift.Capacity, SqlDbType.Int);
  126.                 DBConnection.DBContext.AddParameter("@Year", forklift.Year, SqlDbType.SmallInt);
  127.                 DBConnection.DBContext.AddParameter("@LoweredHeight", forklift.LoweredHeight, SqlDbType.SmallInt);
  128.                 DBConnection.DBContext.AddParameter("@RaisedHeight", forklift.RaisedHeight, SqlDbType.SmallInt);
  129.                 DBConnection.DBContext.AddParameter("@PurchasedPrice", forklift.PurchasedPrice, SqlDbType.Decimal);
  130.                 DBConnection.DBContext.AddParameter("@WholesalePrice", forklift.WholesalePrice, SqlDbType.Decimal);
  131.                 DBConnection.DBContext.AddParameter("@RetailPrice", forklift.RetailPrice, SqlDbType.Decimal);
  132.                 DBConnection.DBContext.AddParameter("@Comments", forklift.Comments, SqlDbType.VarChar);
  133.                 DBConnection.DBContext.AddParameter("@LocationID", forklift.LocationID, SqlDbType.Int);
  134.                 DataSet resultSet = DBConnection.DBContext.GetResultSet();
  135.  
  136.                 if (resultSet != null)
  137.                 {
  138.                     DataTable resultTable = resultSet.Tables[0];
  139.                     if (resultTable.Rows.Count > 0)
  140.                     {
  141.                         foreach (DataRow dr in resultTable.Rows)
  142.                         {
  143.                             return Setup(dr);
  144.                         }
  145.                     }
  146.                 }
  147.                 return null;
  148.             }
  149.             catch (Exception ex)
  150.             {
  151.                 Console.WriteLine(ex.ToString());
  152.                 return null;
  153.             }
  154.         }
  155.  
  156.         public static Forklift GetForkliftByID(int id)
  157.         {
  158.             try
  159.             {
  160.                 DBConnection.InitConnection();
  161.                 DBConnection.DBContext.SetSqlString("Inventory..GetForkliftByID", CommandType.StoredProcedure);
  162.                 DBConnection.DBContext.AddParameter("@ForkliftID", id, SqlDbType.Int);
  163.                 DataSet resultSet = DBConnection.DBContext.GetResultSet();
  164.  
  165.                 if (resultSet != null)
  166.                 {
  167.                     DataTable resultTable = resultSet.Tables[0];
  168.                     if (resultTable.Rows.Count > 0)
  169.                     {
  170.                         foreach (DataRow dr in resultTable.Rows)
  171.                         {
  172.                             return Setup(dr);
  173.                         }
  174.                     }
  175.                 }
  176.                 return null;
  177.             }
  178.             catch (Exception ex)
  179.             {
  180.                 Debug.WriteLine(ex.ToString());
  181.                 return null;
  182.             }
  183.         }
  184.  
  185.         public static Forklift InsertForklift(Forklift forklift)
  186.         {
  187.             try
  188.             {
  189.                 DBConnection.InitConnection();
  190.                 DBConnection.DBContext.SetSqlString("Inventory..InsertForklift", CommandType.StoredProcedure);
  191.                 DBConnection.DBContext.AddParameter("@SerialNumber", forklift.SerialNumber, SqlDbType.VarChar);
  192.                 DBConnection.DBContext.AddParameter("@ModelNumber", forklift.ModelNumber, SqlDbType.VarChar);
  193.                 DBConnection.DBContext.AddParameter("@ManufacturerID", forklift.ManufacturerID, SqlDbType.SmallInt);
  194.                 DBConnection.DBContext.AddParameter("@PowerID", forklift.Power, SqlDbType.Int);
  195.                 DBConnection.DBContext.AddParameter("@Capacity", forklift.Capacity, SqlDbType.Int);
  196.                 DBConnection.DBContext.AddParameter("@Year", forklift.Year, SqlDbType.SmallInt);
  197.                 DBConnection.DBContext.AddParameter("@LoweredHeight", forklift.LoweredHeight, SqlDbType.SmallInt);
  198.                 DBConnection.DBContext.AddParameter("@RaisedHeight", forklift.RaisedHeight, SqlDbType.SmallInt);
  199.                 DBConnection.DBContext.AddParameter("@PurchasedPrice", forklift.PurchasedPrice, SqlDbType.Decimal);
  200.                 DBConnection.DBContext.AddParameter("@WholesalePrice", forklift.WholesalePrice, SqlDbType.Decimal);
  201.                 DBConnection.DBContext.AddParameter("@RetailPrice", forklift.RetailPrice, SqlDbType.Decimal);
  202.                 DBConnection.DBContext.AddParameter("@Comments", forklift.Comments, SqlDbType.VarChar);
  203.                 DBConnection.DBContext.AddParameter("@LocationID", forklift.LocationID, SqlDbType.Int);
  204.  
  205.                 DataSet resultSet = DBConnection.DBContext.GetResultSet();
  206.  
  207.                 if (resultSet != null)
  208.                 {
  209.                     DataTable resultTable = resultSet.Tables[0];
  210.                     if (resultTable.Rows.Count > 0)
  211.                     {
  212.                         foreach (DataRow dr in resultTable.Rows)
  213.                         {
  214.                             return Setup(dr);
  215.                         }
  216.                     }
  217.                 }
  218.                 return null;
  219.             }
  220.             catch (Exception ex)
  221.             {
  222.                 Debug.WriteLine(ex.ToString());
  223.                 return null;
  224.             }
  225.         }
  226.  
  227.         public static HashSet<Forklift> GetForkliftStockInventory()
  228.         {
  229.             try
  230.             {
  231.                 DBConnection.InitConnection();
  232.                 DBConnection.DBContext.SetSqlString("Inventory..GetForkliftStock", CommandType.StoredProcedure);
  233.                 DataSet resultSet = DBConnection.DBContext.GetResultSet();
  234.  
  235.                 HashSet<Forklift> forkliftStockInventory = new HashSet<Forklift>();
  236.  
  237.                 if (resultSet != null)
  238.                 {
  239.                     DataTable resultTable = resultSet.Tables[0];
  240.                    
  241.                     foreach (DataRow dr in resultTable.Rows)
  242.                     {
  243.                         forkliftStockInventory.Add(Setup(dr));
  244.                     }
  245.                 }
  246.                 return forkliftStockInventory;
  247.             }
  248.             catch (Exception ex)
  249.             {
  250.                 Debug.WriteLine(ex.ToString());
  251.                 return null;
  252.             }
  253.         }
  254.  
  255.         public static Image GetForkliftPicture(int id)
  256.         {
  257.             try
  258.             {
  259.                 DBConnection.InitConnection();
  260.                 DBConnection.DBContext.SetSqlString("Inventory..GetForkliftPicture", CommandType.StoredProcedure);
  261.                 DBConnection.DBContext.AddParameter("@ForkliftID", id, SqlDbType.Int);
  262.                 DataSet resultSet = DBConnection.DBContext.GetResultSet();
  263.  
  264.                 if (resultSet != null)
  265.                 {
  266.                     DataTable resultTable = resultSet.Tables[0];
  267.                     if (resultTable.Rows.Count > 0)
  268.                     {
  269.                         foreach (DataRow dr in resultTable.Rows)
  270.                         {
  271.                             using (MemoryStream ms = new MemoryStream((byte[])dr["Image"]))
  272.                             {
  273.                                 return Image.FromStream(ms);
  274.                             }
  275.                         }
  276.                     }
  277.                 }
  278.  
  279.                 return null;
  280.             }
  281.             catch (Exception ex)
  282.             {
  283.                 Debug.WriteLine(ex.ToString());
  284.                 return null;
  285.             }
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement