public ActionResult TrailReviewCreate(TrailReviewCreate model) { if (ModelState.IsValid) { var service = CreateTrailReviewService(); if (service.ValidateTrailReviewCreate(model)) { TempData["SaveResult"] = "Review was created."; return(RedirectToAction("Index")); } else { TempData["FailResult"] = "User already has a review for this trail in the system. Please edit your existing review."; } return(RedirectToAction("Index")); } else { TempData["FailResult"] = "Review was not able to be created."; } return(RedirectToAction("Index")); }
//VALIDATE VISIT DATE ON CREATE public DateTime ValidateVisitDate(TrailReviewCreate model) { var todaysDate = DateTime.Today; if (model.VisitDate > todaysDate) { return(model.VisitDate = DateTime.Today); } else { return(model.VisitDate); } }
//CREATE TRAIL REVIEW public bool CreateTrailReview(TrailReviewCreate model) { var entity = new TrailReview() { OwnerId = _userId, TrailId = model.TrailId, Title = model.Title, Comment = model.Comment, Score = model.Score, VisitDate = ValidateVisitDate(model) }; using (var ctx = new ApplicationDbContext()) { ctx.TrailReviews.Add(entity); return(ctx.SaveChanges() == 1); } }
//VALIDATION OF TRAIL REVIEW CREATE public bool ValidateTrailReviewCreate(TrailReviewCreate model) { using (var ctx = new ApplicationDbContext()) { var query = ctx .TrailReviews .Where(r => r.OwnerId == _userId && r.TrailId == model.TrailId && r.IsDeleted == false); if (query.Count() >= 1) { return(false); } else { return(CreateTrailReview(model)); } } }