/// <summary>
        /// Save a Product
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="product"></param>
        /// <returns>returns the id of the saved product</returns>
        public int Save(DataContext dc, Domain.Product product)
        {
            dc = dc ?? Conn.GetContext();
            var dbProduct = dc.Products.Where(p => p.ProductID == product.ID).SingleOrDefault();
            var isNew     = false;

            if (dbProduct == null)
            {
                dbProduct = new Product();
                isNew     = true;
            }

            dbProduct.Name        = product.Name;
            dbProduct.Description = product.Description;
            dbProduct.Price       = product.Price;
            dbProduct.IsActive    = product.IsActive;
            dbProduct.Image       = product.Image;
            dbProduct.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbProduct.DateCreated = DateTime.Now;
                dc.Products.InsertOnSubmit(dbProduct);
            }
            dc.SubmitChanges();
            return(dbProduct.ProductID);
        }
Пример #2
0
        /// <summary>
        /// Returns the list of Orders placed at the Location with the given ID.
        /// </summary>
        /// <param name="locationID"></param>
        /// <returns> List<Order> toReturn </returns>
        public List <Domain.Order> GetOrdersByLocationID(int locationID)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var dbOrders = _context.Set <Order>().Where(i => i.LocationId == locationID).ToList();
            List <Domain.Order> toReturn = new List <Domain.Order>();

            foreach (var o in dbOrders)
            {
                var n = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
                {
                    Time = (DateTimeOffset)o.Time
                };
                var lines = _context.Set <OrderLine>().Where(i => i.OrderId == o.Id).ToList();
                foreach (var i in lines)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Name, (decimal)dbProduct.Price);
                    n.SetItemAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
Пример #3
0
        /// <summary>
        /// Returns the specific Location matching the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> Location loc </returns>
        public Domain.Location GetLocationByName(string name)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var l = _context.Set <Location>().Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();

            Domain.Location loc = new Domain.Location(l.Id)
            {
                Name = l.Name
            };

            var inventories = _context.Set <Inventory>().Where(i => i.LocationId == loc.ID).ToList();

            foreach (var i in inventories)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);
                loc.SetProductAmount(domProduct, i.Amount);
            }

            return(loc);
        }
Пример #4
0
        /// <summary>
        /// Returns the specific Order matching the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Order ord </returns>
        public Domain.Order GetOrderByID(int id)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var o   = _context.Set <Order>().Find(id);
            var ord = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
            {
                Time = (DateTimeOffset)o.Time
            };

            var items = _context.Set <OrderLine>().Where(i => i.OrderId == id).ToList();

            foreach (var i in items)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Name, (decimal)dbProduct.Price);
                ord.SetItemAmount(domProduct, i.Amount);
            }

            return(ord);
        }
Пример #5
0
 public Domain.Product ToDomainProduct()
 {
     Domain.Product p = new Domain.Product();
     p.Name      = this.Name;
     p.UnitPrice = this.UnitPrice;
     return(p);
 }
Пример #6
0
        // CRUD Location

        /// <summary>
        /// Returns the list of all Locations from the database.
        /// </summary>
        /// <returns> List<Location> toReturn </returns>
        public List <Domain.Location> GetAllLocations()
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var dbLocations = _context.Set <Location>().ToList();
            List <Domain.Location> toReturn = new List <Domain.Location>();

            foreach (var l in dbLocations)
            {
                var n = new Domain.Location(l.Id)
                {
                    Name = l.Name
                };
                var inventories = _context.Set <Inventory>().Where(i => i.LocationId == l.Id).ToList();
                foreach (var i in inventories)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);
                    n.SetProductAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
        /// <summary>
        /// Handle the command initiated by the controller
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <CommonResponse> Handle(CreateProductCommand message, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation($"CreateProductCommandHandler : Create New Product");

                var Product = new Domain.Product(message.Name, message.Price, message.ManufactureDate, message.Location, message.IsAvailable);
                _context.Products.Add(Product);

                var result = await _context.SaveEntitiesAsync();

                if (result)
                {
                    await _requestManager.UpdateRequest(_headerService.GetRequestId(), Product.Id);

                    return(CommonResponse.CreateSuccessResponse("Success", "Product has been successfully created", 200));
                }
                else
                {
                    return(CommonResponse.CreateFailedResponse("Failed", 203));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, nameof(CreateProductCommand));
                return(CommonResponse.CreateFailedResponse("Failed", 500));
            }
        }
