Advertisement
Guest User

frmCarsForSale

a guest
Jun 19th, 2023
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using CarLibrary;
  14.  
  15. namespace CarDealership
  16. {
  17.     public partial class frmCarsForSale : Form, IUser, IUtility
  18.     {
  19.         int SellerID { get; set; } = -1;
  20.  
  21.         frmUsers m_frmUsers;
  22.  
  23.         private Listing listing = new Listing();
  24.         private ListingDB listingDB = new ListingDB();
  25.  
  26.         private Car car;
  27.  
  28.         private Comments comments = new Comments();
  29.         private CommentsDB commentsDB = new CommentsDB();
  30.  
  31.         [Flags]
  32.         public enum ModifyingCarComponents
  33.         {
  34.             NONE = 0,
  35.             IS_MODIFYING_LISTING = 1,
  36.             IS_MODIFYING_CAR = 2,
  37.             IS_MODIFYING_COMMENTS = 3
  38.         }
  39.  
  40.         ModifyingCarComponents modifyingCarComponents = new ModifyingCarComponents();
  41.  
  42.         struct filterByStruct
  43.         {
  44.             // Grab the other structs then use commands to filter
  45.  
  46.             static private List<string> carMakes = ListingDB.GetCarPropertyFilteredByDistinctValues("CarMake", Program.sqlConnection);
  47.             static private List<string> carColors = ListingDB.GetCarPropertyFilteredByDistinctValues("CarColor", Program.sqlConnection);
  48.  
  49.             static private List<string> carAgeRanges = new List<string>
  50.             {
  51.                 "1969-",
  52.                 "1969-1994",
  53.                 "1994-2001",
  54.                 "2001-2012",
  55.                 "2012-2022",
  56.                 "2022+"
  57.             };
  58.  
  59.             static private List<string> carPriceRanges = new List<string>
  60.             {
  61.                 "$5,000-",
  62.                 "$5,000 - $9,999",
  63.                 "$10,000+"
  64.             };
  65.  
  66.             static public Dictionary<string, List<string>> filterByDictionary = new Dictionary<string, List<string>>
  67.             {
  68.                 { "Make", carMakes },
  69.                 { "Color", carColors },
  70.                 { "Age", carAgeRanges },
  71.                 {"Price", carPriceRanges }
  72.             };
  73.         }
  74.  
  75.         public frmCarsForSale()
  76.         {
  77.             InitializeComponent();
  78.         }
  79.  
  80.         public frmCarsForSale(int sellerID, frmUsers frmUsers)
  81.         {
  82.             SellerID = sellerID;
  83.             m_frmUsers = frmUsers;
  84.  
  85.             InitializeComponent();
  86.         }
  87.  
  88.         private void frmCarsForSale_Load(object sender, EventArgs e)
  89.         {
  90.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Comments' table. You can move, or remove it, as needed.
  91.             this.commentsTableAdapter.Fill(this.groupFinal266DataSet.Comments);
  92.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Listing' table. You can move, or remove it, as needed.
  93.             this.listingTableAdapter.Fill(this.groupFinal266DataSet.Listing);
  94.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Sellers' table. You can move, or remove it, as needed.
  95.             this.sellersTableAdapter.Fill(this.groupFinal266DataSet.Sellers);
  96.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Buyers' table. You can move, or remove it, as needed.
  97.             this.buyersTableAdapter.Fill(this.groupFinal266DataSet.Buyers);
  98.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Cars' table. You can move, or remove it, as needed.
  99.             this.carsTableAdapter.Fill(this.groupFinal266DataSet.Cars);
  100.  
  101.             UserAuthentication();
  102.  
  103.             SetUpFilterByComboBox();
  104.             SetUpCarMakeComboBox();
  105.  
  106.             sellerIDTextBox.Text = SellerID.ToString();
  107.  
  108.             LoadSampleCarComponents();
  109.         }
  110.  
  111.         private void LoadSampleCarComponents()
  112.         {
  113.             carVINTextBoxListingInfo.Text = "1HGCG1650Y1081994";
  114.             descriptionTextBox.Text = "Seller: Hoshi Kask; Car: 2000 Honda Accord marked at $2500";
  115.         }
  116.  
  117.         public void UserAuthentication()
  118.         {
  119.             UserDB userDB = new UserDB();
  120.             if (!userDB.VerifyLoginUser(SellerID, Program.sqlConnection)) {
  121.                 MessageBox.Show(userDB.MsgText, userDB.MsgCaption);
  122.                 Close();
  123.             }
  124.         }
  125.  
  126.         // Might be unnecessary
  127.         public void EnableControls(bool enable = true)
  128.         {
  129.             foreach (Control c in Controls)
  130.             {
  131.                 if (c is ComboBox || c is Button)
  132.                     c.Enabled = enable;
  133.             }
  134.         }
  135.  
  136.         private void SetUpFilterByComboBox()
  137.         {
  138.             filterByToolStripComboBox.ComboBox.Items.Add("Make");
  139.             filterByToolStripComboBox.ComboBox.Items.Add("Color");
  140.             filterByToolStripComboBox.ComboBox.Items.Add("Age");
  141.             filterByToolStripComboBox.ComboBox.Items.Add("Price");
  142.         }
  143.  
  144.         private void SetUpCarMakeComboBox()
  145.         {
  146.             List<string> carMakes = ListingDB.GetCarPropertyFilteredByDistinctValues("CarMake", Program.sqlConnection);
  147.             foreach (string carMake in carMakes)
  148.                 carMakeComboBox.Items.Add(carMake);
  149.         }
  150.  
  151.         private void filterByToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
  152.         {
  153.             carPropertyStripComboBox.ComboBox.Text = "";
  154.             carPropertyStripComboBox.ComboBox.SelectedItem = "";
  155.             carPropertyStripComboBox.ComboBox.Items.Clear();
  156.  
  157.             string filterProperty = filterByToolStripComboBox.ComboBox.SelectedItem.ToString();
  158.             carListingPropertyStripLabel.Text = filterProperty + ": ";
  159.  
  160.             foreach (string option in filterByStruct.filterByDictionary[filterProperty])
  161.                 carPropertyStripComboBox.ComboBox.Items.Add(option);
  162.         }
  163.  
  164.         private void viewAllToolStripButton_Click(object sender, EventArgs e)
  165.         {
  166.             SqlConnection sqlConnection = Program.sqlConnection;
  167.             // Get every car in database then remove it from there
  168.             // List<Car> cars = listingDB.GetAllCars(SellerID, sqlConnection);
  169.  
  170.             List<Car> cars = new List<Car>();
  171.  
  172.             switch (filterByToolStripComboBox.ComboBox.SelectedItem)
  173.             {
  174.                 case "Make":
  175.                     ListingDB.FilterByMake filterByMake = new ListingDB.FilterByMake();
  176.                     cars = filterByMake.FilterBy(cars, carPropertyStripComboBox.ComboBox.Text.ToString(),sqlConnection);
  177.                     break;
  178.                 case "Color":
  179.                     ListingDB.FilterByColor filterByColor = new ListingDB.FilterByColor();
  180.                     cars = filterByColor.FilterBy(cars, carPropertyStripComboBox.ComboBox.Text.ToString(),sqlConnection);
  181.                     break;
  182.                 case "Age":
  183.                     ListingDB.FilterByAge filterByAge = new ListingDB.FilterByAge();
  184.                     cars = filterByAge.FilterBy(cars, carPropertyStripComboBox.ComboBox.Text.ToString(),sqlConnection);
  185.                     break;
  186.                 case "Price":
  187.                     ListingDB.FilterByPrice filterByPrice = new ListingDB.FilterByPrice();
  188.                     cars = filterByPrice.FilterBy(cars, carPropertyStripComboBox.ComboBox.Text,sqlConnection);
  189.                     break;
  190.                 default:
  191.                     break;
  192.             }
  193.  
  194.             this.groupFinal266DataSet.Cars.Clear();
  195.  
  196.  
  197.             foreach (Car car in cars)
  198.                 this.groupFinal266DataSet.Cars.Rows.Add(car.carVIN, car.age, car.make, car.model, car.price, car.color, car.miles);
  199.  
  200.             this.groupFinal266DataSet.Listing.Rows.Clear();
  201.  
  202.             if (cars.Count < 1)
  203.                 return;
  204.  
  205.             List<Listing> listings = ListingDB.GetAllListings(Program.sqlConnection);
  206.  
  207.             string carVINs = String.Join("|", cars.Select(x => x.carVIN).ToArray());
  208.             carVINs = carVINs.Trim(new Char[] { '|' });
  209.  
  210.             listings.RemoveAll(l => l.carVIN.Any(c => !Regex.IsMatch(l.carVIN, carVINs)));
  211.  
  212.             foreach (Listing listing in listings)
  213.                 this.groupFinal266DataSet.Listing.Rows.Add(listing.listingID, listing.sellerID, listing.carVIN, listing.description, listing.creationDateTime);
  214.         }
  215.  
  216.         // This should be for uploading
  217.         public void AssignBusinessObjectDataToUpload()
  218.         {
  219.             // Listing
  220.             if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_LISTING)
  221.             {
  222.                 // Assigning auto incrementing values are just for validation checks to not throw error
  223.                 listing.listingID = 0;
  224.                 listing.sellerID = Convert.ToInt32(sellerIDTextBox.Text);
  225.                 listing.carVIN = carVINTextBoxListingInfo.Text;
  226.                 listing.description = descriptionTextBox.Text;
  227.                 listing.creationDateTime = Convert.ToDateTime(creationDateTimeDateTimePicker.Text);
  228.             }
  229.  
  230.             // Car? Are we supposed to add cars? It doesn't seem so.
  231.  
  232.             // Comments
  233.             // Probably should only be able to add comments for the seller that you're adding to
  234.             if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_COMMENTS)
  235.             {
  236.                 comments.CommentsID = 0;
  237.                 comments.ListingID = Convert.ToInt32(listingIDComboBox.Text);
  238.                 comments.CommentText = commentsRichTextBox.Text;
  239.             }
  240.  
  241.             modifyingCarComponents = ModifyingCarComponents.NONE;
  242.         }
  243.  
  244.         public bool ValidateBusinessObjectData()
  245.         {
  246.             try
  247.             {
  248.                 if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_LISTING)
  249.                 {
  250.                     foreach (PropertyInfo property in listing.GetType().GetProperties())
  251.                         if (property.GetValue(listing) == null || string.IsNullOrEmpty(property.GetValue(listing).ToString()))
  252.                             throw new ArgumentNullException(property.Name, char.ToUpper(property.Name[0]) + property.Name.Substring(1) + " not found");
  253.                 }
  254.  
  255.                 if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_COMMENTS)
  256.                 {
  257.                     foreach (PropertyInfo property in comments.GetType().GetProperties())
  258.                         if (property.GetValue(comments) == null || string.IsNullOrEmpty(property.GetValue(comments).ToString()))
  259.                             throw new ArgumentNullException(property.Name, char.ToUpper(property.Name[0]) + property.Name.Substring(1) + " not found");
  260.                 }
  261.  
  262.                 return true;
  263.             }
  264.             catch (Exception ex)
  265.             {
  266.                 MessageBox.Show(ex.Message, ex.GetType().ToString());
  267.             }
  268.  
  269.             return false;
  270.         }
  271.  
  272.         private void btnUpload_Click(object sender, EventArgs e)
  273.         {
  274.             modifyingCarComponents |= ModifyingCarComponents.IS_MODIFYING_LISTING;
  275.  
  276.             AssignBusinessObjectDataToUpload();
  277.  
  278.             if (!ValidateBusinessObjectData())
  279.                 return;
  280.  
  281.             if (!listingDB.Upload(listing, Program.sqlConnection)) {
  282.                 MessageBox.Show(listingDB.MsgText, listingDB.MsgCaption);
  283.                 return;
  284.             }
  285.  
  286.             // Why don't these work?
  287.             listingDataGridView.Update();
  288.             listingDataGridView.Refresh();
  289.         }
  290.  
  291.  
  292.         public void AssignBusinessObjectDataToDelete(int rowIndex)
  293.         {
  294.             if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_LISTING)
  295.             {
  296.                 listing.listingID = Convert.ToInt32(listingDataGridView.Rows[rowIndex].Cells[0].Value);
  297.                 listing.sellerID = Convert.ToInt32(listingDataGridView.Rows[rowIndex].Cells[1].Value);
  298.                 listing.carVIN = Convert.ToString(listingDataGridView.Rows[rowIndex].Cells[2].Value);
  299.                 listing.description = Convert.ToString(listingDataGridView.Rows[rowIndex].Cells[3].Value);
  300.                 listing.creationDateTime = Convert.ToDateTime(listingDataGridView.Rows[rowIndex].Cells[4].Value);
  301.             }
  302.  
  303.             if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_CAR)
  304.             {
  305.                 car = ListingDB.carsCreationDictionary[carMakeComboBox.Text].Invoke();
  306.             }
  307.  
  308.             if (modifyingCarComponents == ModifyingCarComponents.IS_MODIFYING_COMMENTS)
  309.             {
  310.                 comments.CommentsID = Convert.ToInt32(commentsDataGridView.Rows[rowIndex].Cells[0].Value);
  311.                 comments.CommentText = Convert.ToString(commentsDataGridView.Rows[rowIndex].Cells[1].Value);
  312.                 comments.ListingID = Convert.ToInt32(commentsDataGridView.Rows[rowIndex].Cells[2].Value);
  313.             }
  314.  
  315.             modifyingCarComponents = ModifyingCarComponents.NONE;
  316.         }
  317.  
  318.         private void listingDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
  319.         {
  320.             if (e.ColumnIndex == 5)
  321.             {
  322.                 modifyingCarComponents = ModifyingCarComponents.IS_MODIFYING_LISTING;
  323.  
  324.                 AssignBusinessObjectDataToDelete(e.RowIndex);
  325.  
  326.                 if (!ValidateBusinessObjectData())
  327.                     return;
  328.  
  329.                 if (!listingDB.Delete(listing, Program.sqlConnection)) {
  330.                     MessageBox.Show(listingDB.MsgText, listingDB.MsgCaption);
  331.                     return;
  332.                 }
  333.  
  334.                 listingDataGridView.Rows.RemoveAt(e.RowIndex);
  335.             }
  336.         }
  337.  
  338.         private void btnSubmitComment_Click(object sender, EventArgs e)
  339.         {
  340.             try
  341.             {
  342.                 if (string.IsNullOrEmpty(listingIDComboBox.Text))
  343.                     throw new ArgumentException("Please input a comment id", "Listing id not found");
  344.  
  345.                 if (string.IsNullOrEmpty(commentsRichTextBox.Text))
  346.                     throw new ArgumentException("Please input a comment", "Comment not found");
  347.  
  348.                 modifyingCarComponents |= ModifyingCarComponents.IS_MODIFYING_COMMENTS;
  349.  
  350.                 AssignBusinessObjectDataToUpload();
  351.  
  352.                 commentsDB.Upload(comments, Program.sqlConnection);
  353.             }
  354.             catch (Exception ex)
  355.             {
  356.                 MessageBox.Show(ex.Message, ex.GetType().ToString());
  357.             }
  358.         }
  359.  
  360.         private void commentsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
  361.         {
  362.             if (e.ColumnIndex == 3)
  363.             {
  364.                 modifyingCarComponents = ModifyingCarComponents.IS_MODIFYING_COMMENTS;
  365.  
  366.                 AssignBusinessObjectDataToDelete(e.RowIndex);
  367.  
  368.                 if (!ValidateBusinessObjectData())
  369.                     return;
  370.  
  371.                 if (!commentsDB.Delete(comments, Program.sqlConnection)) {
  372.                     MessageBox.Show(commentsDB.MsgText, commentsDB.MsgCaption);
  373.                     return;
  374.                 }
  375.  
  376.                 commentsDataGridView.Rows.RemoveAt(e.RowIndex);
  377.             }
  378.         }
  379.  
  380.         private void LogoutVerification()
  381.         {
  382.             EnableControls(false);
  383.  
  384.             if (MessageBox.Show("Y' sure?", "Log out", MessageBoxButtons.OKCancel) == DialogResult.OK)
  385.             {
  386.                 m_frmUsers.EnableControls();
  387.                 Close();
  388.             }
  389.             else
  390.             {
  391.                 EnableControls();
  392.             }
  393.         }
  394.  
  395.         private void btnLogOut_Click(object sender, EventArgs e)
  396.         {
  397.             LogoutVerification();
  398.         }
  399.     }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement