public ActionResult Report(int? commentId, string ApplicationUserId, int? AccomodationIdValue) { if (commentId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } string testUserName = "******"; try { testUserName = db.UserComments.SingleOrDefault(x => x.ApplicationUserId == ApplicationUserId && x.UserCommentId == commentId && x.AccomodationId == AccomodationIdValue.Value).UserName; } catch { } if(testUserName != "null") { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "UNABLE TO REPORT"); } int testId = -1; try { testId = db.CommentUserReports.SingleOrDefault(x => x.AccomodationId == AccomodationIdValue && x.ApplicationUserId == ApplicationUserId && x.CommentId == commentId && x.IsReported == true).CommentId; } catch { CommentUserReport newCommentUserReport = new CommentUserReport() { AccomodationId = AccomodationIdValue.Value, ApplicationUserId = ApplicationUserId, CommentId = commentId.Value, CommentUserReportId = 0, IsReported = true }; db.CommentUserReports.Add(newCommentUserReport); db.SaveChanges(); var userComment = db.UserComments.Find(commentId); userComment.ReportCount++; db.Entry(userComment).State = EntityState.Modified; db.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK, "THANK YOU FOR REPORT"); } if (testId != -1 && String.IsNullOrEmpty(ApplicationUserId)) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "UNABLE TO REPORT"); } else if (testId != -1) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "UNABLE TO REPORT"); } return RedirectToAction("Index"); }
public IHttpActionResult PostReport([FromBody]UserCommentReportAPI report) { string userName = User.Identity.Name; string userId = ""; try { userId = db.Users.SingleOrDefault(x => x.Email == userName).Id; } catch { return BadRequest("Please sign in to report this comment!"); } //userComment.ApplicationUserId = userId; if (report.CommentId == 0) { return BadRequest("This comment does not exist for this accomodation."); } string testAppUserId = "null"; try { testAppUserId = db.UserComments.Single(x => x.ApplicationUserId == report.ApplicationUserId && x.UserCommentId == report.CommentId && x.AccomodationId == report.AccomodationIdValue).ApplicationUserId; } catch { } if (testAppUserId == userId) { return BadRequest("You are not able to report your own comment."); } int testId = -1; try { testId = db.CommentUserReports.Single(x => x.AccomodationId == report.AccomodationIdValue && x.ApplicationUserId == userId && x.CommentId == report.CommentId && x.IsReported == true).CommentId; } catch { CommentUserReport newCommentUserReport = new CommentUserReport() { AccomodationId = report.AccomodationIdValue, ApplicationUserId = userId, CommentId = report.CommentId, CommentUserReportId = 0, IsReported = true }; db.CommentUserReports.Add(newCommentUserReport); db.SaveChanges(); var userComment = db.UserComments.Find(report.CommentId); userComment.ReportCount++; db.Entry(userComment).State = EntityState.Modified; db.SaveChanges(); return Ok("Thank you for reporting comment."); } if (testId != -1 && String.IsNullOrEmpty(userId)) { return BadRequest("Please sign in to report this comment!"); } else if (testId != -1) { return BadRequest("You have already reported this comment!"); } return Ok(); }