public ActionResult Create([Bind(Include = "Id,Username,Password,Salt,Email,FirstName,LastName")] User user) { if (ModelState.IsValid) { try { user.Id = Guid.NewGuid(); user.Salt = CreateSalt(6); user.Password = CreateHash(user.Salt, user.Password); if (db.Users.Any(x => x.Email == user.Email)) { ModelState.AddModelError("EmailError", "Email adress already taken!"); return(View()); } var util = new RegexUtilities(); //check email with this mf if (!util.IsValidEmail(user.Email)) { ModelState.AddModelError("EmailError", "Not valid e-mail!"); } if (user.FirstName.Any(char.IsDigit)) { ModelState.AddModelError("FirstNameError", "Names can't contain digits!"); var newUser = new User(); return(View(newUser)); } if (user.LastName.Any(char.IsDigit)) { ModelState.AddModelError("FirstNameError", "Names can't contain digits!"); var newUser = new User(); return(View(newUser)); } db.Users.Add(user); db.SaveChanges(); } catch { RedirectToAction("Error", "Home"); } Session["currentUser"] = user; return(RedirectToAction("Index", "Reviews")); } return(View(user)); }
public ActionResult Create([Bind(Include = "Id,UserId,ReviewId,HasLiked,Rating")] UserToReview userToReview) { try { if (ModelState.IsValid) { userToReview.Id = Guid.NewGuid(); db.UserToReviews.Add(userToReview); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ReviewId = new SelectList(db.Reviews, "Id", "Title", userToReview.ReviewId); ViewBag.UserId = new SelectList(db.Users, "Id", "Username", userToReview.UserId); return(View(userToReview)); } catch { RedirectToAction("Error", "Home"); } return(View()); }
public ActionResult BatchDeleteReviews(Guid[] reviewIds) { if (reviewIds != null && reviewIds.Length > 0) { try { var reviewsToDelete = new List <Review>(); var commentsToDelete = new List <CommentToReview>(); var reactionsToDelete = new List <UserToReview>(); foreach (Guid t in reviewIds) { foreach (var review in db.Reviews) { if (review.Id == t) { reviewsToDelete.Add(review); } } foreach (var comment in db.CommentToReviews) { if (comment.ReviewId == t) { commentsToDelete.Add(comment); } } foreach (var reaction in db.UserToReviews) { if (reaction.ReviewId == t) { reactionsToDelete.Add(reaction); } } } foreach (var review in reviewsToDelete) { review.CommentToReviews.Clear(); review.UserToReviews.Clear(); db.Reviews.Remove(review); } foreach (var comment in commentsToDelete) { db.CommentToReviews.Remove(comment); } foreach (var reaction in reactionsToDelete) { db.UserToReviews.Remove(reaction); } db.SaveChanges(); } catch { return(RedirectToAction("Error", "Home")); } } var currUser = ((User)Session["currentUser"]).Username; return(RedirectToAction("Profile", "Users", new { username = currUser })); }
public JsonResult Create(CommentToReview newComment, Guid reviewId) { if (ModelState.IsValid) { try { //get all reviews var reviews = from m in db.Reviews select m; var users = from u in db.Users select u; newComment.UserId = ((User)Session["currentUser"]).Id; newComment.Id = Guid.NewGuid(); newComment.CreatedDate = DateTime.Now; if (newComment.Comment.Length < 3) { ModelState.AddModelError("CommentError", "Please actually write a comment!"); } //loop through all users, and add a user as the newComment.User property only if that user has the same Id as newComment.UserId **1 foreach (var user in users) { if (user.Id == newComment.UserId) { newComment.User = user; } } //loop through all reviews, and add a review as the "newComment.Review" property only if that review has the same ID as newComment.ReviewID **2 foreach (var review in reviews) { if (review.Id == newComment.ReviewId) { newComment.Review = review; } } // **1 and **2 had to be done for some reason otherwise Entity thougth that upon assigning "newComment.Review/User" that a new entity should be implicitly created(???!) //find current Review and add this comment to that review foreach (var review in reviews) { if (review.Id == newComment.ReviewId) { review.CommentToReviews.Add(newComment); } } db.SaveChanges(); } catch { RedirectToAction("Error", "Home"); } //maybe return(Json("success")); } //rly right? return(Json("success")); }