public Task <Response> GetSaleByProductId(SalesRequest salesRequest)
        {
            if (salesRequest == null)
            {
                throw new ArgumentNullException(nameof(salesRequest));
            }

            if (salesRequest.ProductId == 0)
            {
                return(Task.FromResult(new Response {
                    Notification = "ProductId is requerid"
                }));
            }

            List <Sales> sales = _bagelSalesControlContext.Sales.ToList().FindAll(s => s.ProductId == salesRequest.ProductId);

            if (sales.Any())
            {
                _bagelSalesControlContext.Dispose();
                return(Task.FromResult(new Response {
                    Data = sales
                }));
            }

            return(Task.FromResult(new Response {
                Message = "There isn't sales registered with that product"
            }));
        }
 private Sales MaterializeSale(SalesRequest salesRequest, ControlTransactionFields transactionInfo)
 {
     return(new Sales
     {
         ProductId = salesRequest.ProductId,
         SoldQuantity = salesRequest.SoldQuantity,
         TypeSale = salesRequest.TypeSale,
         Total = salesRequest.Total,
         Commentary = salesRequest.Commentary,
         CreatedBy = transactionInfo.CreatedBy,
         TransactionDate = transactionInfo.TransactionDate,
     });
 }
        public async Task <Response> DeleteSale(SalesRequest salesRequest)
        {
            if (salesRequest == null)
            {
                throw new ArgumentNullException(nameof(salesRequest));
            }

            Sales sale = null;

            if (salesRequest.SaleId != 0)
            {
                sale = _bagelSalesControlContext.Sales.ToList().Find(s => s.SaleId == salesRequest.SaleId);

                if (sale != null)
                {
                    IDbContextTransaction transaction = _bagelSalesControlContext.Database.BeginTransaction();

                    _bagelSalesControlContext.Remove <Sales>(sale);
                    await _bagelSalesControlContext.SaveChangesAsync();

                    transaction.Commit();

                    _bagelSalesControlContext.Dispose();
                    transaction.Dispose();

                    return(new Response {
                        Message = "Sale removed successfully"
                    });
                }
            }

            _bagelSalesControlContext.Dispose();

            return(new Response {
                Notification = "SaleId is requerid"
            });
        }
        public async Task <Response> SaveSale(SalesRequest salesRequest)
        {
            if (salesRequest == null)
            {
                throw new ArgumentNullException(nameof(salesRequest));
            }
            string notification = "";
            Sales  sale         = null;
            IDbContextTransaction    transaction     = _bagelSalesControlContext.Database.BeginTransaction();
            ControlTransactionFields transactionInfo = TransactionInfo.GetTransactionData(salesRequest.UserName);

            if (salesRequest.SaleId != 0)
            {
                sale = _bagelSalesControlContext.Sales.ToList().Find(s => s.SaleId == salesRequest.SaleId);
            }

            if (sale == null)
            {
                sale = MaterializeSale(salesRequest, transactionInfo);

                await _bagelSalesControlContext.AddAsync <Sales>(sale);
            }
            else
            {
                sale.SoldQuantity = salesRequest.SoldQuantity;
                sale.TypeSale     = salesRequest.TypeSale;
                sale.Total        = salesRequest.Total;
                sale.Commentary   = salesRequest.Commentary;
                sale.TransactionModificationDate = DateTime.Now;
            }

            ProductAgg product = _bagelSalesControlContext.Product.ToList().Find(p => p.ProductId == salesRequest.ProductId);

            if (product != null)
            {
                if (product.Existence < salesRequest.SoldQuantity)
                {
                    notification = "No hay suficientes " + product.NameProduct + " disponibles";

                    return(new Response {
                        Notification = notification
                    });
                }

                product.Existence = product.Existence - salesRequest.SoldQuantity;
                if (product.Existence <= 5)
                {
                    notification = "El Producto " + product.NameProduct + " está a punto de acabarse";
                }
            }


            await _bagelSalesControlContext.SaveChangesAsync();

            transaction.Commit();

            _bagelSalesControlContext.Dispose();
            transaction.Dispose();


            return(new Response {
                Message = "The sale was registered successfully", Data = sale, Notification = notification
            });
        }
        public IActionResult DeleteSale([FromBody] SalesRequest salesRequest)
        {
            Task <Response> response = _saleService.DeleteSale(salesRequest);

            return(Ok(response));
        }
        public IActionResult GetSaleByProductId([FromQuery] SalesRequest salesRequest)
        {
            Task <Response> response = _saleService.GetSaleByProductId(salesRequest);

            return(Ok(response));
        }