public ListingInfo(ListingItem item, ListView list, Employee user) { InitializeComponent(); this.item = item; this.list = list; this.user = user; originalItem = item.originalItem; }
/// <summary> /// Populates the listing tab with ListingItems /// </summary> /// <param name="list">The listview that contains the items</param> private void FillListingTab(ListView list) { // Fill tab with all listings using (var context = new Model()) { var queryResult = context.Listings.SqlQuery("SELECT * FROM Listing").ToList(); foreach (Listing l in queryResult) { ListingItem newItem = new ListingItem(l.year_built, l.date_listed); newItem.id = l.id; newItem.Address = HelperFunctions.AddressToString(l.StreetAddress); newItem.Bedrooms = l.num_bedrooms; newItem.Bathrooms = l.num_bathrooms; newItem.Stories = l.num_stories; newItem.AskingPriceDecimal = l.asking_price; newItem.originalItem = l; list.Items.Add(newItem); } } }
private void SubmitListing(object sender, RoutedEventArgs e) { Int32 sellerID = 0; // If not existing client, have to make the client to get its ID, a seller client at this point if (clientSelected is false) { string fname = clientFirstName.Text; string lname = clientLastName.Text; string phone = clientPhoneNumber.Text; string email = clientEmail.Text; //need the ID of the last inserted row sellerID = HelperFunctions.AddNewClient(null, fname, lname, "Seller", phone, email); if (sellerID == -1) { MessageBox.Show("Unable to create client", "Incorrect Fields"); return; } } // otherwise, we have to just find the client's ID else { sellerID = clientIDs.ElementAt(existingClientList.SelectedIndex - 1); // If the client is only a buyer atm, upgrade to Both using (var context = new Model()) { var seller = context.Clients.SqlQuery("SELECT * FROM Client WHERE id = @id", new SqlParameter("id", sellerID)).FirstOrDefault <Client>(); if (seller.client_type.Equals("B")) { context.Database.ExecuteSqlCommand("UPDATE Client SET client_type = 'E' WHERE id = @id", new SqlParameter("id", sellerID)); clientTypeChanged = true; } } } // Next we have to make a new address int streetNum = -1; bool parseResult = int.TryParse(streetNumberField.Text, out streetNum); if (streetNum < 0) { MessageBox.Show("Street Address cannot be below 0", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } string streetName = streetNameField.Text; string city = cityField.Text; string postalcode = postalCodeField.Text.ToString(); // Check fields to make sure they arent null if (HelperFunctions.NullOrEmpty(streetName, city) || streetTypeBox.SelectedIndex == -1 || provinceBox.SelectedIndex == -1 || HelperFunctions.CheckPostalCode(postalcode) is false || parseResult is false) { MessageBox.Show("Address related fields have resulted in an error", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } string streetType = ((ComboBoxItem)streetTypeBox.SelectedItem).Content.ToString(); string province = ((ComboBoxItem)provinceBox.SelectedItem).Content.ToString(); // If not, we can make a new address and store it's ID as well. SqlParameter streetNumParam = new SqlParameter("num", streetNum); SqlParameter streetNameParam = new SqlParameter("name", streetName); SqlParameter streetTypeParam = new SqlParameter("type", streetType); SqlParameter cityParam = new SqlParameter("city", city); SqlParameter provinceParam = new SqlParameter("province", province); SqlParameter postalCodeParam = new SqlParameter("postalcode", postalcode); Object[] parameters = new object[] { streetNumParam, streetNameParam, streetTypeParam, cityParam, provinceParam, postalCodeParam }; Int32 addressID = 0; using (var context = new Model()) { context.Database.ExecuteSqlCommand("INSERT INTO StreetAddress (address_num, street, street_type, city, province_short, postal_code)" + "VALUES (@num, @name, @type, @city, @province, @postalcode)", parameters); // Get the foreign key ID var result = context.StreetAddresses.SqlQuery("SELECT * FROM StreetAddress WHERE ID = IDENT_CURRENT('StreetAddress')").FirstOrDefault <StreetAddress>(); addressID = result.id; } // Finally, we can make the listing with the left over information List <bool> parsedAttempts = new List <bool>(); byte numBedrooms = 0; byte numBathrooms = 0; byte numStories = 0; double squareFootage = -1; double lotSize = -1; parsedAttempts.Add(byte.TryParse(numberBedroomsField.Text, out numBedrooms)); parsedAttempts.Add(byte.TryParse(numberBathroomsField.Text, out numBathrooms)); parsedAttempts.Add(byte.TryParse(numberStoriesField.Text, out numStories)); double.TryParse(squareFootageField.Text, out squareFootage); double.TryParse(lotSizeField.Text, out lotSize); // If anything goes wrong in the parsing if (HelperFunctions.IntegersBelowZero(numBedrooms, numBathrooms, numStories) || parsedAttempts.Any(a => a is false)) { if (squareFootageField.Text.Equals("") is false && squareFootage < 0) { MessageBox.Show("House information fields are incorrect", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } if (lotSizeField.Text.Equals("") is false && lotSize < 0) { MessageBox.Show("House information fields are incorrect", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } MessageBox.Show("House information fields are incorrect", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } // squarefootage and lotsize information is optional so must be handled differently if (squareFootage > 0) { if (lotSize > 0) { } } bool hasGarage = (bool)hasGarageBox.IsChecked; DateTime yearBuilt = (DateTime)yearBuiltField.SelectedDate; Decimal askingPrice = -1; if (Decimal.TryParse(askingPriceField.Text, out askingPrice) is false || askingPrice < 0) { MessageBox.Show("Asking price was entered incorrectly", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } DateTime dateListed = DateTime.Today; // Insert into database using (var context = new Model()) { SqlParameter addressFK = new SqlParameter("addrFK", addressID); SqlParameter sellerFK = new SqlParameter("sellerFK", sellerID); SqlParameter askingPriceParam = new SqlParameter("askingprice", askingPrice); SqlParameter numBedParam = new SqlParameter("bed", numBedrooms); SqlParameter numBathParam = new SqlParameter("bath", numBathrooms); SqlParameter numStoriesParam = new SqlParameter("stories", numStories); SqlParameter hasGarageParam = new SqlParameter("garage", hasGarage); SqlParameter yearBuiltParam = new SqlParameter("yearbuilt", SqlDbType.DateTime); //optional yearBuiltParam.Value = yearBuilt; SqlParameter sqFootageParam = new SqlParameter("sqfootage", SqlDbType.Real); //optional sqFootageParam.Value = squareFootage; SqlParameter lotSizeParam = new SqlParameter("lotsize", SqlDbType.Real); //optional lotSizeParam.Value = lotSize; SqlParameter pictureParam = new SqlParameter("picture", SqlDbType.Image); //optional pictureParam.Value = selectedImageData; SqlParameter timestamp = new SqlParameter("timestamp", SqlDbType.DateTime); timestamp.Value = dateListed; yearBuiltParam.IsNullable = true; sqFootageParam.IsNullable = true; lotSizeParam.IsNullable = true; if (changedYearBuilt is false) { yearBuiltParam.Value = DBNull.Value; } if (squareFootage <= 0) { sqFootageParam.Value = DBNull.Value; } if (lotSize <= 0) { lotSizeParam.Value = DBNull.Value; } if (selectedImageData == null) { pictureParam.Value = DBNull.Value; } parameters = new object[] { addressFK, sellerFK, askingPriceParam, numBedParam, numBathParam, numStoriesParam, hasGarageParam, yearBuiltParam, sqFootageParam, lotSizeParam, pictureParam, timestamp }; try { context.Database.ExecuteSqlCommand("INSERT INTO Listing (address_id, seller_id, asking_price, num_bedrooms, num_bathrooms, " + "num_stories, has_garage, year_built, square_footage, lot_size, display_picture, date_listed)" + "VALUES (@addrFK, @sellerFK, @askingprice, @bed, @bath," + "@stories, @garage, @yearbuilt, @sqfootage, @lotsize, @picture, @timestamp)", parameters); } catch (Exception ex) { MessageBox.Show("Could not create listing", "Incorrect Fields"); // Get rid of the client if existing RemoveClient(sellerID); return; } // Get the record we just added var result = context.Listings.SqlQuery("SELECT * FROM Listing WHERE ID = IDENT_CURRENT('listing')").FirstOrDefault <Listing>(); // Construct the list item ListingItem newItem = new ListingItem(yearBuilt, dateListed); newItem.id = result.id; newItem.Address = HelperFunctions.AddressToString(result.StreetAddress); newItem.Bedrooms = result.num_bedrooms; newItem.Bathrooms = result.num_bathrooms; newItem.Stories = result.num_stories; newItem.AskingPriceDecimal = result.asking_price; newItem.originalItem = result; // Add it to the listview list.Items.Add(newItem); Close(); } }