예제 #1
0
        public IActionResult Post([FromBody] AccountModel request)
        {
            UserSessionManager usrSession         = new UserSessionManager();
            var                user               = User as ClaimsPrincipal;
            string             userId             = user.Claims.Where(c => c.Type == "USERID").Select(c => c.Value).SingleOrDefault();
            AccountModelOutput accountModelOutput = new AccountModelOutput();

            try
            {
                _logger.Information("Saving Account");
                if (ModelState.IsValid)
                {
                    AccountServices accountServices = new AccountServices
                    {
                        objUser = usrSession.UserLog(userId)._userInfo
                    };
                    var res = accountServices.Add(request);
                    accountModelOutput.IsSuccess = true;
                    accountModelOutput.Message   = "Success Saving";
                    accountModelOutput.Code      = 200;
                }
                else
                {
                    _logger.Error("Error Post Account");


                    string errordetails = "";
                    var    errors       = new List <string>();
                    foreach (var state in ModelState)
                    {
                        foreach (var error in state.Value.Errors)
                        {
                            string p = error.ErrorMessage;
                            errordetails = errordetails + error.ErrorMessage;
                        }
                    }
                    Dictionary <string, object> dict = new Dictionary <string, object>();
                    dict.Add("error", errordetails);

                    accountModelOutput.IsSuccess   = false;
                    accountModelOutput.Message     = "error saving validating";
                    accountModelOutput.Code        = 422;
                    accountModelOutput.CustomField = dict;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message.ToString());
                accountModelOutput.IsSuccess = false;
                accountModelOutput.Message   = "Failed Saving" + ex.Message;
                accountModelOutput.Code      = 422;
            }

            return(Ok(accountModelOutput));
        }
예제 #2
0
        public void ShouldReturnSuccessForAdd()
        {
            AccountModel account = new AccountModel();

            account.Username   = "******" + randomNumber;
            account.Password   = "******";
            account.EmployeeID = 1;
            account.RoleID     = 1;


            var    opeartionSucceeded = false;
            string formattedJsonStr   = string.Empty;
            string dataAccessJsonStr;

            try
            {
                accountService.Add(account);
                opeartionSucceeded = true;
            }
            catch (ArgumentException e)
            {
                dataAccessJsonStr = JsonConvert.SerializeObject(e);
                formattedJsonStr  = JToken.FromObject(dataAccessJsonStr).ToString();
            }
            catch (DataAccessException e)
            {
                e.DataAccessStatusInfo.OperationSucceeded = opeartionSucceeded;
                dataAccessJsonStr = JsonConvert.SerializeObject(e.DataAccessStatusInfo);
                formattedJsonStr  = JToken.FromObject(dataAccessJsonStr).ToString();
            }

            try
            {
                Assert.True(opeartionSucceeded);
                testOutputHelper.WriteLine("The record has been succesfully added");
            }
            finally
            {
                testOutputHelper.WriteLine(formattedJsonStr);
            }
        }