public static HttpStatusCodeResult CouponBuyingLogic(Coupon coupon,System.Web.Mvc.ModelStateDictionary modelState,string SessionId) { ApplicationDbContext db = new ApplicationDbContext(); if (modelState.IsValid) { coupon.TotalNumberOfCoupons -= coupon.RequiredNumberOfCoupons; coupon.Purchase++; Buy buy = new Buy() { CouponId = coupon.CouponId, ApplicationUserId = SessionId }; db.Buys.Add(buy); if (coupon.NuberOfCouponsToOfferManaged>0) coupon.NuberOfCouponsToOfferManaged -= coupon.RequiredNumberOfCoupons; db.Entry(coupon).State = EntityState.Modified; db.SaveChanges(); if (coupon.TotalNumberOfCoupons > 0) { return new HttpStatusCodeResult(HttpStatusCode.OK); } coupon.Acitve = false; db.Entry(coupon).State = EntityState.Modified; db.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
/// <summary> /// Checking for coupons existance /// </summary> /// <param name="id">id of searched coupon</param> /// <param name="coupon">does coupon exists in db under that id</param> /// <returns>HttpStatusCode.OK if searched id is in database /// otherwise BadRequest for id==null /// and NotFound if that instance doesnt exist in dataBase /// </returns> public static HttpStatusCode CouponsControllerLogic(Coupon coupon) { if (coupon == null) { return HttpStatusCode.NotFound; } else { return HttpStatusCode.OK; } }
public CouponViewModel(Coupon coupon) { this.Comments = new List<CommentsViewModel>(); this.Questions = new List<QuestionViewModel>(); this.CouponId = coupon.CouponId; this.Name = coupon.Name; this.Price = coupon.Price; this.NewPrice = coupon.NewPrice; this.DescriptionOnCoupon = coupon.DescriptionOnCoupon; this.DescriptionOnSellerPage = coupon.DescriptionOnSellerPage; this.ExpirationTime = coupon.ExpirationTime; this.NuberOfCouponsToOfferManaged = coupon.NuberOfCouponsToOfferManaged; this.RequiredNumberOfCoupons = coupon.RequiredNumberOfCoupons; this.TotalNumberOfCoupons = coupon.TotalNumberOfCoupons; this.SellerUrl = coupon.SellerUrl; this.PictureUrl = coupon.PictureUrl; this.CategoryId = coupon.CategoryId; this.Category = coupon.Category; this.SellerName = coupon.ApplicationUser.FirstName + " " + coupon.ApplicationUser.LastName; this.Images = db.Images.Where(x => x.CouponId == coupon.CouponId).ToList(); this.Locations = db.GoogleApis.Where(x => x.CouponId == coupon.CouponId).ToList(); List<Comment> comments = db.Comments.Where(x => x.CouponId == coupon.CouponId).ToList(); for (int i = 0; i < comments.Count; i++) { CommentsViewModel commentView = new CommentsViewModel(comments[i]); Comments.Add(commentView); } this.ApplicationUserId = coupon.ApplicationUserId; var questions = db.Questions.Where(x => x.CouponId == coupon.CouponId).ToList(); for (int i = 0; i < questions.Count; i++) { var question = new QuestionViewModel(questions[i]); this.Questions.Add(question); } this.CtgCoupons = db.Coupons.Where(x => x.CategoryId == this.CategoryId).Take(2).ToList(); var rates = db.Comments.Where(x => x.CouponId == coupon.CouponId && x.CouponRate != 0).Select(x => x.CouponRate).ToList(); if (rates.Count != 0) Rating = (int)rates.Average(); else Rating = 0; }
public IHttpActionResult PostCoupon(Coupon coupon) { HtmlSanitizer.SanitizeHtml(coupon.DescriptionOnSellerPage); coupon.ApplicationUserId = this.User.Identity.GetUserId(); coupon.IsDeleted = false; coupon.IsEdited = false; coupon.Acitve = true; coupon.Approved = false; coupon.PictureUrl = "/Images/placeholder.png"; coupon.Priority = 3; this.Validate(coupon); if (coupon.NuberOfCouponsToOfferManaged % coupon.RequiredNumberOfCoupons != 0 || coupon.NuberOfCouponsToOfferManaged > coupon.TotalNumberOfCoupons) { ModelState.AddModelError("NuberOfCouponsToOfferManaged", "Number of coupons to satisfy your ofer has to be divisible by required number of coupons"); return BadRequest(ModelState); } if (coupon.Price < coupon.NewPrice) { ModelState.AddModelError("NewPrice", "New Price has to be less then current price"); return BadRequest(ModelState); } if (ModelState.IsValid) { if ((coupon.TotalNumberOfCoupons % coupon.RequiredNumberOfCoupons) != 0 || coupon.TotalNumberOfCoupons < coupon.RequiredNumberOfCoupons) { ModelState.AddModelError("TotalNumberOfCoupons", "Total number of coupons doesn't match required coupon count."); return BadRequest(ModelState); } db.Coupons.Add(coupon); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = coupon.CouponId }, coupon); } return BadRequest(ModelState); }