Beispiel #1
0
        public IActionResult OnGetPayout(int supplierId)
        {
            var supplier = _supplierRepo.GetById(supplierId);

            if (supplier == null)
            {
                return(NotFound());
            }

            //Create payout and zero out supplier balance
            var payout = new SupplierPayout {
                Amount     = supplier.Balance,
                PayedAt    = DateTime.UtcNow,
                StaffId    = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                SupplierId = supplier.Id
            };

            supplier.Balance = 0;

            //This should be in a tranaction, but we need to ensure the payment is added before we update the balance.
            if (_supplierPayoutRepo.Insert(payout) != 0)
            {
                _supplierRepo.Update(supplier);
            }

            return(RedirectToPage());
        }
        public bool Update(SupplierPayout model)
        {
            using var conn = new MySqlConnection(_connStr);
            conn.Open();
            var sql = $@"
              UPDATE SupplierPayouts SET
                Amount = @{nameof(SupplierPayout.Amount)},
                PayedAt = @{nameof(SupplierPayout.PayedAt)},
                SupplierId = @{nameof(SupplierPayout.SupplierId)},
                StaffId = @{nameof(SupplierPayout.StaffId)}
              WHERE Id = @{nameof(SupplierPayout.Id)};
            ";

            return(conn.Execute(sql, model) == 1);
        }
        public int Insert(SupplierPayout model)
        {
            using var conn = new MySqlConnection(_connStr);
            conn.Open();
            var sql = $@"
              START TRANSACTION;
              INSERT INTO SupplierPayouts(Amount, PayedAt, SupplierId, StaffId)
              VALUES(
                @{nameof(SupplierPayout.Amount)},@{nameof(SupplierPayout.PayedAt)},
                @{nameof(SupplierPayout.SupplierId)},@{nameof(SupplierPayout.StaffId)}
              );
              SELECT @@IDENTITY;
              COMMIT;
            ";

            return(conn.Query <int>(sql, model).Single());
        }