public ActionResult Edit(string id, string username, double rating, string text) { double newRating = rating < 0 ? 0 : rating > 10 ? 10 : rating; try { Comment commentToedit = _commentBl.GetById(int.Parse(id)); User user = _userBl.GetById(username); if (commentToedit == null) { return(RedirectToAction("Index", "Error", new { error = string.Format("Could not find comment with username {0}", id) })); } commentToedit.Text = text; commentToedit.Publisher = user; commentToedit.Rating = newRating; commentToedit.Date = DateTime.Now; _commentBl.Update(commentToedit); return(RedirectToAction("Details", "Comment")); } catch { return(RedirectToAction("Index", "Error")); } }
// GET: Recommendation public ActionResult Index() { double min = int.MaxValue; var closestUserName = ""; string userName = HttpContext.Session.GetString("ConnectedUserId"); if (String.IsNullOrEmpty(userName)) { return(RedirectToAction("Index", "Error", new { error = string.Format("Could not find current user") })); } try { var user = _userBl.GetById(userName); // dictionary of score of user var usersScore = _commentBl.GetAll().GroupBy(c => c.Publisher.UserName) .Select(g => new { g.Key, Value = g.Sum(x => x.Rating) }).ToDictionary(x => x.Key, x => x.Value); // get current user score var userScore = usersScore[userName]; usersScore.Remove(userName); // get closest user for current user foreach (var item in usersScore) { if (min > Math.Abs(userScore - item.Value)) { min = userScore - item.Value; closestUserName = item.Key; } } var allProducts = _productBl.GetAllProducts(); var comment = allProducts.FindAll(p => p.Comments.Any(c => c.Publisher != user && c.Publisher.UserName == closestUserName)).SelectMany(p => p.Comments).ToList().FindAll(c => c.Publisher.UserName == closestUserName).OrderByDescending(c => c.Rating).FirstOrDefault(); if (comment == null) { var highestComment = allProducts.SelectMany(p => p.Comments).ToList().OrderByDescending(c => c.Rating).FirstOrDefault(); var recommendedHeightestProduct = allProducts.First(p => p.Comments.ToList().Contains(comment)); return(View(recommendedHeightestProduct)); } var recommendedProduct = allProducts.First(p => p.Comments.ToList().Contains(comment)); return(View(recommendedProduct)); } catch (Exception e) { return(RedirectToAction("Index", "Error", new { error = string.Format("an error while get recommendation please try again") })); } }
// GET: User/Edit/ public ActionResult Edit(string id) { if (!IsAdminConnected()) { return(RedirectToAction("Index", "Error", new { error = "You must be an admin to edit. please go to admin page" })); } try { User user = _userBl.GetById(id); if (user == null) { return(RedirectToAction("Index", "Error", new { error = string.Format("Could not find user with id {0}", id) })); } return(View(user)); } catch { return(RedirectToAction("Index", "Error")); } }