예제 #1
0
        public DTOs.Order CreateOrder(DTOs.Order order)
        {
            order.DateCreated ??= DateTime.Now;
            order.DateLastModified ??= DateTime.Now;

            var mappedEntityOrder = Mapper.Map <Entities.Order>(order);
            var orderAdded        = InventoryDbContext.Orders.Add(mappedEntityOrder).Entity;

            InventoryDbContext.SaveChanges();

            return(Mapper.Map <DTOs.Order>(orderAdded));
        }
예제 #2
0
        public DTOs.Order UpdateOrder(DTOs.Order order)
        {
            var entityOrder = InventoryDbContext.Orders.SingleOrDefault(existingOrder => existingOrder.Id == order.Id);

            if (entityOrder == null)
            {
                return(null);
            }

            entityOrder.Name             = order.Name;
            entityOrder.DateLastModified = DateTime.Now;

            var updatedOrder = InventoryDbContext.Update(entityOrder).Entity;

            InventoryDbContext.SaveChanges();

            return(Mapper.Map <DTOs.Order>(updatedOrder));
        }
        public void InventoryAccessor_UpdateOrder_ForExistingOrder_ShouldSucceed()
        {
            // Arrange
            using var inventoryDbContext = InventoryDbContext;

            try
            {
                // Insert seed data into the database using one instance of the context
                AddCustomers(inventoryDbContext);
                AddOrders(inventoryDbContext);

                var order = new DTOs.Order
                {
                    Id               = 1,
                    Name             = "Hashbrowns",
                    DateCreated      = DateTime.Now,
                    DateLastModified = DateTime.Now,
                    CustomerId       = 1
                };

                var inventoryAccessor = new InventoryAccessor(inventoryDbContext, Mapper);

                // Act
                var orderUpdated = inventoryAccessor.UpdateOrder(order);

                // Assert
                Assert.NotNull(orderUpdated);
                Assert.Equal(order.Name, orderUpdated.Name);
            }
            finally
            {
                inventoryDbContext.ChangeTracker
                .Entries()
                .ToList()
                .ForEach(e => e.State = EntityState.Detached);
                inventoryDbContext.Database.EnsureDeleted();
            }
        }