Example #1
0
        public IActionResult CreateAuthority([FromBody] AuthorityRoleViewModel model)
        {
            ErrorObject     response = new ErrorObject();
            ResponseMessage rm       = new ResponseMessage();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (model.tblAuthority == null)
            {
                rm.Type    = AuthorityConstant.TypeAuthority;
                rm.Title   = AuthorityConstant.TitleAddAuthority;
                rm.Message = AuthorityConstant.MessageAddAuthority;
                rm.Status  = AuthorityConstant.statusError;
                var field = new { fieldErrors = rm.Title };

                return(StatusCode(400, Json(rm)));
            }
            else
            {
                string          orgCode = User.Claims.Where(u => u.Type == AuthorityConstant.orgCode).FirstOrDefault().Value;
                TblOrganization org     = _authorityRepository.GetOrganization(orgCode);
                if (org != null)
                {
                    model.tblAuthority.OrganizationId = org.OrganizationId;
                    model.tblAuthority.CreateBy       = User.Claims.FirstOrDefault().Value;
                    if (String.IsNullOrEmpty(model.tblAuthority.OrganizationId.ToString()) || model.tblAuthority.OrganizationId == 0)
                    {
                        rm.Type    = AuthorityConstant.TypeAuthority;
                        rm.Title   = AuthorityConstant.TitleAddAuthorityNullOrg;
                        rm.Message = AuthorityConstant.MessageAddAuthorityNullOrg;
                        rm.Status  = AuthorityConstant.statusError;
                        var field = new { fieldErrors = rm.Title };

                        return(StatusCode(400, Json(rm)));
                    }
                    else
                    {
                        TblAuthority au = _authorityRepository.CreateAuthority(model);
                        if (au == null)
                        {
                            response.entityName = AuthorityConstant.TypeAuthority;
                            response.errorKey   = AuthorityConstant.ErrorKeyAddError;
                            response.status     = AuthorityConstant.statusError;
                            response.message    = AuthorityConstant.MessageAddError;
                            return(StatusCode(400, Json(response)));
                        }
                        return(StatusCode(201, Json(au)));
                    }
                }
                return(BadRequest());
            }
        }