public ApiBadRequestResponse(ModelStateDictionary modelState) : base(400)
        {
            Errors = new List <Error>();
            DataLayerContext       dlContext = new DataLayerContext();
            ErrorMessageRepository errorMessageRepository = new ErrorMessageRepository(dlContext);

            IEnumerable <string> LstError = modelState.SelectMany(x => x.Value.Errors).Select(x => x.ErrorMessage).ToArray();

            foreach (var errorMessage in LstError)
            {
                Error _objError = new Error();

                if (errorMessage.Contains("$$"))
                {
                    string[] strError = errorMessage.Split(new char[] { '$', '$' }, StringSplitOptions.RemoveEmptyEntries);
                    _objError.Code = strError[0];
                    string requiredMessage = errorMessageRepository.GetByCode(strError[0]);
                    if (requiredMessage.Contains("$$InputData$$"))
                    {
                        requiredMessage = requiredMessage.Replace("$$InputData$$", strError[1]);
                    }
                    _objError.Message = requiredMessage;
                    Errors.Add(_objError);
                }
                else
                {
                    string dbMessage = errorMessageRepository.GetByCode(errorMessage);
                    _objError.Code    = errorMessage;
                    _objError.Message = dbMessage;
                    Errors.Add(_objError);
                }
            }
        }
Example #2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string                 email                  = string.Empty;
            bool                   isValid                = false;
            ApiResponse            _objResponse           = new ApiResponse();
            DataLayerContext       dl                     = new DataLayerContext();
            ErrorMessageRepository errorMessageRepository = new ErrorMessageRepository(dl);
            UserRepository         userRepository         = new UserRepository(dl);

            try
            {
                if (!string.IsNullOrEmpty(Permission))
                {
                    var currentUser = filterContext.HttpContext.User;
                    if (currentUser.HasClaim(c => c.Type == ClaimTypes.Email))
                    {
                        email = currentUser.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;
                    }
                    isValid = userRepository.AuthoriseUserWithPermission(email, Permission);
                }

                if (isValid == false)
                {
                    _objResponse.Message    = errorMessageRepository.GetByCode("131");
                    _objResponse.StatusCode = (int)HttpStatusCode.Forbidden;
                    filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                    string json = JsonConvert.SerializeObject(_objResponse, Formatting.Indented);
                    filterContext.Result = new ContentResult {
                        Content = json
                    };
                }
            }
            catch (Exception ex)
            {
                _objResponse.Message    = errorMessageRepository.GetByCode("501");
                _objResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
                filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                string json = JsonConvert.SerializeObject(_objResponse, Formatting.Indented);
                filterContext.Result = new ContentResult {
                    Content = json
                };
            }
        }