Example #1
0
        public bool AddProduct(Product productToAdd)
        {
            if (!IsProductToAddValid(productToAdd))
            {
                return(false);
            }

            _context.Products.Add(productToAdd);
            _context.SaveChanges();
            return(true);
        }
        public bool AddProduct(Product productToAdd)
        {
            var doesProductExist = _context.Products.Any(product =>
                                                         string.Equals(product.Name, productToAdd.Name, StringComparison.CurrentCultureIgnoreCase));

            if (doesProductExist || DoesBarcodeExist(productToAdd.Barcode, productToAdd.Name))
            {
                return(false);
            }

            _context.Products.Add(productToAdd);
            _context.SaveChanges();
            return(true);
        }
Example #3
0
        public Guid?AddReceipt(Receipt receiptToAdd)
        {
            if (receiptToAdd.CashierId == null || receiptToAdd.DateOfIssue == null || receiptToAdd.RegisterId == null)
            {
                return(null);
            }
            receiptToAdd.ReceiptProducts.ToList().ForEach(receiptProduct =>
                                                          receiptProduct.Product = _context.Products.Find(receiptProduct.ProductId));
            var areProductsUnavailable = receiptToAdd.ReceiptProducts.Any(receiptProduct =>
                                                                          receiptProduct.Product.AvailableAmount < receiptProduct.Quantity);

            if (areProductsUnavailable)
            {
                return(null);
            }

            receiptToAdd.ReceiptProducts.ToList().ForEach(receiptProduct =>
            {
                receiptProduct.Product.AvailableAmount -= receiptProduct.Quantity;
            });
            receiptToAdd.Cashier     = _context.Cashiers.Find(receiptToAdd.CashierId);
            receiptToAdd.DateOfIssue = DateTime.Now;
            _context.Receipts.Add(receiptToAdd);
            _context.SaveChanges();
            return(receiptToAdd.Id);
        }
Example #4
0
        public void Update_ProductIsUpdated_UpdatedProductCanBeFoundInDb()
        {
            var testProduct = new Product("Kildevand", 18, true);

            using (var context = new CashRegisterContext())
            {
                context.Products.Add(testProduct);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut = new Repository <Product>(context);

                var modifiedProduct = context.Products.FirstOrDefault(p => p.Id == 1);
                modifiedProduct.Price = 20;

                uut.Update(modifiedProduct);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var modifiedProduct = context.Products.FirstOrDefault(p => p.Id == 1);

                var result = context.Products.FirstOrDefault(p => p.Id == 1);
                Assert.That(modifiedProduct.Price, Is.EqualTo(20));
            }
        }
Example #5
0
        public void Get_ProductIsRequestedWithEmptyFilteredClause_ArgumentExceptionIsThrown()
        {
            var testProduct1 = new Product("Kildevand", 18, true);
            var testProduct2 = new Product("Øl", 20, false);

            var testProductGroup = new ProductGroup()
            {
                Name = "Drikkevarer"
            };

            testProductGroup.Products.Add(testProduct1);
            testProductGroup.Products.Add(testProduct2);

            using (var context = new CashRegisterContext())
            {
                context.ProductGroups.Add(testProductGroup);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut    = new Repository <ProductGroup>(context);
                var filter = new[] { "" };
                Assert.That(() => uut.Get(null, null, filter).ElementAt(0), Throws.ArgumentException);
            }
        }
