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> 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)); } }