public IHttpActionResult PostComment(Comment comment) { int id = comment.CouponId; string userId = this.User.Identity.GetUserId(); if (this.User.IsInRole("Buyer")) { int items = db.Items.Where(x => x.CouponId == id && x.Cart.ApplicationUserId == userId).Count(); if (items == 0) //if user didn't bought this coupon, don't allow comment coupon return BadRequest(); } var comm = db.Comments.Where(x => x.CouponId == id && x.ApplicationUserId == userId && x.CouponRate > 0).FirstOrDefault(); if (comm != null) comment.CouponRate = 0; comment.ApplicationUserId = this.User.Identity.GetUserId(); comment.TimePosted = DateTime.Now; if (ModelState.IsValid) { db.Comments.Add(comment); db.SaveChanges(); var viewModelComment = new CommentsViewModel(comment); return Ok(viewModelComment); } return CreatedAtRoute("DefaultApi", new { id = comment.CommentId }, comment); }
public IHttpActionResult DeleteComment(int id) { Comment comment = db.Comments.Find(id); if (comment == null) { return NotFound(); } var commentViewModel = new CommentsViewModel(comment); if (comment.ApplicationUserId == this.User.Identity.GetUserId() || this.User.IsInRole("Admin") || this.User.IsInRole("SuperAdmin")) { db.Comments.Remove(comment); db.SaveChanges(); return Ok(commentViewModel); } else return BadRequest(); }
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; }