public ActionResult <Purchase> PostPurchase([FromBody] PurchaseCreationDTO purchaseCreationDTO, [FromHeader] string key)
        {
            try
            {
                //just autorized users can access
                if (!auth.AuthorizeUser(key))
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
                }


                Purchase purchase = mapper.Map <Purchase>(purchaseCreationDTO);


                purchaseRepository.CreatePurchase(purchase);
                purchaseRepository.SaveChanges();

                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "Create new purchase", null);

                string location = linkGenerator.GetPathByAction("GetPurchaseById", "Purchase", new { purchaseId = purchase.PurchaseId });
                return(Created(location, purchase));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Error while creating purchase", null);
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #2
0
        public PurchaseForPurchaseController CreatePurchase(Guid customerId, [FromBody] ReceiptModel receiptModel)
        {
            Shop shop = shopRepository.CreateShop(
                Convert.ToInt64(receiptModel.UserInn),
                receiptModel.User,
                receiptModel.RetailPlaceAddress);

            Customer customer = customerRepository.GetCustomer(customerId);

            Purchase purchase = purchaseRepository.CreatePurchase(
                customer.Id,
                shop.Id,
                Convert.ToDateTime(receiptModel.DateTime),
                receiptModel.TotalSum
                );

            foreach (var item in receiptModel.Items)
            {
                Product     product     = productRepository.CreateProduct(item.Name);
                ProductItem productItem = productItemRepository.CreateProductItem(
                    product.Id,
                    purchase.Id,
                    item.Price / 100,
                    item.Quantity,
                    item.Sum / 100);
            }
            PurchaseForPurchaseController purchaseInfo =
                Mapper.Map <Purchase, PurchaseForPurchaseController>(purchaseRepository.GetUserPurchase(purchase.Id, customerId));

            return(purchaseInfo);
        }
        public async Task <bool> CreatePurchase(PurchaseCreateChargeDTO dto)
        {
            var rao = _mapper.Map <PurchaseCreateChargeRAO>(dto);

            if (await _repository.CreatePurchase(rao))
            {
                return(true);
            }

            throw new NotImplementedException();
        }