Example #6
0
        public void Get_ProductIsRequestedWithFilteredClauseWithoutLazyLoading_ProductCollectionContainsNoProductList()
        {
            var testProduct1 = new Product("Kildevand", 18, true);
            var testProduct2 = new Product("Øl", 20, false);

            var testProductGroup = new ProductGroup()
            {
                Name = "Drikkevarer"
            };

            testProductGroup.Products.Add(testProduct1);
            testProductGroup.Products.Add(testProduct2);

            using (var context = new CashRegisterContext())
            {
                context.ProductGroups.Add(testProductGroup);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                context.Configuration.LazyLoadingEnabled = false;
                var uut    = new Repository <ProductGroup>(context);
                var result = uut.Get().ElementAt(0);
                Assert.That(result.Products.Count, Is.EqualTo(0));
            }
        }
        public bool AddCashierRegister(int registerId, int cashierId)
        {
            var doesRegisterExist = _context.Registers.Any(register => register.Id.Equals(registerId));
            var doesCashierExist  = _context.Cashiers.Any(cashier => cashier.Id.Equals(cashierId));

            if (!doesRegisterExist || !doesCashierExist)
            {
                return(false);
            }

            var newCashierRegister = new CashierRegister()
            {
                RegisterId = registerId, CashierId = cashierId
            };

            var doesCashierRegisterExist = _context.CashierRegisters.Any(cashierRegister =>
                                                                         cashierRegister.RegisterId.Equals(registerId) && cashierRegister.CashierId.Equals(cashierId));

            if (doesCashierRegisterExist)
            {
                return(true);
            }

            _context.CashierRegisters.Add(newCashierRegister);
            _context.SaveChanges();

            return(true);
        }
        public async Task Return_OK_Status_Code_When_Successful()
        {
            var databaseName = nameof(Return_OK_Status_Code_When_Successful);

            var options = CashRegisterService_Utilities.GetOptions(databaseName);

            // Arrange
            var context = new CashRegisterContext(options);

            var banknote = new Banknotes()
            {
                Fifty = 1,
                One   = 1,
            };

            context.Banknotes.Add(banknote);

            context.SaveChanges();


            var sut = new CashRegisterService(context);

            var withdrawAmount = 51;
            var response       = await sut.HandleCashWithdraw(withdrawAmount);

            Assert.IsTrue(response.StatusCode == 200);
        }
        public async Task Update_Database_With_Withdrawn_Banknotes()
        {
            var databaseName = nameof(Update_Database_With_Withdrawn_Banknotes);

            var options = CashRegisterService_Utilities.GetOptions(databaseName);

            // Arrange
            var context = new CashRegisterContext(options);

            var banknote = new Banknotes()
            {
                Fifty = 1,
                One   = 1,
            };

            context.Banknotes.Add(banknote);

            context.SaveChanges();


            // Act
            var sut = new CashRegisterService(context);

            var withdrawAmount = 51;
            var response       = await sut.HandleCashWithdraw(withdrawAmount);

            // Assert
            using (var actAndAssertContext = new CashRegisterContext(options))
            {
                Assert.IsTrue(actAndAssertContext.Banknotes.FirstOrDefault().Fifty == 0);
                Assert.IsTrue(actAndAssertContext.Banknotes.FirstOrDefault().One == 0);
            };
        }
        public async Task Return_Model_With_Withdrawn_Banknotes()
        {
            var databaseName = nameof(Return_Model_With_Withdrawn_Banknotes);

            var options = CashRegisterService_Utilities.GetOptions(databaseName);

            // Arrange
            var context = new CashRegisterContext(options);

            var banknote = new Banknotes()
            {
                Fifty = 1,
                One   = 1,
            };

            context.Banknotes.Add(banknote);

            context.SaveChanges();


            var sut = new CashRegisterService(context);

            var withdrawAmount = 51;
            var response       = await sut.HandleCashWithdraw(withdrawAmount);

            Assert.IsNotNull(response.Model);
        }
        public async Task Return_Failure_Message_When_Banknotes_Cannot_Fulfil_Withdraw()
        {
            var databaseName = nameof(Return_Failure_Message_When_Banknotes_Cannot_Fulfil_Withdraw);

            var options = CashRegisterService_Utilities.GetOptions(databaseName);

            // Arrange
            var context = new CashRegisterContext(options);

            var banknote = new Banknotes()
            {
                Fifty = 1,
                One   = 1,
            };

            context.Banknotes.Add(banknote);

            context.SaveChanges();


            var sut = new CashRegisterService(context);

            var withdrawAmount = 41;
            var response       = await sut.HandleCashWithdraw(withdrawAmount);

            Assert.IsTrue(response.Message == Messages.BanknotesCannotFulfilPayment);
        }
        public bool AddProduct(Product productToAdd)
        {
            if (string.IsNullOrEmpty(productToAdd.Barcode) || string.IsNullOrEmpty(productToAdd.Name) ||
                productToAdd.PriceWithTax <= 0 || productToAdd.AvailableAmount <= 0)
            {
                return(false);
            }
            var doesProductExist = _context.Products.Any(product =>
                                                         string.Equals(product.Barcode, productToAdd.Barcode, StringComparison.CurrentCultureIgnoreCase));

            if (doesProductExist)
            {
                return(false);
            }

            _context.Products.Add(productToAdd);
            _context.SaveChanges();
            return(true);
        }
