Exemple #1
0
        public void Customer_Add_Delete_Test()
        {
            using (DataContext db = new DataContext())
            {
                //Arrange
                Customer customer = new Customer
                {
                    Id = 0,
                    Name = Name.FullName(),
                    Address = Address.StreetAddress(),

                    PhoneNumber = Phone.Number()
                };

                //Act - Create
                db.Customers.Add(customer);
                db.SaveChanges();

                //Assert - Create
                Assert.NotEqual(0, customer.Id);
                customer.Id.ShouldBeGreaterThan(0);

                //Act - Delete
                db.Entry(customer).State = System.Data.Entity.EntityState.Deleted;
                var result = db.SaveChanges();

                //Asset - Create
                Assert.NotEqual(0, result);
                result.ShouldBeGreaterThan(0);
            }
        }
Exemple #2
0
 public SalesController()
 {
     _salesContext = new DataContext();
 }
Exemple #3
0
        public virtual JsonResult Save(SalesOrderViewModel salesOrderViewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelStateException(ModelState);
            }

            SalesOrder salesOrder = ViewModels.Helpers.CreateSalesOrderFromSalesOrderViewModel(salesOrderViewModel);

            _salesContext.SalesOrders.Attach(salesOrder);

            if (salesOrder.ObjectState == ObjectState.Deleted)
            {
                foreach (SalesOrderItemViewModel salesOrderItemViewModel in salesOrderViewModel.SalesOrderItems)
                {
                    SalesOrderItem salesOrderItem = _salesContext.SalesOrderItems.Find(salesOrderItemViewModel.SalesOrderItemId);
                    if (salesOrderItem != null)
                        salesOrderItem.ObjectState = ObjectState.Deleted;
                }
            }
            else
            {
                foreach (int salesOrderItemId in salesOrderViewModel.SalesOrderItemsToDelete)
                {
                    SalesOrderItem salesOrderItem = _salesContext.SalesOrderItems.Find(salesOrderItemId);
                    if (salesOrderItem != null)
                        salesOrderItem.ObjectState = ObjectState.Deleted;
                }
            }

            _salesContext.ApplyStateChanges();
            string messageToClient = string.Empty;

            try
            {
                _salesContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                messageToClient = "Someone else have modified this sales order since you retrieved it.  Your changes have not been applied.  What you see now are the current values in the database.";
            }
            catch (Exception ex)
            {
                throw new ModelStateException(ex);
            }

            if (salesOrder.ObjectState == ObjectState.Deleted)
                return Json(new { newLocation = "/Sales/Index/" });

            if (messageToClient.Trim().Length == 0)
                messageToClient = ViewModels.Helpers.GetMessageToClient(salesOrderViewModel.ObjectState, salesOrder.CustomerName);

            salesOrderViewModel.SalesOrderId = salesOrder.SalesOrderId;
            _salesContext.Dispose();
            _salesContext = new DataContext();
            salesOrder = _salesContext.SalesOrders.Find(salesOrderViewModel.SalesOrderId);

            salesOrderViewModel = ViewModels.Helpers.CreateSalesOrderViewModelFromSalesOrder(salesOrder);
            salesOrderViewModel.MessageToClient = messageToClient;

            return Json(new { salesOrderViewModel });
        }