public ActionResult UserPreferencesAbout() { try { using (var db = new whoaverseEntities()) { var userPreferences = db.Userpreferences.Find(User.Identity.Name); if (userPreferences != null) { // load existing preferences and return to view engine var tmpModel = new UserAboutViewModel() { Shortbio = userPreferences.Shortbio, Avatar = userPreferences.Avatar }; return(PartialView("_UserPreferencesAbout", tmpModel)); } else { var tmpModel = new UserAboutViewModel(); return(PartialView("_UserPreferencesAbout", tmpModel)); } } } catch (Exception) { return(new EmptyResult()); } }
public async Task <UserAboutViewModel> GetUserAbout() { return(await Task.Run(() => { var about = (from abt in _dbContext.UserDetails where abt.UserId == this.UserId select new { About = abt.About, UserId = abt.UserId, NickName = abt.Nickname }).FirstOrDefault(); var userAboutViewModel = new UserAboutViewModel() { UserId = this.UserId, About = about != null ? about.About : string.Empty, NickName = about != null ? about.NickName : string.Empty, UserSocialAddressList = new List <UserSocilaAddVM>() }; var userSocialAddressResult = SocialAddressRepository.Instance.GetSocialAddressAsync(this.UserId); userAboutViewModel.UserSocialAddressList.AddRange(userSocialAddressResult.Result); return userAboutViewModel; })); }
public ActionResult GetUserPreferencesAbout() { try { using (var db = new voatEntities()) { var userPreferences = GetUserPreference(db); var tmpModel = new UserAboutViewModel() { Bio = String.IsNullOrEmpty(userPreferences.Bio) ? STRINGS.DEFAULT_BIO : userPreferences.Bio, Avatar = userPreferences.Avatar }; return(PartialView("_UserPreferencesAbout", tmpModel)); } } catch (Exception) { return(new EmptyResult()); } }
public async Task <ActionResult> UserPreferencesAbout([Bind(Include = "Shortbio, Avatarfile")] UserAboutViewModel model) { // save changes using (var db = new whoaverseEntities()) { var userPreferences = db.Userpreferences.Find(User.Identity.Name); var tmpModel = new Userpreference(); if (userPreferences == null) { // create a new record for this user in userpreferences table tmpModel.Shortbio = model.Shortbio; tmpModel.Username = User.Identity.Name; } if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0) { // check uploaded file size is < 300000 bytes (300 kilobytes) if (model.Avatarfile.ContentLength < 300000) { try { using (var img = Image.FromStream(model.Avatarfile.InputStream)) { if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png)) { // resize uploaded file ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType); if (userPreferences == null) { tmpModel.Avatar = User.Identity.Name + ".jpg"; } else { userPreferences.Avatar = User.Identity.Name + ".jpg"; } } else { // uploaded file was invalid ModelState.AddModelError("", "Uploaded file is not recognized as an image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } } catch (Exception) { // uploaded file was invalid ModelState.AddModelError("", "Uploaded file is not recognized as an image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } else { // refuse to save the file and explain why ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge })); } } if (userPreferences == null) { db.Userpreferences.Add(tmpModel); await db.SaveChangesAsync(); } else { userPreferences.Shortbio = model.Shortbio; userPreferences.Username = User.Identity.Name; await db.SaveChangesAsync(); } } return(RedirectToAction("Manage")); }
public async Task <ActionResult> UserPreferencesAbout([Bind(Include = "Bio, Avatarfile")] UserAboutViewModel model) { ViewBag.UserName = User.Identity.Name; // save changes using (var db = new voatEntities()) { var userPreferences = GetUserPreference(db); if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0) { // check uploaded file size is < 300000 bytes (300 kilobytes) if (model.Avatarfile.ContentLength < 300000) { try { using (var img = Image.FromStream(model.Avatarfile.InputStream)) { if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png)) { // resize uploaded file var thumbnailResult = await ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType); if (thumbnailResult) { userPreferences.Avatar = User.Identity.Name + ".jpg"; } else { // unable to generate thumbnail ModelState.AddModelError("", "Uploaded file is not recognized as a valid image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } else { // uploaded file was invalid ModelState.AddModelError("", "Uploaded file is not recognized as an image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } } catch (Exception) { // uploaded file was invalid ModelState.AddModelError("", "Uploaded file is not recognized as an image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } else { // refuse to save the file and explain why ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge })); } } var bio = model.Bio.TrimSafe(); if (String.IsNullOrEmpty(bio)) { userPreferences.Bio = "I tried to delete my bio but they gave me this instead"; } else if (bio == STRINGS.DEFAULT_BIO) { userPreferences.Bio = null; } else { userPreferences.Bio = bio; } await db.SaveChangesAsync(); } ClearUserCache(); return(RedirectToAction("Manage")); }
public async Task <ActionResult> UserPreferencesAbout([Bind("Bio, Avatarfile")] UserAboutViewModel model) { if (!ModelState.IsValid) { return(View("Manage", model)); } ViewBag.UserName = User.Identity.Name; string avatarKey = null; //ThumbGenerator.GenerateThumbnail if (model.Avatarfile != null) { try { var stream = model.Avatarfile.OpenReadStream(); var result = await ThumbGenerator.GenerateAvatar(stream, model.Avatarfile.FileName, model.Avatarfile.ContentType); if (result.Success) { avatarKey = result.Response; } else { ModelState.AddModelError("Avatarfile", result.Message); return(View("Manage", model)); } } catch (Exception ex) { ModelState.AddModelError("Avatarfile", "Uploaded file is not recognized as a valid image."); return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat })); } } var bio = model.Bio.TrimSafe(); //This is a hack var context = new VoatOutOfRepositoryDataContextAccessor(); using (var repo = new Repository(User, context)) { var p = await repo.GetUserPreferences(User.Identity.Name); if (bio != p.Bio) { if (String.IsNullOrEmpty(bio)) { p.Bio = "I tried to delete my bio but they gave me this instead"; } else if (bio == STRINGS.DEFAULT_BIO) { p.Bio = null; } else { p.Bio = bio; } } if (!String.IsNullOrEmpty(avatarKey)) { p.Avatar = avatarKey; } await context.SaveChangesAsync(); } /* * using (var db = new VoatUIDataContextAccessor()) * { * var userPreferences = GetUserPreference(db); * * if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0) * { * // check uploaded file size is < 300000 bytes (300 kilobytes) * if (model.Avatarfile.ContentLength < 300000) * { * try * { * using (var img = Image.FromStream(model.Avatarfile.InputStream)) * { * if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png)) * { * // resize uploaded file * var thumbnailResult = await ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType); * if (thumbnailResult) * { * userPreferences.Avatar = User.Identity.Name + ".jpg"; * } * else * { * // unable to generate thumbnail * ModelState.AddModelError("", "Uploaded file is not recognized as a valid image."); * return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }); * } * } * else * { * // uploaded file was invalid * ModelState.AddModelError("", "Uploaded file is not recognized as an image."); * return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }); * } * } * } * catch (Exception) * { * // uploaded file was invalid * ModelState.AddModelError("", "Uploaded file is not recognized as an image."); * return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }); * } * } * else * { * // refuse to save the file and explain why * ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image."); * return RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge }); * } * } * * var bio = model.Bio.TrimSafe(); * * if (String.IsNullOrEmpty(bio)) * { * userPreferences.Bio = "I tried to delete my bio but they gave me this instead"; * } * else if (bio == STRINGS.DEFAULT_BIO) * { * userPreferences.Bio = null; * } * else * { * userPreferences.Bio = bio; * } * await db.SaveChangesAsync(); * } */ ClearUserCache(); return(RedirectToAction("Manage")); }
public async Task <IActionResult> About(UserAboutViewModel userAboutVM) { return(await Task.Run(() => { var about = _profileDbContext.UserDetails.ToList().Find(select => select.UserId == UserId); if (about != null) { about.About = userAboutVM.About; about.Nickname = userAboutVM.NickName; _profileDbContext.UserDetails.Update(about); } else { _profileDbContext.UserDetails.Add(new UserDetail { UserId = UserId, About = userAboutVM.About, Nickname = userAboutVM.NickName }); } userAboutVM.UserSocialAddressList.ForEach(val => { var socialAddressId = _profileDbContext.SocialAddress.FirstOrDefault(v => v.SocialName == val.SocialName).Id; if (val.UserSocialAddressId.ToString() == new Guid().ToString()) { _profileDbContext.UserSocialAddress.Add( new UserSocialAddress { Status = Convert.ToInt32(StatusEnum.Active), SocialUserName = val.SocialUserName, UserId = UserId, SocialAddressId = socialAddressId }); } else { if (string.IsNullOrEmpty(val.SocialUserName)) { var toDelete = _profileDbContext.UserSocialAddress.Where(v => v.Id.ToString() == val.UserSocialAddressId.ToString()).FirstOrDefault(); if (toDelete != null) { _profileDbContext.UserSocialAddress.Remove(toDelete); } } else { var toUpdate = _profileDbContext.UserSocialAddress.Where(v => v.Id.ToString() == val.UserSocialAddressId.ToString()).FirstOrDefault(); if (toUpdate != null) { toUpdate.SocialUserName = val.SocialUserName; toUpdate.Status = Convert.ToInt32(StatusEnum.Active); _profileDbContext.UserSocialAddress.Update(toUpdate); } } } }); _profileDbContext.SaveChanges(); userAboutVM.UserSocialAddressList.Clear(); userAboutVM.UserSocialAddressList.AddRange(SocialAddressRepository.Instance.GetSocialAddressAsync(this.UserId).Result); return View(userAboutVM); })); }