Ejemplo n.º 1
0
        public bool Save(User user)
        {
            db.Users.Add(user);
            db.SaveChanges();

            return true;
        }
Ejemplo n.º 2
0
        public JsonResult New(UserRegistrationForm userRegistrationForm)
        {
            if (userRegistrationForm == null)
            {
                return Json(JsonHelper.CreateErrorResponse("Bad Request"));
            }

            if (TryValidateModel(userRegistrationForm))
            {
                var errors = new string[] { };
                var user = new User() {
                    Login = userRegistrationForm.Login.Trim(),
                    Name = userRegistrationForm.Name.Trim(),
                    PasswordHash = PasswordHelper.CreateHash(userRegistrationForm.Password)
                };

                try
                {
                    userRepository.Save(user);

                    return Json(new { errors = Enumerable.Empty<string>().ToArray() });
                }
                catch (DbEntityValidationException ex) //db validation errors
                {
                    errors = ex.EntityValidationErrors.SelectMany(v => v.ValidationErrors).Select(e => e.ErrorMessage).ToArray<string>();
                }
                catch (DbUpdateException ex)
                {
                    errors = new string[] { "Login already registered" };
                }
                catch (Exception ex)
                {
                    errors = new string[] { ex.Message };
                }

                return Json(JsonHelper.CreateErrorResponse(errors));
            }
            else
            {
                var allErrors = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage);
                return Json(JsonHelper.CreateErrorResponse(allErrors));
            }
        }