public async Task <IActionResult> Save(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.UserId == 0)
                    {
                        model = await UsersBL.CreateUser(_context, _userManager, model);

                        if (!string.IsNullOrEmpty(model.ErrorText))
                        {
                            throw new Exception(model.ErrorText);
                        }
                    }
                    else
                    {
                        model = UsersBL.EditUserProfile(_context, model);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            return(PartialView("CreateEdit", model));
        }
Beispiel #2
0
        public async Task <IActionResult> Post([FromBody] User user)
        {
            using (var usersBl = new UsersBL(App.ConnectionString))
            {
                user.PasswordHash = App.PasswordHasher.HashPassword(user.PasswordHash);

                var result = await usersBl.CreateUser(user);

                var response = new Response();
                var logInfo  = new LogInfo
                {
                    Time = DateTime.Now
                };

                if (result.ResponseCode != ResponseCode.Success)
                {
                    response.ResponseCode = result.ResponseCode;
                    response.Content      = Messages.Message(result.ResponseCode);

                    logInfo.LogType = LogType.Fail;
                    logInfo.Message = result.Exception?.Message;

                    return(this.ApiResponse(HttpStatusCode.BadRequest, response, logInfo));
                }

                var u = result.Content;
                response.ResponseCode = 0;
                response.Content      = new
                {
                    u.Id,
                    u.FirstName,
                    u.LastName,
                    u.Username
                };

                return(this.ApiResponse(HttpStatusCode.OK, response));
            }
        }