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 CommentsViewModel(Comment comment) { var user = db.Users.Find(comment.ApplicationUserId); this.CommentId = comment.CommentId; this.CouponId = comment.CouponId; this.Content = comment.Content; this.CouponRate = comment.CouponRate; this.Name = user.FirstName; this.ApplicationUserId = comment.ApplicationUserId; this.TimePosted = comment.TimePosted; this.AvatarUrl = user.AvatarUrl; }