private static double GetPriceBasedOnQuantityAndLoss(IceCream iceCream, int price, int quantity)
 {
     var totalQuantity = quantity + iceCream.Quantity;
     var fraction1 = (double) (quantity)*price;
     var fraction2 = (iceCream.Quantity)*iceCream.Price;
     var fraction3 = (fraction1 + fraction2 + iceCream.Loss.SafeValue()*iceCream.Price)/totalQuantity;
     return fraction3;
 }
 public void BuyIceThrowsHttpExceptionIfThereAreZeroIcesLeft()
 {
     var ice = new CiberIs.Models.IceCream {Quantity = 0};
     A.CallTo(() => _mongoDb.FindById<CiberIs.Models.IceCream>(A<string>.Ignored, A<string>.Ignored))
      .Returns(ice);
     var exception =
         Assert.Throws<HttpResponseException>(
             () => _controller.Post(new FormDataCollection(Enumerable.Empty<KeyValuePair<string, string>>())));
     Assert.AreEqual(exception.Response.StatusCode, HttpStatusCode.Conflict);
 }
 public ActionResult RegisterForIce(string objectId, IceCream iceCream)
 {
     var ice = _db.FindById<IceCream>(objectId, "IceCreams");
     if(ice.Quantity < iceCream.Loss.SafeValue()) {
         throw new ArgumentException("Loss cannot be greater than the registered quantity");
     }
     ice.Loss = iceCream.Loss.SafeValue();
     ice.Quantity -= ice.Loss.SafeValue();
     _db.Save(ice, "IceCreams");
     return RedirectToAction("Index", "Admin");
 }
 public void BuyIceReducesQuantityWithOne()
 {
     var ice = new CiberIs.Models.IceCream {Quantity = 2};
     A.CallTo(() => _mongoDb.FindById<CiberIs.Models.IceCream>(A<string>.Ignored, A<string>.Ignored))
      .Returns(ice);
     var keyValuePairs = new List<KeyValuePair<string, string>>
         {
             new KeyValuePair<string, string>("buyer", "561")
         };
     _controller.Post(new FormDataCollection(keyValuePairs));
     Assert.AreEqual(1, ice.Quantity);
 }
 public void BuyIceCallsSaveWithUpdatedQuantity()
 {
     var ice = new CiberIs.Models.IceCream {Quantity = 2};
     A.CallTo(() => _mongoDb.FindById<CiberIs.Models.IceCream>(A<string>.Ignored, A<string>.Ignored))
      .Returns(ice);
     var keyValuePairs = new List<KeyValuePair<string, string>>
         {
             new KeyValuePair<string, string>("buyer", "561")
         };
     _controller.Post(new FormDataCollection(keyValuePairs));
     A.CallTo(() => _mongoDb.Save(A<CiberIs.Models.IceCream>.Ignored, A<string>.Ignored)).MustHaveHappened();
 }
 public dynamic Post(IceCream iceCream)
 {
     if (iceCream == null) throw new HttpResponseException(HttpStatusCode.BadRequest);
     try
     {
         _mongoDb.Insert(iceCream, "IceCreams");
     }
     catch (MongoException e)
     {
         return new {success = false, errorMessage = e.Message};
     }
     return new { success = true, errorMessage = string.Empty, iceCreamId = iceCream.Id.ToString() };
 }
 public void BuyIceInsertsPurchase()
 {
     var ice = new CiberIs.Models.IceCream {Quantity = 2, Price = 7};
     A.CallTo(() => _mongoDb.FindById<CiberIs.Models.IceCream>(A<string>.Ignored, A<string>.Ignored))
      .Returns(ice);
     var keyValuePairs = new List<KeyValuePair<string, string>>
         {
             new KeyValuePair<string, string>("buyer", "561")
         };
     _controller.Post(new FormDataCollection(keyValuePairs));
     A.CallTo(
         () =>
         _mongoDb.Insert(A<Purchase>.That.Matches(x => x.Buyer == 561 && x.Price == ice.Price), "Purchases"))
      .MustHaveHappened();
 }
 public void BuyIceWhenExceptionReturnsSuccssFalse()
 {
     var ice = new CiberIs.Models.IceCream {Quantity = 2, Price = 7};
     A.CallTo(() => _mongoDb.FindById<CiberIs.Models.IceCream>(A<string>.Ignored, A<string>.Ignored))
      .Returns(ice);
     A.CallTo(() => _mongoDb.Save(A<CiberIs.Models.IceCream>.Ignored, A<string>.Ignored))
      .Throws(new MongoException("sorry"));
     var keyValuePairs = new List<KeyValuePair<string, string>>
         {
             new KeyValuePair<string, string>("buyer", "561")
         };
     var result = _controller.Post(new FormDataCollection(keyValuePairs));
     Assert.IsFalse(result.success);
 }