Example #13
0
        public bool AddReceiptProductList(List <ReceiptProduct> receiptProductsToAdd)
        {
            foreach (var receiptProduct in receiptProductsToAdd)
            {
                var receipt       = _context.Receipts.Find(receiptProduct.ReceiptId);
                var product       = _context.Products.Find(receiptProduct.ProductId);
                var alreadyExists =
                    _context.ReceiptProducts.Any(rp => rp.ReceiptId == receiptProduct.ReceiptId &&
                                                 rp.ProductId == receiptProduct.ProductId);

                if (alreadyExists ||
                    receiptProduct.Quantity == 0 ||
                    receipt == null ||
                    product == null ||
                    product.InStock < receiptProduct.Quantity)
                {
                    return(false);
                }

                receiptProduct.UnitPrice = product.Price;
                receiptProduct.TaxType   = product.TaxType;

                receipt.PriceSubtotal += Math.Round(
                    receiptProduct.UnitPrice * receiptProduct.Quantity,
                    2,
                    MidpointRounding.AwayFromZero);

                if (product.TaxType == TaxType.Excise)
                {
                    receipt.TotalExciseTax += Math.Round(
                        receiptProduct.UnitPrice * 0.05 * receiptProduct.Quantity,
                        2,
                        MidpointRounding.AwayFromZero);
                }
                else
                {
                    receipt.TotalDirectTax += Math.Round(
                        receiptProduct.UnitPrice * 0.25 * receiptProduct.Quantity,
                        2,
                        MidpointRounding.AwayFromZero);
                }

                receipt.PriceTotal = Math.Round(
                    receipt.PriceSubtotal + receipt.TotalExciseTax + receipt.TotalDirectTax,
                    2,
                    MidpointRounding.AwayFromZero);

                product.InStock -= receiptProduct.Quantity;

                _context.ReceiptProducts.Add(receiptProduct);
            }

            _context.SaveChanges();
            return(true);
        }
Example #14
0
        public bool Add(Product productToAdd)
        {
            var productExists = _context.Products.Any(product => product.Equals(productToAdd));

            if (productExists)
            {
                return(false);
            }

            if (productToAdd.Barcode.GetType() != typeof(Guid) || productToAdd.Price.GetType() != typeof(float) ||
                productToAdd.TaxRate > 100 || productToAdd.TaxRate < 0 || productToAdd.Name.Length > 45 || productToAdd.Name.Length < 2 ||
                productToAdd.Count.GetType() != typeof(int))
            {
                return(false);
            }


            _context.Products.Add(productToAdd);
            _context.SaveChanges();
            return(true);
        }
Example #15
0
        public bool AddProduct(Product productToAdd)
        {
            var doesProductExist = _context.Products.Any(product =>
                string.Equals
                (
                    product.Barcode, 
                    productToAdd.Barcode, 
                    StringComparison.CurrentCultureIgnoreCase)
                );
            if
            (
                doesProductExist ||
                !StringHelper.IsDigitsOnly(productToAdd.Barcode) ||
                productToAdd.Barcode.Length != 13
            )
            {
                return false;
            }

            _context.Products.Add(productToAdd);
            _context.SaveChanges();
            return true;
        }
