Esempio n. 1
0
        public JsonResult ProcessRequest(RequestPostModel requestPageData)
        {
            bool success = true;

            try
            {
                RequestState state = (RequestState)Enum.Parse(typeof(RequestState), requestPageData.State, true);
                if (state == RequestState.Pending)
                {
                    return(Json(new JavaScriptSerializer().Serialize(new { success })));
                }
                using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext())
                {
                    Requests request    = dbContext.Requests.Where(r => r.Id == requestPageData.RequestId).FirstOrDefault();
                    Requests newRequest = new Requests
                    {
                        Id    = request.Id,
                        From  = request.From,
                        To    = request.To,
                        Time  = request.Time,
                        State = (RequestState)Enum.Parse(typeof(RequestState), requestPageData.State, true)
                    };

                    newRequest.Update(dbContext);
                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write("Failed with following ex : {0}", ex.ToString());
                success = false;
            }

            return(Json(new JavaScriptSerializer().Serialize(new { success })));
        }
Esempio n. 2
0
        private bool TrySaveRequest(int profileId, UserProfile fromUser, UserProfileDatabaseContext dbContext, out int?socialId)
        {
            socialId = -1;
            try
            {
                UserProfile toUser           = dbContext.UserProfiles.FirstOrDefault(p => p.Id == profileId);
                bool        dublicateRequest = dbContext.Requests.Where(r => r.To.Id == toUser.Id && r.From.Id == fromUser.Id).Any();
                if (dublicateRequest)
                {
                    return(false);
                }
                Requests request = new Requests
                {
                    To    = toUser,
                    From  = fromUser,
                    Time  = DateTime.UtcNow,
                    State = RequestState.Pending
                };

                dbContext.Requests.Add(request);
                dbContext.SaveChanges();
                socialId = request.Id;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write("Faild with following ex : {0}", ex.ToString());

                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        public ActionResult SignUp(SignUpModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext())
                    {
                        UserProfile user = new UserProfile
                        {
                            UserName    = model.UserName,
                            Country     = model.Country,
                            EmailId     = model.EmailId,
                            MobilePhone = model.MobilePhone
                        };

                        dbContext.UserProfiles.Add(user);
                        dbContext.SaveChanges();
                    }

                    WebSecurity.CreateAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    //return RedirectToAction("UserExtendedProfile", "Account");
                    return(RedirectToAction("Index", "Home"));
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
            ViewBag.Countries = new SelectList(Util.ListOfCountries());
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 4
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"));
        }
Esempio n. 5
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));
        }
Esempio n. 6
0
        private bool TrySaveSocialType(int profileId, SocialTypes socialType)
        {
            bool success = false;

            try
            {
                using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext())
                {
                    // Get the SocialManager handle first
                    UserProfile   currentUser  = dbContext.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name);
                    SocialManager socialRecord = dbContext.SocialManager.Where(x => x.SocialType == socialType && x.User.Id == currentUser.Id).FirstOrDefault();
                    DateTime      currentTime  = DateTime.UtcNow;
                    int           days         = socialRecord != null ? ((TimeSpan)currentTime.Subtract((DateTime)socialRecord.TimeStamp)).Days : 0;
                    int?          socialId;

                    if (days <= ThresholdDays && socialRecord != null)
                    {
                        if (socialRecord.Count < ThresholdSocialTypeCount)
                        {
                            success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId);
                            if (!success)
                            {
                                return(success);
                            }
                            socialRecord.Count++;
                            socialRecord.Update(dbContext);
                        }
                        else
                        {
                            // No. of counts exceeded for the given period.
                            success = false;
                        }
                    }
                    else
                    {
                        success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId);
                        if (!success)
                        {
                            return(success);
                        }

                        if (socialRecord != null)
                        {
                            socialRecord.TimeStamp = DateTime.UtcNow;
                            socialRecord.SocialId  = socialId.GetValueOrDefault();
                            socialRecord.Count     = 1;
                            socialRecord.Update(dbContext);
                        }
                        else
                        {
                            SocialManager newSocialRecord = new SocialManager
                            {
                                User       = currentUser,
                                SocialId   = socialId.GetValueOrDefault(),
                                SocialType = socialType,
                                TimeStamp  = DateTime.UtcNow,
                                Count      = 1,
                            };

                            dbContext.SocialManager.Add(newSocialRecord);
                        }
                    }

                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write("Operation failed with following ex : {0}", ex.ToString());

                return(false);
            }

            return(success);
        }