Exemplo n.º 1
0
        public async Task <IHttpActionResult> Post([FromBody] ContractReq req)
        {
            try
            {
                string errorMessage = "UnknowError";
                string errorCode    = ErrorCodeEnum.UnknownError.ToString();
                #region token
                var header = Request.Headers;
                if (header.Authorization == null)
                {
                    return(StatusCode(HttpStatusCode.Unauthorized));
                }
                var      token = header.Authorization.Parameter;
                Employee employee;
                if (string.IsNullOrWhiteSpace(token) || !TokenManager.ValidateToken(token, out employee))
                {
                    return(StatusCode(HttpStatusCode.Unauthorized));
                }
                #endregion
                if (!Operator.IsAdmin(employee))
                {
                    return(Ok(new RequestErrorCode(false, ErrorCodeEnum.Error_NotHavePermision.ToString(), "Khong co quyen")));
                }

                #region Validate
                if (!Validate(req.GetEntity(), out errorCode, out errorMessage))
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion
                // lay connectionTypeName
                var    startStringId          = req.ServiceFormId[0];
                var    lstContractWithStartId = MemoryInfo.GetListContractByStartId(startStringId.ToString());
                int    idContractWithStartId  = lstContractWithStartId.Count + 1;
                string contractId             = startStringId.ToString();
                int    countZeroNumber        = 15 - idContractWithStartId.ToString().Length;
                for (int i = 0; i < countZeroNumber; i++)
                {
                    contractId += "0";
                }
                contractId    += idContractWithStartId.ToString();
                req.ContractId = contractId;
                #region Tạo key
                var oldKey = Memory.Memory.GetMaxKey(req.GetEntity().GetName());
                int newKey = oldKey + 1;
                // set key
                req.Id = newKey;
                #endregion

                #region Process
                req.CreatedAt = DateTime.Now;
                req.CreatedBy = employee.Id;
                req.IsDeleted = 0;
                UpdateEntitySql updateEntitySql = new UpdateEntitySql();
                var             lstCommand      = new List <EntityCommand>();
                lstCommand.Add(new EntityCommand {
                    BaseEntity = new Entity.Entity(req.GetEntity()), EntityAction = EntityAction.Insert
                });
                bool isOkDone = updateEntitySql.UpdateDefault(lstCommand);
                if (!isOkDone)
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion
                // update memory
                MemorySet.UpdateAndInsertEntity(req.GetEntity());
                var result = new RequestErrorCode(true);
                result.DataResult = req;
                return(Ok(result));
            }
            catch (Exception ex)
            {
                Logger.Write(ex.ToString());
            }
            return(BadRequest("Unknow"));
        }