Пример #1
0
        public ActionResult Profile(User model)
        {
            if (ModelState.IsValid)
            {
                // get the user object
                var user = db.Users.First(u => u.UserID == model.UserID);

                // update information
                user.City = model.City;
                user.DateOfBirth = model.DateOfBirth;
                user.Email = model.Email;
                user.Name = model.Name;
                user.Number = model.Number;
                user.Phone = model.Phone;
                user.Street = model.Street;
                user.Surname = model.Surname;
                user.Zip = model.Zip;

                // save changes
                db.SaveChanges();

                return RedirectToAction("Profile");
            }
            else
            {
                ModelState.AddModelError("", "Error");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus createStatus;

                // check for username uniqueness
                if (UserNameIsUnique(model.UserName))
                {
                    // Attempt to register the user
                    Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);
                    Roles.AddUserToRole(model.UserName, "User");
                }
                else
                {
                    // username exists so throw error
                    createStatus = MembershipCreateStatus.DuplicateUserName;
                }

                if (createStatus == MembershipCreateStatus.Success)
                {
                    var newUser = new User
                    {
                        City = string.Empty,
                        Comments = new List<Comment>(),
                        DateOfBirth = null,
                        Email = model.Email,
                        Messages = new List<Message>(),
                        Name = string.Empty,
                        Number = string.Empty,
                        Phone = 0,
                        Queue = new List<Rental>(),
                        Ratings = new List<Rating>(),
                        Street = string.Empty,
                        Surname = string.Empty,
                        Username = model.UserName,
                        Zip = string.Empty
                    };
                    db.Users.Add(newUser);
                    db.SaveChanges();
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return RedirectToAction("Profile", "Users");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #3
0
        /// <summary>
        /// send user a message of dispatch
        /// </summary>
        /// <param name="aUser"></param>
        /// <returns></returns>
        private static bool msgSend(User aUser, Movie aMovie)
        {
            try
            {
                // create message
                var usr = db.Users.First(u => u.UserID == aUser.UserID);
                var msg = new Message
                              {
                                  DateSent = DateTime.Now,
                                  User = usr,
                                  UserID = usr.UserID,
                                  Text = string.Format("A DVD of {0} was dispatched to you.", aMovie.Name)
                              };

                // add message to db
                db.Messages.Add(msg);
                db.SaveChanges();

                // add message to user
                usr.Messages.Add(msg);
                db.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }