internal void Purchase(int clothingItemId) { using (ClothingStoreDBContext db = new ClothingStoreDBContext()) { var repo = new ClothingStoreRepository(db); ClothingItem clothingItem; //@@ Find Clothing Item try { clothingItem = repo.GetClothingItemById(clothingItemId); } catch (InvalidOperationException) { throw new InvalidOperationException("Invalid Clothing Item Id!"); } //@@ Check if Purchased var isPurchased = repo.IsClothingItemPurchased(clothingItemId); if (isPurchased) { throw new InvalidOperationException("Can't purchase item. Already purchased"); } //@@ Add New Purchase repo.AddPurchase(clothingItem); repo.Save(); } }
internal void Return(int clothingItemId) { using (ClothingStoreDBContext db = new ClothingStoreDBContext()) { var repo = new ClothingStoreRepository(db); ClothingItem clothingItem; //@@ Find Clothing Item try { clothingItem = repo.GetClothingItemById(clothingItemId); } catch (InvalidOperationException) { throw new Exception("Invalid Clothing Item Id!"); } var clothingItemReturn = new ClothingItemReturn(); //@@ Check Return Policy var returnPolicy = repo.GetReturnPolicyForClothingItem(clothingItemId); ClothingItemPurchase clothingItemPurchase; try { clothingItemPurchase = repo.GetPurchaseForClothingItem(clothingItemId); } catch (InvalidOperationException) { throw new InvalidOperationException("Can't return item. Purchase not Made for Item"); } if (returnPolicy != null) { var daysPassedSincePurchase = (DateTime.Now - clothingItemPurchase.DatePurchashed).Days; if (daysPassedSincePurchase <= returnPolicy.DaysForFullRefund) { clothingItemReturn.IsStoreCredit = false; } else if (daysPassedSincePurchase <= returnPolicy.DaysForStoreCredit) { clothingItemReturn.IsStoreCredit = true; } else { throw new InvalidOperationException("Can't return item. Item does nor meet return / refund policy"); } } //@@ Add New Return clothingItemReturn.DateReturned = DateTime.Now; clothingItemReturn.ReturnedAmmount = clothingItem.Price; clothingItemReturn.ClothingItemId = clothingItemId; repo.RemovePurchase(clothingItemPurchase); repo.AddReturn(clothingItemReturn); repo.Save(); } }