Exemple #1
0
        public string Create(UserInfo userInfo)
        {
            if (!ModelState.IsValid)
            {
                ErrorModel modelIsInvalidError = new ErrorModel("Error", new List <string>(), new List <string>());

                foreach (var item in ModelState)
                {
                    foreach (var error in item.Value.Errors)
                    {
                        modelIsInvalidError.ErrorKeysList.Add(item.Key);
                        modelIsInvalidError.ErrorMessagesList.Add(error.ErrorMessage);
                    }
                }

                return(modelIsInvalidError.ToJson());
            }

            userInfo.Id = 1; //temporary user's id for testing DB.

            try
            {
                _userService.Add(userInfo);
                db.Commit();
            }
            catch (Exception)
            {
                var errorAddingtoDB = new ErrorModel("Error", new List <string>()
                {
                    "Some troubles with DB happened, try to add your info later."
                }, new List <string>()
                {
                    "DataBaseError"
                });
                return(errorAddingtoDB.ToJson());
            }

            var succesResult = new JsonNetResult(new { header = "Succes", message = "Resources added succesfully" });

            return(succesResult.ToJson());
        }