public virtual async Task <Response> RemoveMedia(string id, AttachMediaModel model) { try { // load the media object. ApplicationUser user = await _account.GetUserByIdAsync(id); MediaObject media = _db.Media.SingleOrDefault(m => m.Id == model.MediaId); switch (model.FieldName) { case nameof(Models.ApplicationUser.Avatar): user.Avatar = null; break; } await _db.SaveChangesAsync(); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BaseUsersController>($"Error removing a media file from an entity.", ex)); } }
public virtual async Task <Response> RemoveMedia(int id, AttachMediaModel model) { try { // load the media object. Content content = await _db.Content.Where(p => p.Id == id).FirstOrDefaultAsync(); MediaObject media = _db.Media.SingleOrDefault(m => m.Id == model.MediaId); string cacheKey = typeof(Content).ToString() + ".Single." + id; switch (model.FieldName) { case nameof(Models.Content.FeaturedImage): content.FeaturedImageJson = null; break; case nameof(Models.Content.ShareImage): content.ShareImageJson = null; break; } await _db.SaveChangesAsync(); _cache.Remove(cacheKey); return(new Response(true, MediaObject.Blank, $"The media file has been removed successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BaseContentController>($"Error removing a media file from an entity.", 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 <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 <Response> UploadMedia(string id, AttachMediaModel model) { try { model.ValidateOrThrow(); // load the media object. ApplicationUser user = await _account.GetUserByIdAsync(id); if (user == null) { throw new Exception("Could not load content 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.ApplicationUser.Avatar): user.Avatar = media; break; } await _db.SaveChangesAsync(); return(new Response(true, media, $"The media has been attached successfully.")); } catch (Exception ex) { return(await ErrorResponseAsync <BaseUsersController>($"Error attaching a media file to an entity.", ex)); } }