コード例 #1
0
ファイル: ItemsController.cs プロジェクト: nhatvinh277/da-2
        public BaseModel <object> Orders([FromBody] ItemsOrdersModel data)
        {
            BaseModel <object> model     = new BaseModel <object>();
            ErrorModel         _error    = new ErrorModel();
            string             Token     = _account.GetHeader(Request);
            LoginData          loginData = _account._GetInfoUser(Token);

            if (loginData == null)
            {
                return(model = new BaseModel <object>
                {
                    data = null,
                    status = 0,
                    error = new ErrorModel
                    {
                        code = Constant.ERRORDATA,
                        message = "Phiên đăng nhập hết hạn hoặc bạn chưa truyền Token"
                    }
                });
            }

            try
            {
                _context.Database.BeginTransaction();
                DBTransaction transaction = new DBTransaction();
                transaction.IdAccount       = loginData.IdAccount;
                transaction.TransactionCode = DateTime.Now.ToString("yyyyMMddHHmmss");
                _context.DBTransaction.Add(transaction);
                _context.SaveChanges();


                DBTransactionDetail detail = new DBTransactionDetail();
                detail.IdTransaction = transaction.IdTransaction;
                detail.IdItem        = data.IdItem;
                detail.Money         = data.Money;
                detail.Created       = DateTime.Now;
                detail.Quantity      = data.Quantity;


                _context.DBTransactionDetail.Add(detail);
                _context.SaveChanges();

                _context.Database.CommitTransaction();
                model.status = 1;
                model.data   = data;
                return(model);
            }
            catch (Exception ex)
            {
                model.status   = 0;
                _error.message = "Thêm mới thất bại, vui lòng kiểm tra lại!";
                _error.code    = Constant.ERRORCODE;
                model.error    = _error;
                model.data     = null;
                return(model);
            }
        }
コード例 #2
0
        public BaseModel <object> Add(Transaction data)
        {
            BaseModel <object> _baseModel = new BaseModel <object>();
            PageModel          _pageModel = new PageModel();
            ErrorModel         _error     = new ErrorModel();

            string Token = _account.GetHeader(Request);

            try
            {
                DBTransaction item = new DBTransaction();
                item.IdAccount = data.IdAccount;

                _context.DBTransaction.Add(item);
                _context.SaveChanges();

                foreach (ItemTransaction detail in data.items)
                {
                    DBTransactionDetail subitem = new DBTransactionDetail();
                    subitem.IdTransaction = item.IdTransaction.Value;
                    subitem.IdItem        = detail.IdItem;
                    subitem.Quantity      = detail.Quantity;
                    subitem.Money         = detail.Money;

                    _context.DBTransactionDetail.Add(subitem);
                    _context.SaveChanges();
                }



                _context.Database.CommitTransaction();

                data.IdTransaction = item.IdTransaction;
                _baseModel.status  = 1;
                _baseModel.data    = data;
                return(_baseModel);
            }
            catch (Exception ex)
            {
                _error         = new ErrorModel();
                _error.message = "Lỗi dữ liệu hoặc bạn phải truyền Token!";
                _error.code    = Constant.ERRORCODE;
                return(new BaseModel <object>
                {
                    status = 0,
                    error = _error,
                    data = null,
                    page = null
                });
            }
        }