Пример #8
0
        private static void EnsureWarehouse(FulfillmentDB context, Domain.Product product)
        {
            var warehouses = context.GetWarehouseRepository();
            var warehouse  = warehouses.GetAll()
                             .FirstOrDefault();

            if (warehouse == null)
            {
                warehouse = new Domain.Warehouse {
                    Name = "Hangar 18"
                };
                warehouse.Requisitions = new List <Domain.Requisition>()
                {
                    new Domain.Requisition
                    {
                        Product  = product,
                        Quantity = 50,
                        Restocks = new List <Domain.Restock> {
                            new Domain.Restock()
                        }
                    }
                };
                warehouses.Add(warehouse);
                warehouses.SaveChanges();
            }
        }
        // CRUD Orders

        /// <summary>
        /// Returns the list of all Orders in the database.
        /// </summary>
        /// <returns> List<Order> toReturn </returns>
        public List <Domain.Order> GetAllOrders()
        {
            var dbOrders = _context.Set <Order>().ToList();
            List <Domain.Order> toReturn = new List <Domain.Order>();

            foreach (var o in dbOrders)
            {
                var n = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
                {
                    Time = (DateTimeOffset)o.Time
                };

                var lines = _context.Set <OrderLine>().Where(i => i.OrderId == o.Id).ToList();
                foreach (var i in lines)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);

                    n.SetItemAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
Пример #10
0
        /// <summary>
        /// Returns the specific Product that has the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Product x </returns>
        public Domain.Product GetProductByID(int id)
        {
            var c = _context.Set <Product>().Find(id);
            var x = new Domain.Product(c.Name, (decimal)c.Price);

            return(x);
        }
Пример #11
0
 public static ProductDto From(Domain.Product product)
 {
     return(new ProductDto()
     {
         Name = product.Name,
         Units = product.UnitsInStock
     });
 }
Пример #12
0
        public async Task AddItem(Guid id, Domain.Product product)
        {
            var cart = _carts.SingleOrDefault(x => x.Id == id);

            cart.AddProduct(product);

            await Task.CompletedTask;
        }
Пример #13
0
            public async Task <Guid> Handle(Command request, CancellationToken cancellationToken)
            {
                var c    = new Domain.Product(Guid.NewGuid(), request.Name);
                var data = this.database.GetCollection <Domain.Product>("products");
                await data.InsertOneAsync(c);

                return(c.Id);
            }
 public void Remove(Domain.Product product)
 {
     using (var session = NHibernateHelper.CreateSession())
     {
         using (var tran = session.BeginTransaction())
         {
             session.Delete(product);
             tran.Commit();
         }
     }
 }
Пример #15
0
 public static ProductModel FormDomain(Domain.Product product)
 {
     return(new ProductModel
     {
         Id = product.Id,
         Status = ProductStatusModel.FormDto(product.Status),
         Type = ProductTypeModel.FormDto(product.Type),
         Description = product.Description,
         Owner = OwnerModel.FromDomain(product.Owner),
         Zone = ZoneModel.FromDomain(product.Zone)
     });
 }
Пример #16
0
        /// <summary>
        /// Returns the specific Product that has the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Product x </returns>
        public Domain.Product GetProductByID(int id)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var c = _context.Set <Product>().Find(id);
            var x = new Domain.Product(c.Name, (decimal)c.Price);

            return(x);
        }
