示例#1
0
        public ActionResult Send(Message message)
        {
            message.Time = DateTime.Now;
            message.Sender = User.Identity.Name;

            if (User.Identity.GetUserId() == message.ApplicationUserId)  //user can't send message to himself
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);

            if (ModelState.IsValid)
            {
                db.Messages.Add(message);
                db.SaveChanges();
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }

            Response.StatusCode = 400;
            Response.TrySkipIisCustomErrors = true;
            var modelErrors = ModelState.AllErrors();
            return Json(modelErrors);
        }
        public IHttpActionResult AddReport(int id)
        {
            Comment comment = db.Comments.Find(id);
            string userId = this.User.Identity.GetUserId();

            if (comment.ApplicationUserId == userId)
                return Unauthorized();

            int buyers = db.Buys.Where(x => x.CouponId == comment.CouponId && x.ApplicationUserId == userId).Count();
            if (buyers == 0)  //if user didn't bought this coupon, don't allow him to report it
                return NotFound();

            Report report = db.Reports.Where(x => x.CommentId == id && x.ApplicationUserId == userId).SingleOrDefault();  //find report of selected comment

            if (report != null)  //if there is report then this user has already reported comment
                return BadRequest();
            else
            {
                Report newReport = new Report() { CommentId = id, ApplicationUserId = userId };  //if there is not report about selected coupon, create new one
                db.Reports.Add(newReport);
            }
            db.SaveChanges();

            int numberOfPurchases = db.Coupons.Find(comment.CouponId).Purchase;
            int counter = db.Reports.Where(x => x.CommentId == id).Count();

            //if there are more then 20% reports on selected comment, send message to admin
            if (((double)counter / (double)numberOfPurchases) > 0.2)
            {
                Message message = new Message() { Sender = "System", Content = "Comment with content: " + comment.Content + " has been reported more than 20%.", IsRead = false, Title = "Report about Comment", Time = DateTime.Now, CommentId = comment.CommentId };
                db.Messages.Add(message);
                db.SaveChanges();
            }

            return Ok();
        }