Example #1
0
        public GetTransactionDTO CreateTransaction(CreateTransactionDTO createTransactionDTO)
        {
            var transaction = _mapper.Map <Transaction>(createTransactionDTO);

            _transactionService.CreateTransaction(transaction);
            return(_mapper.Map <GetTransactionDTO>(transaction));
        }
Example #2
0
        public async Task <IActionResult> CreateTransaction([FromBody] CreateTransactionDTO dto)
        {
            try
            {
                var user = await GetCurrentUser();

                var pharmacy = await RetrievePharmacy(user.PharmacyId);

                var register = RetrieveRegister(pharmacy, dto.RegisterId);

                var transaction = new Transaction(pharmacy.Id, dto);

                foreach (var product in dto.Products)
                {
                    await TryAddProduct(pharmacy, product, transaction);
                }

                if (transaction.PaymentTypeId == PaymentTypeId.Cash)
                {
                    register.Cash += transaction.TotalPrice;
                }

                Context.Add(transaction);
                await Context.SaveChangesAsync();

                return(Created());
            }
            catch (ResourceNotFoundException ex)
            {
                return(ApiNotFound(ApiErrorSlug.ResourceNotFound, ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(ApiBadRequest(ApiErrorSlug.InsufficientBalance, ex.Message));
            }
        }