Exemplo n.º 1
0
        public void Delete(string userId, int receiptId, int productId)
        {
            var product = new DatabaseModel.Product()
            {
                Id        = productId,
                UserId    = userId,
                ReceiptId = receiptId
            };

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var productCount = db.Product.Count(p => p.UserId == userId && p.ReceiptId == receiptId);

                db.Product.Attach(product);
                db.Product.Remove(product);

                if (productCount == 1)
                {
                    var receipt = new DatabaseModel.Receipt()
                    {
                        Id     = receiptId,
                        UserId = userId
                    };

                    db.Receipt.Attach(receipt);
                    db.Receipt.Remove(receipt);
                }

                UpdateReceiptControlSum(receiptId, userId, db);

                db.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public void AddEmpty(string userId)
        {
            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var databaseReceipt = new DatabaseModel.Receipt();

                databaseReceipt.Id      = GenerateReceiptIdForUser(db, userId);
                databaseReceipt.UserId  = userId;
                databaseReceipt.AddDate = DateTime.Now;

                db.Receipt.Add(databaseReceipt);

                var firstProduct = new DatabaseModel.Product();
                firstProduct.Id         = 1;
                firstProduct.UserId     = userId;
                firstProduct.ReceiptId  = databaseReceipt.Id;
                firstProduct.Name       = "Update me";
                firstProduct.Price      = 0m;
                firstProduct.Quantity   = 0;
                firstProduct.CategoryId = 12;

                db.Product.Add(firstProduct);

                db.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public void Update(string userId, int receiptId, int productId, Product updatedProduct)
        {
            if (updatedProduct == null)
            {
                throw new ArgumentNullException("updatedProduct");
            }

            var dbUpdatedProduct = productMapper.MapToDatabase(updatedProduct);

            dbUpdatedProduct.Id        = productId;
            dbUpdatedProduct.UserId    = userId;
            dbUpdatedProduct.ReceiptId = receiptId;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                db.Product.Attach(dbUpdatedProduct);
                var entry = db.Entry(dbUpdatedProduct);

                entry.Property(e => e.Name).IsModified       = true;
                entry.Property(e => e.Price).IsModified      = true;
                entry.Property(e => e.Quantity).IsModified   = true;
                entry.Property(e => e.CategoryId).IsModified = true;

                UpdateReceiptControlSum(receiptId, userId, db);

                db.SaveChanges();
            }
        }
Exemplo n.º 4
0
        private void UpdateReceiptControlSum
            (int receiptId, string userId, DatabaseModel.ReceiptReaderDatabaseContext db)
        {
            var receipt = db.Receipt.FirstOrDefault(r => r.UserId == userId && r.Id == receiptId);

            receipt.ControlSum = receipt.Product.Sum(p => p.Price * p.Quantity);
        }
Exemplo n.º 5
0
        public Collection <Category> GetUserReceiptCategories(string userId, int receiptId)
        {
            var domainCategories = new Collection <Category>();

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var receipt = db.Receipt.FirstOrDefault(r => (r.UserId == userId) && (r.Id == receiptId));

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

                var dbCategories = new List <DatabaseModel.Category>();

                foreach (var dbProduct in receipt.Product)
                {
                    dbCategories.Add(dbProduct.Category);
                }

                var distinctDbCategories = dbCategories.Distinct();

                foreach (var dbCategory in distinctDbCategories)
                {
                    domainCategories.Add(categoryMapper.MapFromDatabase(dbCategory));
                }
            }

            return(domainCategories);
        }
Exemplo n.º 6
0
        public void Add(string userId, Receipt receipt)
        {
            if (receipt == null)
            {
                throw new ArgumentNullException("receipt");
            }

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var databaseReceipt = receiptMapper.MapToDatabase(receipt);
                databaseReceipt.Id      = GenerateReceiptIdForUser(db, userId);
                databaseReceipt.UserId  = userId;
                databaseReceipt.AddDate = DateTime.Now;

                db.Receipt.Add(databaseReceipt);

                int count = 1;
                foreach (Product product in receipt.Products)
                {
                    var databaseProduct = productMapper.MapToDatabase(product);
                    databaseProduct.Id        = count;
                    databaseProduct.UserId    = userId;
                    databaseProduct.ReceiptId = databaseReceipt.Id;

                    db.Product.Add(databaseProduct);
                    count++;
                }

                db.SaveChanges();
            }

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var newCustomizedProductId = customizedProductService.GenerateId(userId, db);

                foreach (Product product in receipt.Products)
                {
                    if (customizedProductService.CheckForExisting(product, userId, receipt.PurchasePlace, db) == false)
                    {
                        var dbCustomizedProduct = customizedProductService.MapToDatabase(product);
                        dbCustomizedProduct.UserId        = userId;
                        dbCustomizedProduct.PurchasePlace = receipt.PurchasePlace;

                        dbCustomizedProduct.Id = newCustomizedProductId;
                        newCustomizedProductId++;

                        db.CustomizedProduct.Add(dbCustomizedProduct);
                    }
                }

                db.SaveChanges();
            }
        }
