public IHttpActionResult PutRating(int id, Rating rating) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != rating.id) { return(BadRequest()); } db.Entry(rating).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!RatingExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult GetRating(int id) { Rating rating = db.Ratings.Find(id); if (rating == null) { return(NotFound()); } return(Ok(rating)); }
public IHttpActionResult DeleteRating(int id) { Rating rating = db.Ratings.Find(id); if (rating == null) { return(NotFound()); } db.Ratings.Remove(rating); db.SaveChanges(); return(Ok(rating)); }
public HttpResponseMessage PostRating(Rating rating) { string token = Request.Headers.Authorization.Parameter; JwtAuthUtil jwtAuthUtil = new JwtAuthUtil(); int Mid = Convert.ToInt32(jwtAuthUtil.GetId(token)); var OrderComment = db.Orders.Find(Mid); if (OrderComment.MemberId != Mid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new { success = false, message = "非訂購人員,無法評論" })); } if (OrderComment.Status != 3) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new { success = false, message = "訂單尚未完成,無法評價" })); } rating.MemberId = Mid; rating.CreateOn = DateTime.Now; //rating.Ratingtotal = db.Ratings.Where(x => x.id == OrderComment.TravelPlan_id).Sum(x => x.rating); //rating.StarAmount = db.Ratings.Where(x => x.id == OrderComment.TravelPlan_id).Average(x => x.star); db.Ratings.Add(rating); db.SaveChanges(); var result = db.Ratings.Where(x => x.id == rating.id).Select(x => new { x.id, x.TravelId, x.rating, x.RatingContent }); return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, message = "成功評論", result })); }