Example #16
0
        public void Insert_ProductIsInserted_ProductCanBeFoundInDb()
        {
            var product = new Product("Øl", 20, true);

            using (var context = new CashRegisterContext())
            {
                var uut = new Repository <Product>(context);
                uut.Insert(product);
                context.SaveChanges();

                var result = context.Products.FirstOrDefault(p => p.Id == 1);
                Assert.That(result, Is.EqualTo(product));
            }
        }
        public bool Add(Receipt receiptToAdd)
        {
            var receiptExists = _context.Receipts.Any(receipt => receipt.Equals(receiptToAdd));

            if (receiptExists)
            {
                return(false);
            }

            receiptToAdd.Cashier = _context.Cashiers.Find(receiptToAdd.CashierId);
            _context.Receipts.Add(receiptToAdd);
            _context.SaveChanges();
            return(true);
        }
Example #18
0
        public Guid AddReceipt(Receipt receiptToAdd)
        {
            var cashier      = _context.Cashiers.Find(receiptToAdd.CashierId);
            var cashRegister = _context.CashRegisters.Find(receiptToAdd.CashRegisterId);

            if (cashier == null || cashRegister == null)
            {
                return(Guid.Empty);
            }

            receiptToAdd.CreatedOn = receiptToAdd.CreatedOn.AddHours(2);
            _context.Receipts.Add(receiptToAdd);
            _context.SaveChanges();
            return(receiptToAdd.Id);
        }
Example #19
0
        public void Get_ProductsIsRequested_ProductNameIsInCollectionFromDb()
        {
            var testProduct = new Product("Kildevand", 18, true);

            using (var context = new CashRegisterContext())
            {
                context.Products.Add(testProduct);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut    = new Repository <Product>(context);
                var result = uut.Get().ElementAt(0);
                Assert.That(result.Name, Is.EqualTo("Kildevand"));
            }
        }
Example #20
0
        public void GetById_ProductWithId1IsRequested_ProductIsReturnedFromDbPriceIsEqual()
        {
            var testProduct = new Product("Kildevand", 18, true);

            using (var context = new CashRegisterContext())
            {
                context.Products.Add(testProduct);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut    = new Repository <Product>(context);
                var result = uut.GetById((long)1);
                Assert.That(result.Price, Is.EqualTo(18));
            }
        }
Example #21
0
        public void Get_ProductIsRequestedWithOrderByClause_ProductId1IsReturned()
        {
            var testProduct1 = new Product("Kildevand", 18, true);
            var testProduct2 = new Product("Øl", 20, false);

            using (var context = new CashRegisterContext())
            {
                context.Products.Add(testProduct1);
                context.Products.Add(testProduct2);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut    = new Repository <Product>(context);
                var result = uut.Get(null, q => q.OrderByDescending(x => x.Price)).ElementAt(0);
                Assert.That(result.Name, Is.EqualTo("Øl"));
            }
        }
Example #22
0
        public static CashRegisterContext FillContextWithUserData(DbContextOptions <CashRegisterContext> options)
        {
            var context = new CashRegisterContext(options);

            var banknote = new Banknotes()
            {
                Fifty  = 1,
                Twenty = 1,
                Ten    = 1,
                Five   = 1,
                Two    = 1,
                One    = 1
            };

            context.Banknotes.Add(banknote);

            context.SaveChanges();

            return(context);
        }
Example #23
0
        public void Delete_ProductIsDeleted_NoProductsInDatabase()
        {
            var testProduct = new Product("Kildevand", 18, true);

            using (var context = new CashRegisterContext())
            {
                context.Products.Add(testProduct);
                context.SaveChanges();
            }

            using (var context = new CashRegisterContext())
            {
                var uut = new Repository <Product>(context);

                uut.Delete(testProduct);
                context.SaveChanges();

                var result = context.Products.AsEnumerable();
                Assert.That(result, Is.Empty);
            }
        }
