public bool TryUploadImage(string username, HttpPostedFileBase file) { string imageName = Path.GetFileName(file.FileName); if (imageName == null) { return(false); } //string imageExtension = imageName.Substring(imageName.IndexOf('.')); //imageName = imageName.Substring(0, imageName.IndexOf('.')) + "_" + User.Identity.Name + imageExtension; //string physicalPath = Server.MapPath("~/Images/ProfilePic"); //physicalPath = Path.Combine(physicalPath, imageName); //file.SaveAs(physicalPath); try { UserProfile user; using (var unitOfWork = new UnitOfWork()) { user = unitOfWork.UserProfileRepository.Get(entity => entity.UserName == username).FirstOrDefault(); var userExtendedProfile = user.UserExtendedProfile; if (userExtendedProfile == null) { userExtendedProfile = new UserExtendedProfile() { Id = user.Id, UserProfile = user, ImageUrl = imageName, }; unitOfWork.UserExtendedProfileRepository.Insert(userExtendedProfile); } else { userExtendedProfile.ImageUrl = imageName; unitOfWork.UserExtendedProfileRepository.Update(userExtendedProfile); } } } catch (Exception ex) { throw ex; } return(true); }
void IAccountService.AddUserExtendedProfile(string username, string almamater, string city, string dob, string profession, string qualifications, string description, IList <int> selectedTags) { using (var unitOfWork = new UnitOfWork()) { var userRepository = unitOfWork.UserProfileRepository; var user = userRepository.Get(entity => entity.UserName == username).FirstOrDefault(); var userExtendedProfile = new UserExtendedProfile { Id = user.Id, UserProfile = user, AlmaMater = almamater, City = city, DOB = dob, Profession = profession, Qualifications = qualifications, Description = description, Tags = new List <Tag>() }; var tagMap = unitOfWork.TagRepository.Get(_ => true).ToDictionary(x => x.Id, x => x); foreach (int tagId in selectedTags) { Tag selectedTag = tagMap[tagId]; userExtendedProfile.Tags.Add(selectedTag); selectedTag.Users.Add(userExtendedProfile); } if (user.UserExtendedProfile != null) { user.UserExtendedProfile.Tags = user.UserExtendedProfile.Tags.ToList(); userExtendedProfile.ImageUrl = user.UserExtendedProfile.ImageUrl; unitOfWork.UserExtendedProfileRepository.Update(userExtendedProfile); } else { unitOfWork.UserExtendedProfileRepository.Insert(userExtendedProfile); } user.UserExtendedProfile = userExtendedProfile; userRepository.Update(user); unitOfWork.Save(); } }
public ActionResult UploadImage(HttpPostedFileBase file) { if (file != null) { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { try { string imageName = Path.GetFileName(file.FileName); if (imageName == null) { return(RedirectToAction("UserExtendedProfile", "Account")); } // TODO : Image name needs to be hashed. string imageExtension = imageName.Substring(imageName.IndexOf('.')); imageName = imageName.Substring(0, imageName.IndexOf('.')) + "_" + User.Identity.Name + imageExtension; string physicalPath = Server.MapPath("~/Images/ProfilePic"); physicalPath = Path.Combine(physicalPath, imageName); // Saves the image to file system. file.SaveAs(physicalPath); UserProfile user = dbContext.UserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name); if (user == null) { throw new Exception("User not found."); } UserExtendedProfile profile = new UserExtendedProfile { Id = user.Id, UserProfile = user, ImageUrl = imageName }; if (user.UserExtendedProfile != null) { profile.UserProfile = user; profile.Id = user.UserExtendedProfile.Id; profile.AlmaMater = user.UserExtendedProfile.AlmaMater; profile.City = user.UserExtendedProfile.City; profile.DOB = user.UserExtendedProfile.DOB; profile.Profession = user.UserExtendedProfile.Profession; profile.Qualifications = user.UserExtendedProfile.Qualifications; profile.ImageUrl = imageName; profile.Update(dbContext); } else { dbContext.UserExtendedProfile.Add(profile); } dbContext.SaveChanges(); } // TODO : Exception printing stacktrace needs to be removed. catch (Exception e) { ModelState.AddModelError("", e.StackTrace); } } } return(RedirectToAction("UserExtendedProfile", "Account")); }
public ActionResult UserExtendedProfile(UserExtendedProfileModel model) { if (ModelState.IsValid) { // Extend the user's profile try { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile user = dbContext.UserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name); Dictionary <int, Tag> tagMap = dbContext.Tag.ToDictionary(x => x.Id, x => x); if (user == null) { // Log throw new Exception("User not found"); } UserExtendedProfile profile = new UserExtendedProfile { Id = user.Id, UserProfile = user, AlmaMater = model.AlmaMater, City = model.City, DOB = model.DOB, Profession = model.Profession, Qualifications = model.Qualifications, Description = model.Description, ImageUrl = string.Empty, Tags = new List <Tag>() }; foreach (int tagId in model.Tags) { Tag selectedTag = tagMap[tagId]; profile.Tags.Add(selectedTag); selectedTag.Users.Add(profile); } if (user.UserExtendedProfile != null) { foreach (Tag tag in user.UserExtendedProfile.Tags) { // This updates the tag } profile.ImageUrl = user.UserExtendedProfile.ImageUrl; profile.Update(dbContext); } else { dbContext.UserExtendedProfile.Add(profile); } UserProfile userProfile = new UserProfile { Id = user.Id, EmailId = user.EmailId, MobilePhone = user.MobilePhone, Country = user.Country, UserName = user.UserName, UserExtendedProfile = profile }; userProfile.Update(dbContext); dbContext.SaveChanges(); } return(RedirectToAction("Index", "Home")); } catch (Exception e) { // TODO : Exception printing stacktrace needs to be removed. ModelState.AddModelError("", e.StackTrace); } } ViewBag.TagList = Util.TagList; ViewBag.ProfessionList = new SelectList(Util.ListOfProfessions); // If we got this far, something failed, redisplay form return(View(model)); }