public int CreatePurchase(PurchaseModel pm) { int result = DaoUtilities.NO_CHANGES; PURCHASE p = db.PURCHASE.Create(); p.PURCHASE_ID = pm.PurchaseId; p.PURCHASE_USERID = pm.UserId; p.PURCHASE_DATE = pm.PurchaseDate; p.PURCHASE_SHIPDATE = pm.ShipDate; p.PURCHASE_TOTALPRICE = pm.TotalPrice; db.PURCHASE.Add(p); try { int saveResult = db.SaveChanges(); if (saveResult == 1) result = DaoUtilities.SAVE_SUCCESSFUL; } catch (DbUpdateConcurrencyException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION; } catch (DbUpdateException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UPDATE_EXCEPTION; } catch (DbEntityValidationException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION; } catch (NotSupportedException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UNSUPPORTED_EXCEPTION; } catch (ObjectDisposedException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.DISPOSED_EXCEPTION; } catch (InvalidOperationException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.INVALID_OPERATION_EXCEPTION; } return result; }
public int DeletePurchase(PurchaseModel pm) { int result = DaoUtilities.NO_CHANGES; PURCHASE p = db.PURCHASE.Find(pm.PurchaseId); if (p != null) { db.PURCHASE.Remove(p); try { int saveResult = db.SaveChanges(); if (saveResult == 1) result = DaoUtilities.SAVE_SUCCESSFUL; } catch (DbUpdateConcurrencyException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION; } catch (DbUpdateException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UPDATE_EXCEPTION; } catch (DbEntityValidationException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION; } catch (NotSupportedException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.UNSUPPORTED_EXCEPTION; } catch (ObjectDisposedException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.DISPOSED_EXCEPTION; } catch (InvalidOperationException e) { Console.WriteLine(e.GetBaseException().ToString()); result = DaoUtilities.INVALID_OPERATION_EXCEPTION; } } return result; }
public int CreateUserPurchase(string UserId) { int result = DaoUtilities.NO_CHANGES; IProductServices prs = new ProductServices(); IPurchaseServices pus = new PurchaseServices(); IPurchaseLineServices pls = new PurchaseLineServices(); IBasketServices bs = new BasketServices(); List<BasketModel> bm = FindBasketOfUser(UserId); PurchaseModel pm = new PurchaseModel(); pm.PurchaseId = ""; pm.PurchaseDate = DateTime.Today; pm.UserId = UserId; pm.TotalPrice = 0; List<PurchaseLineModel> purchaseLines = new List<PurchaseLineModel>(); List<ProductModel> products = new List<ProductModel>(); foreach (BasketModel bl in bm) { ProductModel prm = prs.FindProductWithId(bl.ProductId); prm.AvailableQuantity -= ((int)bl.Quantity); products.Add(prm); pm.TotalPrice += prm.Price * bl.Quantity; PurchaseLineModel plm = new PurchaseLineModel(); plm.ProductId = bl.ProductId; plm.Quantity = bl.Quantity; purchaseLines.Add(plm); } result = pus.CreatePurchase(pm); if(result == DaoUtilities.SAVE_SUCCESSFUL) { PURCHASE p = ((PURCHASE)db.PURCHASE.Where(PURCHASE => PURCHASE.PURCHASE_USERID == pm.UserId && PURCHASE.PURCHASE_TOTALPRICE == pm.TotalPrice && PURCHASE.PURCHASE_DATE == pm.PurchaseDate).FirstOrDefault()); foreach(PurchaseLineModel plm in purchaseLines) { plm.PurchaseId = p.PURCHASE_ID; result = pls.CreatePurchaseLine(plm); if (result != DaoUtilities.SAVE_SUCCESSFUL) break; } if (result == DaoUtilities.SAVE_SUCCESSFUL) { foreach (ProductModel prm in products) { result = prs.UpdateProduct(prm); if (result != DaoUtilities.SAVE_SUCCESSFUL) break; } if (result == DaoUtilities.SAVE_SUCCESSFUL) { result = DeleteUserBasket(UserId); } else { foreach (ProductModel prm in products) { ProductModel prm2 = prs.FindProductWithId(prm.ProductId); if(prm == prm2) { BasketModel bm2 = bs.FindBasketLineWithIds(UserId, prm.ProductId); prm.AvailableQuantity += ((int)bm2.Quantity); prs.UpdateProduct(prm); } } db.PURCHASE.Remove(p); } } else { db.PURCHASE.Remove(p); } } return result; }
/// <summary> /// Convert a purchase from the database to a PurchaseModel /// </summary> /// <param name="p"></param> /// <returns></returns> public PurchaseModel ConvertPurchaseToPurchaseModel(PURCHASE p) { PurchaseModel pm = new PurchaseModel(); if (p != null) { pm.PurchaseId = p.PURCHASE_ID; pm.UserId = p.PURCHASE_USERID; pm.PurchaseDate = p.PURCHASE_DATE; pm.ShipDate = p.PURCHASE_SHIPDATE; pm.TotalPrice = p.PURCHASE_TOTALPRICE; } else pm = null; return pm; }