Exemplo n.º 7
0
        private int GenerateProductIdForReceipt(DatabaseModel.ReceiptReaderDatabaseContext db, string userId, int receiptId)
        {
            var query = db.Product.Where(p => p.UserId == userId && p.ReceiptId == receiptId);

            if (query.Count() == 0)
            {
                return(1);
            }

            var lastMax = query.Max(r => r.Id);

            return(lastMax + 1);
        }
Exemplo n.º 8
0
        public Receipt GetUserReceiptById(string userId, int receiptId)
        {
            DatabaseModel.Receipt receipt;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                receipt = db.Receipt.FirstOrDefault(r => (r.UserId == userId) && (r.Id == receiptId));
            }

            var domainReceipt = receiptMapper.MapFromDatabase(receipt);

            return(domainReceipt);
        }
Exemplo n.º 9
0
        private int GenerateReceiptIdForUser(DatabaseModel.ReceiptReaderDatabaseContext db, string userId)
        {
            var query = db.Receipt.Where(r => r.UserId == userId);

            if (query.Count() == 0)
            {
                return(1);
            }

            var lastMax = query.Max(r => r.Id);

            return(lastMax + 1);
        }
Exemplo n.º 10
0
        public int GenerateId(string userId, DatabaseModel.ReceiptReaderDatabaseContext db)
        {
            var query = db.CustomizedProduct.Where(p => p.UserId == userId);

            if (query.Count() == 0)
            {
                return(1);
            }

            var lastMax = query.Max(r => r.Id);

            return(lastMax + 1);
        }
Exemplo n.º 11
0
        public Product GetUserProductById(string userId, int receiptId, int productId)
        {
            var domainProduct = new Product();

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var dbProduct = db.Product.FirstOrDefault(p => (p.UserId == userId) && (p.ReceiptId == receiptId) &&
                                                          (p.Id == productId));

                domainProduct = productMapper.MapFromDatabase(dbProduct);
            }

            return(domainProduct);
        }
Exemplo n.º 12
0
        public byte[] GetUserReceiptImage(string userId, int receiptId)
        {
            byte[] imageBytes = null;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var receipt = db.Receipt.FirstOrDefault(r => (r.UserId == userId) && (r.Id == receiptId));

                if (receipt != null)
                {
                    imageBytes = receipt.Image;
                }
            }

            return(imageBytes);
        }
Exemplo n.º 13
0
        public Collection <Product> GetAllUserProducts(string userId)
        {
            var domainProducts = new Collection <Product>();

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var dbProducts = db.Product.Where(p => p.UserId == userId);

                foreach (var product in dbProducts)
                {
                    var domainProduct = productMapper.MapFromDatabase(product);
                    domainProducts.Add(domainProduct);
                }
            }

            return(domainProducts);
        }
Exemplo n.º 14
0
        public void Delete(string userId, int receiptId)
        {
            var receipt = new DatabaseModel.Receipt()
            {
                Id     = receiptId,
                UserId = userId
            };

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                db.Product.RemoveRange(db.Product.Where(p => p.UserId == userId && p.ReceiptId == receiptId));

                db.Receipt.Attach(receipt);
                db.Receipt.Remove(receipt);

                db.SaveChanges();
            }
        }
Exemplo n.º 15
0
        public Collection <Receipt> GetAllUserReceipts(string userId)
        {
            List <DatabaseModel.Receipt> receipts;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                receipts = db.Receipt.Where(r => r.UserId == userId).ToList();
            }

            var domainReceipts = new Collection <Receipt>();

            foreach (var receipt in receipts)
            {
                var domainReceipt = receiptMapper.MapFromDatabase(receipt);
                domainReceipts.Add(domainReceipt);
            }

            return(domainReceipts);
        }
Exemplo n.º 16
0
        public Collection <Product> GetUserProductsByReceipt(string userId, int receiptId)
        {
            var domainProducts = new Collection <Product>();

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var dbProducts = db.Product.Where(p => (p.UserId == userId) && (p.ReceiptId == receiptId));

                if (dbProducts.Count() == 0)
                {
                    return(null);
                }

                foreach (var dbProduct in dbProducts)
                {
                    var domainProduct = productMapper.MapFromDatabase(dbProduct);
                    domainProducts.Add(domainProduct);
                }
            }

            return(domainProducts);
        }
