public static void Initialize(PropertiesContext context) { context.Database.EnsureCreated(); // Look for any Property. if (context.Property.Any()) { return; // DB has been seeded } var properties = new Property[] { new Property { Name = "Chief Joseph", AddressLine1 = "2025, NE Terre View, Pullman, WA, 99163", AddressLine2 = "C26", SpacesAvailable = 10, TotalSpaces = 350, Rent = 1200, Owner = new Landlord() { FirstName = "WSU" }, Description = "Nice college apartments" }, new Property { Name = "Columbia", AddressLine1 = "Blah Blah", AddressLine2 = "C26", SpacesAvailable = 15, TotalSpaces = 350, Rent = 1050, Owner = new Landlord() { FirstName = "WSU" }, Description = "Okay college apartments" }, }; }
/// <summary> /// Method to add property /// </summary> /// <param name="property">Property to add</param> public async Task <bool> AddProperty(Property newProperty, PropertiesContext context) { // Cannot have no name. if (newProperty.Name == null || newProperty.Name == string.Empty) { return(false); } // Cannot have empty address. if (newProperty.AddressLine1 == null || newProperty.AddressLine1 == string.Empty) { return(false); } // Rent cannot be negative or more than max integer. if (newProperty.Rent <= -1 || newProperty.Rent > 10000000) { return(false); } // ID cannot be negative or more than max integer. if (newProperty.ID <= -1 || newProperty.ID > 10000000) { return(false); } // Spaces available cannot be negative or more than max integer. if (newProperty.SpacesAvailable <= -1 || newProperty.SpacesAvailable > 10000000) { return(false); } // Total spaces cannot be negative or more than max integer. if (newProperty.TotalSpaces <= -1 || newProperty.TotalSpaces > 10000000) { return(false); } // Available spaces cannot be greater than total spaces, since that won't make sense. if (newProperty.SpacesAvailable > newProperty.TotalSpaces) { newProperty.SpacesAvailable = newProperty.TotalSpaces; } // Check if we already contain property with given name or the address. if (context.Property.Any(e => ((e.ID == newProperty.ID) || (e.Name == newProperty.Name) || (e.AddressLine1 == newProperty.AddressLine1)))) { return(false); } else { context.Property.Add(newProperty); await context.SaveChangesAsync(); return(true); } }
/// <summary> /// Method to delete property /// </summary> /// <param name="property">property to delete</param> public async Task <bool> DeleteProperty(Property property, PropertiesContext context) { if (property != null) { context.Property.Remove(property); await context.SaveChangesAsync(); return(true); } return(false); }
/// <summary> /// Method to edit the property /// </summary> /// <param name="property">property landlord wants to manage.</param> public async Task <bool> ManageProperties(Property newProperty, PropertiesContext context) { // Cannot have no name. if (newProperty.Name == null || newProperty.Name == string.Empty) { return(false); } // Cannot have empty address. if (newProperty.AddressLine1 == null || newProperty.AddressLine1 == string.Empty) { return(false); } // Rent cannot be negative or more than max integer. if (newProperty.Rent <= -1 || newProperty.Rent > 10000000) { return(false); } // ID cannot be negative or more than max integer. if (newProperty.ID <= -1 || newProperty.ID > 10000000) { return(false); } // Spaces available cannot be negative or more than max integer. if (newProperty.SpacesAvailable <= -1 || newProperty.SpacesAvailable > 10000000) { return(false); } // Total spaces cannot be negative or more than max integer. if (newProperty.TotalSpaces <= -1 || newProperty.TotalSpaces > 10000000) { return(false); } // Available spaces cannot be greater than total spaces, since that won't make sense. if (newProperty.SpacesAvailable > newProperty.TotalSpaces) { newProperty.SpacesAvailable = newProperty.TotalSpaces; } // Check if we already contain property with given name or the address. But avoid comparing to itself. if (context.Property.Any(e => ((e.ID != newProperty.ID) && ((e.Name == newProperty.Name) || (e.AddressLine1 == newProperty.AddressLine1))))) { return(false); } context.Attach(newProperty).State = EntityState.Modified; try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (context.Property.Any(e => e.ID == newProperty.ID)) { } else { throw; } return(false); } return(true); }