Exemplo n.º 1
0
        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"));
        }
Exemplo n.º 2
0
        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));
        }