public async Task <PropertyListing> AddAsync(PropertyListing property) { _db.Properties.Add(property); await _db.SaveChangesAsync(); return(property); }
public async Task DeleteAsync(int id) { PropertyListing property = await _db.Properties .Include(p => p.Metadata) .Include(p => p.Media) .Include(p => p.FloorPlans) .Where(p => p.Id == id) .FirstOrDefaultAsync(); property.Media.ForEach(async m => { try { await _media.DeleteStoredMedia(m); } catch (Exception) { } _db.Entry(m).State = EntityState.Deleted; }); property.FloorPlans.ForEach(async m => { try { await _media.DeleteStoredMedia(m); } catch (Exception) { } _db.Entry(m).State = EntityState.Deleted; }); property.Metadata.ForEach(m => { _db.Entry(m).State = EntityState.Deleted; }); await _db.SaveChangesAsync(); _db.Entry(property).State = EntityState.Deleted; await _db.SaveChangesAsync(); ClearPropertyCache(id); }
public async Task UpdateAsync(PropertyListing property) { _db.Entry(property).State = EntityState.Modified; await _db.SaveChangesAsync(); ClearPropertyCache(property.Id); }
public async Task ClearFieldAsync(int id, string field) { PropertyListing property = _db.Properties.FirstOrDefault(c => c.Id == id); _db.Entry(property).Property(field).CurrentValue = null; await _db.SaveChangesAsync(); ClearPropertyCache(id); }
public async Task SetStatusAsync(int id, ContentStatus status) { PropertyListing property = _db.Properties.Where(p => p.Id == id).FirstOrDefault(); property.Status = status; await _db.SaveChangesAsync(); ClearPropertyCache(property.Id); }
public virtual async Task <IActionResult> Edit(int id) { PropertyListing model = await _property.GetPropertyByIdAsync(id, true); model = await LoadAgents(model); model.AutoGeocode = true; return(View(model)); }
protected virtual async Task <PropertyListing> LoadAgents(PropertyListing listing) { IList <ApplicationUser> admins = await _account.GetUsersInRole("Admin"); IList <ApplicationUser> editors = await _account.GetUsersInRole("Editor"); listing.AvailableAgents = editors.Concat(admins).Distinct().OrderBy(u => u.FirstName).ThenBy(u => u.Email).ToList(); return(listing); }
public virtual IActionResult Create() { PropertyListing model = new PropertyListing() { PublishDate = DateTime.UtcNow, AgentId = User.GetLocalUserId() }; return(View("_Blade_Property", model)); }
public async Task <PropertyListing> GetPropertyByIdAsync(int id, bool nocache = false) { PropertyListing property = await _db.Properties .Include(p => p.Media) .Include(p => p.FloorPlans) .Include(p => p.Agent) .Include(p => p.Metadata) .AsNoTracking() .FirstOrDefaultAsync(c => c.Id == id); return(property); }
public async Task AddFloorplanAsync(PropertyListing property, PropertyFloorplan media) { if (property.FloorPlans == null) { property.FloorPlans = new List <PropertyFloorplan>(); } property.FloorPlans.Add(media); _db.Properties.Update(property); await _db.SaveChangesAsync(); ClearPropertyCache(property.Id); }
public async Task AddMediaAsync(PropertyListing property, PropertyMedia media) { if (property.Media == null) { property.Media = new List <PropertyMedia>(); } property.Media.Add(media); _db.Properties.Update(property); await _db.SaveChangesAsync(); ClearPropertyCache(property.Id); }
public async Task <PropertyListing> ReloadReferences(PropertyListing property) { _db.Entry(property).State = EntityState.Added; await _db.Entry(property).Collection(s => s.Media).LoadAsync(); await _db.Entry(property).Collection(s => s.FloorPlans).LoadAsync(); await _db.Entry(property).Reference(s => s.Agent).LoadAsync(); await _db.Entry(property).Collection(s => s.Metadata).LoadAsync(); return(property); }
public async Task <PropertyListingListViewModel> Handle(CreatePropertyListingCommand request, CancellationToken cancellationToken) { var contact = new ListingContact(request.ContactName, request.ContactTel, request.ContactEmail, request.ContactSMS, request.ContactOthers); //var listedProperty = _context.RentalProperty.FirstOrDefault(p => p.Id == request.RentalPropertyId); var listing = new PropertyListing(request.Title, request.ListingDesc, contact, request.RentalPropertyId, false, request.MonthlyRent, request.Notes, DateTime.Now, DateTime.Now); var listingProeprty = _context.RentalProperty.FirstOrDefault(p => p.Id == request.RentalPropertyId); await _context.AddAsync(listing); var addedListing = new PropertyListingListViewModel(); addedListing.Title = request.Title; addedListing.ListingDesc = request.ListingDesc; addedListing.MonthlyRent = request.MonthlyRent; addedListing.ListingNote = request.Notes; addedListing.IsActive = false; // this represents the status of listing: published or not addedListing.PropertyName = listingProeprty.PropertyName; addedListing.PropertyType = listingProeprty.PropertyType; addedListing.PropertyBuildYear = listingProeprty.PropertyBuildYear; addedListing.IsShared = listingProeprty.IsShared; addedListing.IsBasementSuite = listingProeprty.IsBasementSuite; addedListing.Created = DateTime.Now; addedListing.Updated = DateTime.Now; try { await _context.SaveChangesAsync(); // Send message to message queue addedListing.Id = listing.Id; } catch (Exception ex) { throw ex; } return(addedListing); //throw new NotImplementedException(); }
public async Task <PropertyListing> RemoveFloorplanAsync(int id, int mediaId) { PropertyListing property = await _db.Properties.Include(p => p.FloorPlans).SingleOrDefaultAsync(p => p.Id == id); PropertyFloorplan media = property.FloorPlans.SingleOrDefault(m => m.Id == mediaId); if (media != null) { property.FloorPlans.Remove(media); } await _db.SaveChangesAsync(); return(property); }
public virtual async Task <Response> UploadFloorplan(List <int> media, int id) { try { PropertyListing property = await _db.Properties .Include(p => p.FloorPlans) .FirstOrDefaultAsync(c => c.Id == id); if (property == null) { throw new Exception("Property not found!"); } if (media != null) { if (media.Count == 0) { throw new Exception("There are no files selected!"); } var directory = await _property.GetDirectoryAsync(); foreach (int mediaId in media) { // load the media object from db MediaObject mediaObject = _db.Media.AsNoTracking().SingleOrDefault(m => m.Id == mediaId); if (media == null) { throw new Exception("Could not load media to attach."); } var propertyMedia = new PropertyFloorplan(mediaObject); propertyMedia.PropertyId = property.Id; propertyMedia.Id = 0; _db.PropertyFloorplans.Add(propertyMedia); await _db.SaveChangesAsync(); } } return(new Response(true, "The media has been attached successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BasePropertyController>($"Error uploading a floorplan.", ex)); } }
public virtual async Task <Response> UploadMedia(int id, AttachMediaModel model) { try { model.ValidateOrThrow(); // load the media object. PropertyListing property = await _db.Properties.Where(p => p.Id == id).FirstOrDefaultAsync(); if (property == null) { throw new Exception("Could not load property to attach media."); } MediaObject media = _db.Media.SingleOrDefault(m => m.Id == model.MediaId); if (media == null) { throw new Exception("Could not load media to attach."); } switch (model.FieldName) { case nameof(Models.PropertyListing.FeaturedImage): property.FeaturedImage = new PropertyMedia(media); break; case nameof(Models.PropertyListing.InfoDownload): property.InfoDownload = new PropertyMedia(media); break; } await _db.SaveChangesAsync(); string cacheKey = typeof(Content).ToString() + ".Single." + id; _cache.Remove(cacheKey); return(new Response(true, media, $"The media has been attached successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BasePropertyController>($"Error attaching a media file to an entity.", ex)); } }
public virtual async Task <IActionResult> AddMeta(int id, string name) { try { PropertyListing property = await _db.Properties.AsNoTracking().SingleOrDefaultAsync(p => p.Id == id); if (property == null) { throw new Exception("Property not found."); } int?count = await _db.PropertyMetadata.Where(m => m.Name.Contains($"{name}")).CountAsync(); if (!count.HasValue) { count = 0; } PropertyMeta meta = new PropertyMeta() { PropertyId = property.Id, Name = name, Type = "System.String" }; meta.SetValue(""); _db.Add(meta); await _db.SaveChangesAsync(); SaveMessage = $"Successfully added new field: {meta.Name}."; MessageType = AlertType.Success; } catch (Exception ex) { SaveMessage = $"Error adding a property meta: {name}."; MessageType = AlertType.Danger; await _logService.AddExceptionAsync <BasePropertyController>(SaveMessage, ex); } return(RedirectToAction(nameof(Edit), new { id })); }
public virtual async Task <Response> RemoveMedia(int id, AttachMediaModel model) { try { // load the media object. PropertyListing property = await _db.Properties.Where(p => p.Id == id).FirstOrDefaultAsync(); if (property == null) { throw new Exception("Could not load property to remove media."); } MediaObject media = _db.Media.SingleOrDefault(m => m.Id == model.MediaId); switch (model.FieldName) { case nameof(Models.PropertyListing.FeaturedImage): property.FeaturedImageJson = null; break; case nameof(Models.PropertyListing.InfoDownload): property.InfoDownload = null; break; } await _db.SaveChangesAsync(); string cacheKey = typeof(PropertyListing).ToString() + ".Single." + id; _cache.Remove(cacheKey); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BasePropertyController>($"Error removing a media file from an entity.", ex)); } }
public virtual async Task <ActionResult> Edit(PropertyListing model) { try { var modelToUpdate = await _property.GetPropertyByIdAsync(model.Id, true); var updatedFields = Request.Form.Keys.ToHashSet(); modelToUpdate = modelToUpdate.UpdateFromFormModel(model, updatedFields); modelToUpdate = await _property.ReloadReferences(modelToUpdate); modelToUpdate.LastEditedBy = User.Identity.Name; modelToUpdate.LastEditedOn = DateTime.UtcNow; if (model.AutoGeocode) { var StatusMessage = ""; try { try { GoogleAddress address = _address.GeocodeAddress(modelToUpdate); if (address != null) { modelToUpdate.SetLocation(address.Coordinates); } } catch (Exception ex) { if (ex.InnerException != null) { throw ex.InnerException; } StatusMessage = "There was an error GeoLocating the property."; ModelState.AddModelError("GeoCoding", StatusMessage); await _logService.AddExceptionAsync <BasePropertyController>(StatusMessage, ex, LogType.Warning); } } catch (GoogleGeocodingException ex) { switch (ex.Status) { case GoogleStatus.RequestDenied: StatusMessage = "There was an error with the Google API [RequestDenied] this means your API account is not activated for Geocoding Requests."; ModelState.AddModelError("GeoCoding", StatusMessage); await _logService.AddExceptionAsync <BasePropertyController>(StatusMessage, ex, LogType.Warning); break; case GoogleStatus.OverQueryLimit: StatusMessage = "There was an error with the Google API [OverQueryLimit] this means your API account is has run out of Geocoding Requests."; ModelState.AddModelError("GeoCoding", StatusMessage); await _logService.AddExceptionAsync <BasePropertyController>(StatusMessage, ex, LogType.Warning); break; default: StatusMessage = "There was an error with the Google API [" + ex.Status.ToString() + "]: " + ex.Message; ModelState.AddModelError("GeoCoding", StatusMessage); await _logService.AddExceptionAsync <BasePropertyController>(StatusMessage, ex, LogType.Warning); break; } } } string type = Engine.Settings.Property.GetPlanningFromType(modelToUpdate.Planning); if (modelToUpdate.HasMeta("PlanningDescription")) { modelToUpdate.UpdateMeta("PlanningDescription", type); } else { if (modelToUpdate.Metadata == null) { modelToUpdate.Metadata = new List <PropertyMeta>(); } modelToUpdate.AddMeta("PlanningDescription", type); } foreach (KeyValuePair <string, Microsoft.Extensions.Primitives.StringValues> val in Request.Form) { if (val.Key.StartsWith("Meta:")) { if (modelToUpdate.HasMeta(val.Key.Replace("Meta:", ""))) { modelToUpdate.UpdateMeta(val.Key.Replace("Meta:", ""), val.Value.ToString()); } else { modelToUpdate.AddMeta(val.Key.Replace("Meta:", ""), val.Value.ToString()); } } } await _property.UpdateAsync(modelToUpdate); SaveMessage = "Saved"; MessageType = AlertType.Success; modelToUpdate = await LoadAgents(modelToUpdate); return(View(modelToUpdate)); } catch (Exception ex) { await _logService.AddExceptionAsync <BasePropertyController>("Error saving a property listing.", ex); SaveMessage = "An error occurred: " + ex.Message; MessageType = AlertType.Danger; model = await _property.ReloadReferences(model); model = await LoadAgents(model); return(View(model)); } }
public virtual async Task <Response> Clear(MediaListModel model) { try { // load the media object. string cacheKey; switch (model.Entity) { case nameof(Models.Content): // create the new media item for content => int contentId = int.Parse(model.Id); Content content = await _db.Content.Where(p => p.Id == contentId).FirstOrDefaultAsync(); switch (model.Field) { case nameof(Models.Content.FeaturedImage): content.FeaturedImageJson = null; break; case nameof(Models.Content.ShareImage): content.ShareImageJson = null; break; } await _db.SaveChangesAsync(); cacheKey = typeof(Content).ToString() + ".Single." + contentId; _cache.Remove(cacheKey); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); case nameof(ApplicationUser): // create the new media item for content => ApplicationUser user = await _db.Users.Where(p => p.Id == model.Id).FirstOrDefaultAsync(); switch (model.Field) { case nameof(ApplicationUser.Avatar): user.AvatarJson = null; break; } cacheKey = typeof(ApplicationUser).ToString() + ".Single." + model.Id; _cache.Remove(cacheKey); await _db.SaveChangesAsync(); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); case nameof(PropertyListing): int propertyId = int.Parse(model.Id); PropertyListing property = await _db.Properties.Where(p => p.Id == propertyId).FirstOrDefaultAsync(); switch (model.Field) { case nameof(PropertyListing.FeaturedImage): property.FeaturedImageJson = null; break; case nameof(PropertyListing.InfoDownload): property.InfoDownloadJson = null; break; } await _db.SaveChangesAsync(); cacheKey = typeof(PropertyListing).ToString() + ".Single." + propertyId; _cache.Remove(cacheKey); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); default: throw new Exception($"No entity supplied to remove from."); } } catch (Exception ex) { return(await ErrorResponseAsync <BaseMediaController>($"Error removing a media file from an entity.", ex)); } }
public virtual async Task <IActionResult> FloorPlans(int id) { PropertyListing model = await _property.GetPropertyByIdAsync(id, true); return(View("_List_PropertyFloorplans", model)); }
public virtual async Task <IActionResult> Gallery(int id) { PropertyListing model = await _property.GetPropertyByIdAsync(id, true); return(View("_List_PropertyMedia", model)); }
public virtual async Task <Response> Create(PropertyListing model) { try { model.AgentId = User.GetLocalUserId(); model.CreatedBy = User.Identity.Name; model.CreatedOn = DateTime.UtcNow; model.LastEditedBy = User.Identity.Name; model.LastEditedOn = DateTime.UtcNow; model.Confidential = false; model.Featured = false; model.AskingPrice = 0; model.Fees = 0; model.Rent = 0; model.Premium = 0; model.AskingPriceDisplay = "{0}"; model.FeesDisplay = "{0}"; model.RentDisplay = "{0}"; model.PremiumDisplay = "{0}"; model.ShareCount = 0; model.Views = 0; List <string> leaseStatuses = _propertySettings.GetLeaseStatuses(); if (leaseStatuses.Count > 0) { model.LeaseStatus = leaseStatuses.FirstOrDefault(); } else { model.LeaseStatus = "Available"; } Dictionary <string, string> planningTypes = _propertySettings.GetPlanningTypes(); if (planningTypes.Count > 0) { model.Planning = planningTypes.FirstOrDefault().Key; } else { model.Planning = "VAR"; } List <string> listingTypes = _propertySettings.GetListingTypes(); if (listingTypes.Count > 0) { model.ListingType = listingTypes.FirstOrDefault(); } else { model.ListingType = "Not Specified"; } // Geocode Geocoding.Google.GoogleAddress address = _address.GeocodeAddress(model); if (address != null) { model.SetLocation(address.Coordinates); } await _property.AddAsync(model); if (model.Metadata == null) { model.Metadata = new List <PropertyMeta>(); } model.UpdateMeta("PlanningDescription", Engine.Settings.Property.GetPlanningTypes().FirstOrDefault().Value); for (int i = 0; i < 11; i++) { model.AddMeta("Feature" + i.ToString(), "", "System.String"); } await _property.UpdateAsync(model); return(new Response(true, "Created successfully."));; } catch (Exception ex) { return(await ErrorResponseAsync <BasePropertyController>($"Error creating a new property.", ex)); } }
public virtual async Task <Response> Attach(MediaListModel model) { try { // load the media object. MediaObject media = _db.Media.SingleOrDefault(m => m.Id == model.MediaId); string cacheKey; switch (model.Entity) { case "Content": // create the new media item for content => int contentId = int.Parse(model.Id); Content content = await _db.Content.Where(p => p.Id == contentId).FirstOrDefaultAsync(); switch (model.Field) { case "FeaturedImage": content.FeaturedImage = new ContentMedia(media); break; case "ShareImage": content.ShareImage = new ContentMedia(media); break; } cacheKey = typeof(Content).ToString() + ".Single." + contentId; _cache.Remove(cacheKey); break; case "ApplicationUser": // create the new media item for content => ApplicationUser user = await _db.Users.Where(p => p.Id == model.Id).FirstOrDefaultAsync(); switch (model.Field) { case "Avatar": user.Avatar = media; break; } cacheKey = typeof(ApplicationUser).ToString() + ".Single." + model.Id; _cache.Remove(cacheKey); break; case "PropertyListing": int propertyId = int.Parse(model.Id); PropertyListing property = await _db.Properties.Where(p => p.Id == propertyId).FirstOrDefaultAsync(); switch (model.Field) { case "FeaturedImage": property.FeaturedImage = media; break; case "InfoDownload": property.InfoDownload = media; break; } cacheKey = typeof(PropertyListing).ToString() + ".Single." + propertyId; _cache.Remove(cacheKey); break; default: throw new Exception("No field set to attach the media item to."); } await _db.SaveChangesAsync(); return(new Response(true, media, $"The media has been set for attached successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BaseMediaController>($"Error attaching a media file to an entity.", ex)); } }