Пример #17
0
        public async Task <bool> Handle(RegisterProductCommand request, CancellationToken cancellationToken)
        {
            if (!ValidateObject(new AddProductValidation(), request))
            {
                return(false);
            }

            var product = new Domain.Product(request.Id, request.Name, request.StockBalance);

            await _productRepository.AddAsync(product);

            return(true);
        }
        /// <summary>
        /// Delete a single product
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="product"></param>
        public void Delete(DataContext dc, Domain.Product product)
        {
            dc = dc ?? Conn.GetContext();
            var dbProduct = dc.Products.Where(p => p.ProductID == product.ID).SingleOrDefault();

            if (dbProduct == null)
            {
                return;
            }
            dc.Products.Attach(dbProduct, true);
            dc.Products.DeleteOnSubmit(dbProduct);
            dc.SubmitChanges();
        }
Пример #19
0
 public ActionResult Edit([Bind(Prefix = "Product")] Domain.Product entity)
 {
     if (!ModelState.IsValid)
     {
         return(View(
                    new Models.EditProductViewModel()
         {
             Product = entity,
             Categories = _categoryRepository.GetAll()
         }));
     }
     _productRepository.Update(entity);
     return(RedirectToAction("Index"));
 }
Пример #20
0
        private static Domain.Product EnsureProduct(FulfillmentDB context)
        {
            var products = context.GetProductRepository();
            var product = products.GetAll()
                .FirstOrDefault(p => p.ProductNumber == 11190);

            if (product == null)
            {
                product = new Domain.Product { ProductNumber = 11190 };
                products.Add(product);
                products.SaveChanges();
            }

            return product;
        }
Пример #21
0
        // CRUD Product

        /// <summary>
        /// Returns the list of all Products in the database.
        /// </summary>
        /// <returns> List<Product> list </returns>
        public List <Domain.Product> GetAllProducts()
        {
            var x = _context.Set <Product>().AsEnumerable();
            List <Domain.Product> list = new List <Domain.Product>();

            foreach (var i in x)
            {
                var p = new Domain.Product(i.Name, (decimal)i.Price)
                {
                    ID = i.Id
                };
                list.Add(p);
            }

            return(list);
        }
Пример #22
0
        /// <summary>
        /// Returns the list of products that match the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> List<Product> toReturn </returns>
        public List <Domain.Product> GetProductByName(string name)
        {
            var products = _context.Set <Product>().Where(x => name == x.Name).ToList();
            List <Domain.Product> toReturn = new List <Domain.Product>();

            foreach (var p in products)
            {
                var x = new Domain.Product(p.Name, (decimal)p.Price)
                {
                    ID = p.Id
                };
                toReturn.Add(x);
            }

            return(toReturn);
        }
Пример #23
0
        private static Domain.Product EnsureProduct(FulfillmentDB context)
        {
            var products = context.GetProductRepository();
            var product  = products.GetAll()
                           .FirstOrDefault(p => p.ProductNumber == 11190);

            if (product == null)
            {
                product = new Domain.Product {
                    ProductNumber = 11190
                };
                products.Add(product);
                products.SaveChanges();
            }

            return(product);
        }
        public void Add(Domain.Product product)
        {
            //using (var session = NHibernateHelper.CreateSession())
            //{
            //    using (var tran = session.BeginTransaction())
            //    {
            //        session.Save(product);
            //        tran.Commit();
            //    }
            //}

            using (var session = NHibernateHelper.CreateSession())
            {
                session.Save(product);
                session.Flush();
            }
        }
