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(); } }
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(); } } }