Example #24
0
        public Guid AddReceipt(AddReceiptDto addReceiptDto)
        {
            if (IsReceiptNonValid(addReceiptDto))
            {
                return(Guid.Empty);
            }

            var cashRegister = _context.CashRegisters.Find(addReceiptDto.CashRegisterId);

            if (cashRegister == null)
            {
                return(Guid.Empty);
            }

            var cashier = _context.Cashiers.Find(addReceiptDto.CashierId);

            if (cashier == null)
            {
                return(Guid.Empty);
            }

            var receiptToAdd = new Receipt(
                DateTime.Now,
                addReceiptDto.TaxFreePrice,
                addReceiptDto.TotalExciseTax,
                addReceiptDto.TotalDirectTax,
                addReceiptDto.TotalPrice,
                cashRegister,
                cashier);

            _context.Receipts.Add(receiptToAdd);

            AddProductsOnReceiptAndChangeAq(receiptToAdd.Id, addReceiptDto);

            _context.SaveChanges();
            return(receiptToAdd.Id);
        }
Example #25
0
        public bool AddCashier(Cashier toAdd)
        {
            var alreadyExists = _context.Cashiers.Any(cashier =>
                                                      string.Equals(cashier.Username, toAdd.Username, StringComparison.CurrentCulture));

            if (alreadyExists ||
                toAdd.Username.Length < 5 ||
                toAdd.Password.Length < 3)
            {
                return(false);
            }

            var cashierToBeAdded = new Cashier
            {
                FirstName = toAdd.FirstName,
                LastName  = toAdd.LastName,
                Username  = toAdd.Username,
                Password  = HashHelper.Hash(toAdd.Password)
            };

            _context.Cashiers.Add(cashierToBeAdded);
            _context.SaveChanges();
            return(true);
        }
Example #26
0
        private static void Main(string[] args)
        {
            _logger = LogFactory.GetLogger(typeof(Program));

            IDatabaseInitializer <CashRegisterContext> seed;

            // Empty
            // seed = new EmptyInitializer();

            // Kalle Seed
            //seed = new CashProductInitializer();

            // Lærke Seed
            seed = new FullProductInitializer();

            using (var contex = new CashRegisterContext(seed))
            {
                Console.WriteLine("FLAF");
                contex.Database.Initialize(true);
                contex.SaveChanges();
            }


            IDalFacade         dalFacade = new DalFacade();
            IProductDao        pd        = new ProductDao(dalFacade);
            IProductController pc        = new ProductController(pd);

            SalesOrder o;

            using (var uow = dalFacade.UnitOfWork)
            {
                var d = new Discount
                {
                    Description = "Discount",
                    Percent     = 0,
                };
                uow.DiscountRepository.Insert(d);
                uow.Save();

                o = new SalesOrder
                {
                    Date   = DateTime.Now,
                    Status = OrderStatus.Created,
                };
                uow.SalesOrderRepository.Insert(o);
            }
            using (var uow = dalFacade.UnitOfWork)
            {
                var t = new Transaction
                {
                    Date        = DateTime.Now,
                    Description = "Flaf",
                    PaymentType = PaymentType.Cash,
                    Price       = 20,
                    SalesOrder  = o,
                    Status      = TransactionStatus.Created
                };
                uow.TransactionRepository.Insert(t);
                uow.Save();
            }


            Console.WriteLine("ProductTabs");
            foreach (var productTab in pc.ProductTabs)
            {
                Console.WriteLine(productTab.Priority + ": " + productTab.Name);
                foreach (var productType in productTab.ProductTypes)
                {
                    Console.WriteLine("\t" + productType.Name);
                    foreach (var productGroup in productType.ProductGroups)
                    {
                        Console.WriteLine("\t\t" + productGroup.Name);
                        foreach (var product in productGroup.Products)
                        {
                            Console.WriteLine("\t\t\t" + product.Name);
                        }
                    }
                }
            }


            _logger.Fatal("Fatal");
            _logger.Err("Error");
            _logger.Warn("Warn");
            _logger.Info("Info");
            _logger.Debug("Debug");
        }
 void ICashRegisterRepository.AddCashRegister(Data.Entities.Models.CashRegister cashRegisterToAdd)
 {
     _context.CashRegisters.Add(cashRegisterToAdd);
     _context.SaveChanges();
 }
Example #28
0
 /// <summary>
 /// To save the changes to the database context
 /// </summary>
 public void Save()
 {
     _context.SaveChanges();
 }