Exemplo n.º 17
0
        public void Update(string userId, int receiptId, Receipt updatedReceipt)
        {
            if (updatedReceipt == null)
            {
                throw new ArgumentNullException("updatedReceipt");
            }

            var dbUpdatedReceipt = receiptMapper.MapToDatabase(updatedReceipt);

            dbUpdatedReceipt.UserId = userId;
            dbUpdatedReceipt.Id     = receiptId;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                db.Receipt.Attach(dbUpdatedReceipt);
                var entry = db.Entry(dbUpdatedReceipt);

                entry.Property(e => e.PurchaseDate).IsModified  = true;
                entry.Property(e => e.PurchasePlace).IsModified = true;

                db.SaveChanges();
            }
        }
Exemplo n.º 18
0
        public void Add(string userId, int receiptId, Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var databaseProduct = productMapper.MapToDatabase(product);
                databaseProduct.Id        = GenerateProductIdForReceipt(db, userId, receiptId);
                databaseProduct.UserId    = userId;
                databaseProduct.ReceiptId = receiptId;

                db.Product.Add(databaseProduct);
                UpdateReceiptControlSum(receiptId, userId, db);

                db.SaveChanges();
            }

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var receipt = db.Receipt.FirstOrDefault(r => r.Id == receiptId);

                if (customizedProductService.CheckForExisting(product, userId, receipt.PurchasePlace, db) == false)
                {
                    var dbCustomizedProduct = customizedProductService.MapToDatabase(product);
                    dbCustomizedProduct.Id            = customizedProductService.GenerateId(userId, db);
                    dbCustomizedProduct.UserId        = userId;
                    dbCustomizedProduct.PurchasePlace = receipt.PurchasePlace;

                    db.CustomizedProduct.Add(dbCustomizedProduct);

                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 19
0
        public ProductWithCategory SuggestProductCategoryId(string userId, string productName, string purchasePlace)
        {
            var    bestFitValue       = -1.0;
            var    bestFitCategory    = -1;
            string bestFitProductName = null;

            using (var db = new DatabaseModel.ReceiptReaderDatabaseContext())
            {
                var basicProducs = db.BasicProduct.ToList();

                foreach (var basicProduct in basicProducs)
                {
                    var nameFit = algorithm.CompareStrings(productName, basicProduct.Name);

                    if (nameFit > bestFitValue)
                    {
                        bestFitValue       = nameFit;
                        bestFitCategory    = basicProduct.CategoryId;
                        bestFitProductName = basicProduct.Name;
                    }
                }

                if (bestFitValue >= BasicProductThreshold)
                {
                    return(new ProductWithCategory()
                    {
                        ProductName = bestFitProductName, CategoryId = bestFitCategory
                    });
                }

                var customizedProducts = db.CustomizedProduct.Where(cp => cp.UserId == userId).ToList();

                if (purchasePlace != null)
                {
                    customizedProducts = customizedProducts.Where(cp => cp.PurchasePlace == purchasePlace).ToList();
                }

                foreach (var customizedProduct in customizedProducts)
                {
                    var nameFit = algorithm.CompareStrings(productName, customizedProduct.Name);

                    if (nameFit > bestFitValue)
                    {
                        bestFitValue       = nameFit;
                        bestFitCategory    = customizedProduct.CategoryId;
                        bestFitProductName = customizedProduct.Name;
                    }
                }
            }

            if (bestFitValue < OtherCategoryThreshold)
            {
                return(new ProductWithCategory()
                {
                    ProductName = null, CategoryId = OtherCategoryId
                });
            }

            return(new ProductWithCategory()
            {
                ProductName = bestFitProductName, CategoryId = bestFitCategory
            });
        }
Exemplo n.º 20
0
 public bool CheckForExisting(Product product, string userId, string purchasePlace, DatabaseModel.ReceiptReaderDatabaseContext db)
 {
     if (purchasePlace != null)
     {
         return(db.BasicProduct.Any(bp => bp.Name == product.Name) ||
                db.CustomizedProduct.Any(cp => cp.UserId == userId && cp.Name == product.Name));
     }
     else
     {
         return(db.BasicProduct.Any(bp => bp.Name == product.Name) ||
                db.CustomizedProduct.Any(cp => cp.UserId == userId && cp.PurchasePlace == purchasePlace && cp.Name == product.Name));
     }
 }