예제 #1
0
        /// <summary>
        /// Called when the user wants to add a new sale
        /// </summary>
        /// <param name="sender">Event owner</param>
        /// <param name="e">Event args</param>
        private void View_Add(object sender, EventArgs e)
        {
            if (!OpenSession())
                return;

            // Create a new sale object and assign values
            // values should be validated before getting here
            var sale = new Sale();
            sale.StoreId = _view.StoreId;
            sale.ProductId = _view.ProductId;
            sale.SaleTime = DateFormatter.GetDateWithoutMilliseconds(DateTime.Now);

            try
            {
                // Save new sale
                _saleRepository.Save(sale);
                _saleRepository.Commit();

                UpdateStatus("New sale created with ID of " + sale.Id);
            }
            catch (Exception ex)
            {
                OperationFailed(ex, "Could not add Sale\n");
            }
        }
예제 #2
0
        /// <summary>
        /// Called when the user wants to find one or more sales
        /// using a specfied criteria
        /// </summary>
        /// <param name="sender">Event owner</param>
        /// <param name="e">Event args</param>
        private void View_Get(object sender, EventArgs e)
        {
            if (!OpenSession())
                return;

            // Search by ID if we have one
            if (_view.Id != 0)
            {
                var sale = new Sale();
                try
                {
                    // Search by ID
                    sale = _saleRepository.Get(_view.Id);
                }
                catch (Exception ex)
                {
                    OperationFailed(ex, "Could not retrieve Sale by ID\n");
                    return;
                }

                // Nothing found inform the user
                if (sale == null)
                {
                    UpdateStatus("Could not find Sale with an ID of " + _view.Id);
                    return;
                }

                UpdateStatus("ID: " + sale.Id +
                             "\nStore ID: " + sale.StoreId +
                             "\nProduct ID: " + sale.ProductId +
                             "\nTimestamp: " + sale.SaleTime);

                return; // Prevent further searching

            }

            IList<Sale> sales = new List<Sale>();
            try
            {
                // Determine which search to perform
                if (_view.StoreId != 0)
                {
                    // Search by store ID
                    sales = _saleRepository.GetWithStoreId(_view.StoreId);
                } else if (_view.ProductId != 0)
                {
                    // Search by product ID
                    sales = _saleRepository.GetWithProductId(_view.ProductId);
                }
            }
            catch (Exception ex)
            {
                OperationFailed(ex, "Could not retrieve Sale by Store ID\n");
                return;
            }

            // None found inform the user
            if (sales.Count == 0)
            {
                UpdateStatus("No Sales found with the Store ID of " + _view.StoreId);
                return;
            }

            // Return the list of sales back to the view
            OnObjectGetAllReturned?.Invoke(this, new ObjectGetAllReturnedArgs<IList<Sale>>(sales));
        }