コード例 #1
0
        public async Task <IHttpActionResult> Delete(int id)
        {
            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 Check exist
                var obj = MemoryInfo.GetPayment(id);
                if (obj == null)
                {
                    return(Ok(new RequestErrorCode(false, ErrorCodeEnum.DataNotExist.ToString(), "Khong ton tai")));
                }
                #endregion

                bool isHasDeleteProperties = obj.GetType().GetProperty("IsDeleted") != null;
                if (!isHasDeleteProperties)
                {
                    return(Ok(new RequestErrorCode(false, ErrorCodeEnum.DataNotExist.ToString(), "Khong ton tai")));
                }
                obj.IsDeleted = 1;

                #region Process
                UpdateEntitySql updateEntitySql = new UpdateEntitySql();
                var             lstCommand      = new List <EntityCommand>();
                var             lstPaymentFee   = MemoryInfo.GetListPaymentFeeByField(obj.Id.ToString(), PaymentFee.PaymentFeeFields.IdPayment);
                foreach (var paymentFee in lstPaymentFee)
                {
                    lstCommand.Add(new EntityCommand {
                        BaseEntity = new Entity.Entity(paymentFee), EntityAction = EntityAction.Delete
                    });
                    MemorySet.RemoveMemory(paymentFee);
                }
                lstCommand.Add(new EntityCommand {
                    BaseEntity = new Entity.Entity(obj), EntityAction = EntityAction.Update
                });
                bool isOkDone = updateEntitySql.UpdateDefault(lstCommand);
                if (!isOkDone)
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion
                // update memory
                MemorySet.UpdateAndInsertEntity(obj);
                var result = new RequestErrorCode(true);
                result.DataResult = obj;
                return(Ok(result));
            }
            catch (Exception ex)
            {
                Logger.Write(ex.ToString());
            }
            return(BadRequest("Unknow"));
        }
        public async Task <IHttpActionResult> Put(int id, [FromBody] ImportReceiptReq 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")));
                }

                var entityData = req.GetEntity();
                #region Validate
                if (!ValidateUpdate(req.GetEntity(), out errorCode, out errorMessage))
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion

                #region Check exist
                var obj = MemoryInfo.GetImportReceipt(id);
                if (obj == null)
                {
                    return(Ok(new RequestErrorCode(false, ErrorCodeEnum.DataNotExist.ToString(), "Khong ton tai")));
                }
                #endregion
                req.Id = obj.Id;                 // gan lai id de update
                #region Process
                req.UpdatedAt = DateTime.Now;
                req.UpdatedBy = employee.Id;
                UpdateEntitySql updateEntitySql  = new UpdateEntitySql();
                var             lstCommandDelete = new List <EntityCommand>();
                var             lstCommand       = new List <EntityCommand>();
                var             lstDetailImport  = MemoryInfo.GetListDetailImportReceiptByField(obj.Id.ToString(), DetailImportReceipt.DetailImportReceiptFields.IdImportReceipt);
                foreach (var detailImport in lstDetailImport)
                {
                    lstCommandDelete.Add(new EntityCommand {
                        BaseEntity = new Entity.Entity(detailImport), EntityAction = EntityAction.Delete
                    });
                    MemorySet.RemoveMemory(detailImport);
                }
                bool isOkDone = updateEntitySql.UpdateDefault(lstCommandDelete);
                if (isOkDone)
                {
                    if (req.ListDataTemp != null)
                    {
                        foreach (var detailImport in req.ListDataTemp)
                        {
                            lstCommand.Add(new EntityCommand {
                                BaseEntity = new Entity.Entity(detailImport), EntityAction = EntityAction.Insert
                            });
                            MemorySet.UpdateAndInsertEntity(detailImport);
                        }
                    }
                }
                lstCommand.Add(new EntityCommand {
                    BaseEntity = new Entity.Entity(req.GetEntity()), EntityAction = EntityAction.Update
                });
                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"));
        }