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 <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> RemoveFloorplan(int id, int mediaId) { try { PropertyFloorplan media = await _db.PropertyFloorplans.SingleOrDefaultAsync(m => m.Id == mediaId); _db.Entry(media).State = EntityState.Deleted; await _db.SaveChangesAsync(); return(new Response(true, $"The floorplan has been removed.")); } catch (Exception ex) { return(await ErrorResponseAsync <BasePropertyController>($"Error removing floorplan from property.", ex)); } }
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)); } }