Example #1
0
        public ActionResult <TransactionDto> UpdateTransaction([FromBody] TransactionUpdateDto transaction)
        {
            try
            {
                var oldTransaction = transactionRepository.GetTransactionById(transaction.TransactionId);
                if (oldTransaction == null)
                {
                    return(NotFound("There is no transaction with specified id."));
                }

                mapper.Map(transaction, oldTransaction);

                var valid = transactionValidator.Validate(oldTransaction);

                if (!valid.IsValid)
                {
                    return(BadRequest(valid.Errors));
                }

                transactionRepository.SaveChanges();

                logger.Log(LogLevel.Information, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Transaction successfully updated.");

                return(Ok(mapper.Map <TransactionDto>(oldTransaction)));
            }
            catch (Exception e)
            {
                logger.Log(LogLevel.Warning, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Transaction unsuccessfully updated.");
                return(StatusCode(StatusCodes.Status500InternalServerError, "Create error " + e.Message));
            }
        }
Example #2
0
        public async Task <IActionResult> UpdateTransaction(TransactionUpdateDto updatedTransaction)
        {
            ServiceResponse <TransactionGetDto> response = await _transactionService.UpdateTransaction(updatedTransaction);

            if (response.Data == null)
            {
                return(NotFound(response));
            }

            return(Ok(response));
        }
Example #3
0
        public async Task <int> Update(TransactionUpdateDto dto, int id, string userId)
        {
            var oldTransaction = await _dbContext.Transactions
                                 .Include(x => x.Cheques)
                                 .Include(x => x.ExchangeBills)
                                 .Include(x => x.AccessoryFiles)
                                 .Include(x => x.SaleContract)
                                 .SingleOrDefaultAsync(x => x.Id == id);

            var updatedTransaction = _mapper.Map(dto, oldTransaction);

            _dbContext.Transactions.Update(updatedTransaction);

            await _dbContext.SaveChangesAsync();

            return(updatedTransaction.Id);
        }
        public async Task <ServiceResponse <TransactionGetDto> > UpdateTransaction(TransactionUpdateDto updatedTransaction)
        {
            ServiceResponse <TransactionGetDto> serviceResponse = new ServiceResponse <TransactionGetDto>();

            try{
                Transaction transaction = await _context.Transactions.FirstOrDefaultAsync(u => u.id == updatedTransaction.id);

                transaction.id                 = updatedTransaction.id;
                transaction.userid             = updatedTransaction.user;
                transaction.shippingProviderid = updatedTransaction.shippingProvider;
                transaction.shippingAddressid  = updatedTransaction.shippingAddress;
                transaction.total              = updatedTransaction.total;

                _context.Transactions.Update(transaction);
                await _context.SaveChangesAsync();

                serviceResponse.Data = _mapper.Map <TransactionGetDto>(transaction);
            }
            catch (Exception ex) {
                serviceResponse.Success = false;
                serviceResponse.Message = ex.Message;
            }
            return(serviceResponse);
        }
 public async Task <IActionResult> Update([FromBody] TransactionUpdateDto dto, int id)
 => await GetResponse(async (userId) =>
                      new ApiResponseViewModel(true, "Transaction Updated Successfully", await _service.Update(dto, id, userId)));
Example #6
0
        public async Task <IActionResult> UpdateTransactions(int inventoryId, int transactionId,
                                                             [FromBody] TransactionUpdateDto updateTransaction)
        {
            var inventory = await _invRepo.GetInventory(inventoryId);

            if (inventory == null)
            {
                return(NotFound(new { error = new string[] { "Inventory Not Found" } }));
            }

            var transaction = await _transRepo.GetInventoryTransaction(transactionId);

            if (transaction == null)
            {
                return(NotFound(new { error = new string[] { "Transaction Not Found" } }));
            }

            var transactionType = await _transRepo.GetTransactionType(updateTransaction.Transaction);

            if (transactionType == null)
            {
                return(NotFound(new { error = new string[] { "Transaction Type not Found" } }));
            }

            if (updateTransaction.Quantity < 0)
            {
                ModelState.AddModelError("error", "quantity must be a positive number");
            }

            //Revert Back the Inventory Count prior to the Transaction.
            //If Add, then subtract the transaction original quantity to the Inventory Quantity
            //If Subtract, then add the transaction original quantity to the Inventory Quantity
            if (string.Equals(transaction.TransactionType.Action, "add"))
            {
                inventory.Quantity -= transaction.Quantity;
            }

            if (string.Equals(transaction.TransactionType.Action, "subtract"))
            {
                inventory.Quantity += transaction.Quantity;
            }

            //Check what is the updated TransactionType.
            //After reverting the inventory quantity prior to transaction.
            //Update the inventory count according to the Action and Quantity of the updatedtransaction
            if (string.Equals(transactionType.Action, "add"))
            {
                inventory.Quantity += updateTransaction.Quantity;
            }


            if (string.Equals(transactionType.Action, "subtract"))
            {
                //For Subtract need to make sure that the updatedTransaction wont make the quantity negative.
                var quantity = inventory.Quantity -= transaction.Quantity;

                if (quantity < 0)
                {
                    ModelState.AddModelError("error", "Inventory quantity cannot be negative");
                }
                else
                {
                    inventory.Quantity = quantity;
                }
            }

            //Check and adjust the status based on the Threshold and Critical
            if (inventory.Quantity > inventory.ThresholdCritical && inventory.Quantity <= inventory.ThresholdWarning)
            {
                var status = await _invRepo.GetInventoryStatusByName("Warning");

                inventory.Status = status;
            }
            else if (inventory.Quantity > 0 && inventory.Quantity <= inventory.ThresholdCritical)
            {
                var status = await _invRepo.GetInventoryStatusByName("Critical");

                inventory.Status = status;
            }
            else if (inventory.Quantity > inventory.ThresholdWarning)
            {
                var status = await _invRepo.GetInventoryStatusByName("OK");

                inventory.Status = status;
            }


            //Check for ModelState Error
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //Update the Transaction based on the updatedTransaction sent
            transaction.Quantity        = updateTransaction.Quantity;
            transaction.TransactionType = transactionType;
            transaction.Details         = updateTransaction.Details;
            transaction.TimeStamp       = DateTime.Now;


            if (await _transRepo.Save())
            {
                var transactionToReturn = _mapper.Map <TransactionUpdatedDto>(transaction);

                return(Ok(transactionToReturn));
            }
            else
            {
                return(BadRequest(new { error = new string[] { "Error saving transaction" } }));
            }
        }