Exemplo n.º 1
0
        public async Task <GlobalResponse> Add(Guid businessId, SellRequestDTO sellRequest)
        {
            var business = await DbContext.Businesses.Include(x => x.Products).Include(x => x.Outlets).FirstOrDefaultAsync(x => x.Id == businessId);

            if (business == null)
            {
                throw new KeyNotFoundException("Business not found");
            }
            var outlet = business.Outlets.FirstOrDefault(x => x.Id == sellRequest.OutletId);

            if (outlet == null)
            {
                throw new KeyNotFoundException("Outlet not found");
            }
            var paymentType = await DbContext.PaymentTypes.FirstOrDefaultAsync(x => x.Name == sellRequest.PaymentType);

            if (paymentType == null)
            {
                throw new KeyNotFoundException("Payment Type not found");
            }

            // is this right ?
            var customer = await DbContext.Customers.FirstOrDefaultAsync(x => x.Id == sellRequest.CustomerId);

            //if (customer == null)
            //{
            //    throw new KeyNotFoundException("Customer not found");
            //}


            var newSell = new Sell()
            {
                Discount        = sellRequest.Discount,
                TotalCost       = sellRequest.TotalCost,
                TransactionDate = DateTime.Now,
                SellItems       = sellRequest.SellItems.Any() ?  sellRequest.SellItems.Select(x => new SellItem()
                {
                    Quantity  = x.Quantity,
                    PriceSold = x.PriceSold,
                    Discount  = x.Discount,
                    Product   = DbContext.Products.FirstOrDefault(y => y.Id == x.ProductId) ?? throw new AppException($"product with id {x.ProductId}  not found")
                }).ToList() : throw  new AppException("Sell items cannot be null"),
        public async Task <IActionResult> Create([FromRoute] Guid businessId, [FromBody] SellRequestDTO request)
        {
            var response = await SellService.Add(businessId, request);

            return(Ok(response));
        }