Пример #25
0
        public void AddsProductToRepository_RaisesIntegrationEvent()
        {
            var product    = new Domain.Product(Id, "Product Name", null);
            var repository = new Mock <IProductRepository>(MockBehavior.Strict);

            repository.Setup(r => r.GetAsync(product.Id)).Returns(Task.FromResult(product)).Verifiable();
            repository.Setup(r => r.DeleteAsync(product)).Returns(Task.CompletedTask).Verifiable();
            repository.Setup(r => r.SaveChanges()).Returns(Task.CompletedTask).Verifiable();

            var busPublisher = new Mock <IBusPublisher>(MockBehavior.Strict);

            busPublisher.Setup(p => p.Publish <IProductDeleted>(It.Is <IProductDeleted>(e => ValidateEquality(e)))).Returns(Task.CompletedTask).Verifiable();

            var handler = new DeleteProductHandler(repository.Object, busPublisher.Object);
            var result  = handler.HandleAsync(Cmd, new Mock <ICorrelationContext>().Object).GetAwaiter().GetResult();

            Assert.IsTrue(result.Successful);
            repository.Verify();
            busPublisher.Verify();
        }
        /// <summary>
        /// Returns the specific Location matching the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> Location loc </returns>
        public Domain.Location GetLocationByName(string name)
        {
            var l = _context.Set <Location>().Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();

            Domain.Location loc = new Domain.Location(l.Id)
            {
                Name = l.Name
            };

            var inventories = _context.Set <Inventory>().Where(i => i.LocationId == loc.ID).ToList();

            foreach (var i in inventories)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);
                loc.SetProductAmount(domProduct, i.Amount);
            }

            return(loc);
        }
        /// <summary>
        /// Returns the specific Order matching the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Order ord </returns>
        public Domain.Order GetOrderByID(int id)
        {
            var o   = _context.Set <Order>().Find(id);
            var ord = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
            {
                Time = (DateTimeOffset)o.Time
            };

            var items = _context.Set <OrderLine>().Where(i => i.OrderId == id).ToList();

            foreach (var i in items)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);

                ord.SetItemAmount(domProduct, i.Amount);
            }

            return(ord);
        }
Пример #28
0
        // CRUD Product

        /// <summary>
        /// Returns the list of all Products in the database.
        /// </summary>
        /// <returns> List<Product> list </returns>
        public List <Domain.Product> GetAllProducts()
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var x = _context.Set <Product>().AsEnumerable();
            List <Domain.Product> list = new List <Domain.Product>();

            foreach (var i in x)
            {
                var p = new Domain.Product(i.Name, (decimal)i.Price)
                {
                    ID = i.Id
                };
                list.Add(p);
            }

            return(list);
        }
Пример #29
0
        /// <summary>
        /// Returns the list of products that match the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> List<Product> toReturn </returns>
        public List <Domain.Product> GetProductByName(string name)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var products = _context.Set <Product>().Where(x => name == x.Name).ToList();
            List <Domain.Product> toReturn = new List <Domain.Product>();

            foreach (var p in products)
            {
                var x = new Domain.Product(p.Name, (decimal)p.Price)
                {
                    ID = p.Id
                };
                toReturn.Add(x);
            }

            return(toReturn);
        }
Пример #30
0
        public async Task <IIdentifierResult> HandleAsync(ICreateProduct command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var product = new Domain.Product(command.Id, command.DisplayName, command.RecommendedPrice);
            await _repository.AddAsync(product);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("product_already_exists", $"A product with the ID '{command.Id}' already exists.");
            }

            await _busPublisher.Publish(new ProductCreated(product.Id, product.DisplayName, product.RecommendedPrice));

            return(new IdentifierResult(product.Id));
        }
        // CRUD Location

        /// <summary>
        /// Returns the list of all Locations from the database.
        /// </summary>
        /// <returns> List<Location> toReturn </returns>
        public List <Domain.Location> GetAllLocations()
        {
            var dbLocations = _context.Set <Location>().ToList();
            List <Domain.Location> toReturn = new List <Domain.Location>();

            foreach (var l in dbLocations)
            {
                var n = new Domain.Location(l.Id)
                {
                    Name = l.Name
                };
                var inventories = _context.Set <Inventory>().Where(i => i.LocationId == l.Id).ToList();
                foreach (var i in inventories)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);
                    n.SetProductAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
Пример #32
0
 public MenuProduct(Domain.Menu menu, Domain.Product product, decimal price)
 {
     this.menu = menu;
     this.product = product;
     this.Price = price;
 }