示例#1
0
        public ActionResult Comment(string comment, int id)
        {
            RedisUserRepository redisUser = new RedisUserRepository();
            RedisOreRepository redisOre = new RedisOreRepository();
            RedisCommentRepository redisComment = new RedisCommentRepository();
            Ore parentOre = redisOre.ReadOre(id);

            if (Context.Session.GetString("user") == null)
            {
                return RedirectToAction("Login", "Community");
            }
            User author = JsonConvert.DeserializeObject<User>(Context.Session.GetString("user"));

            List<string> validation = new List<string>();
            if (comment == null)
            {
                validation.Add("Please enter your comment");
            }
            if (id == 0)
            {
                return RedirectToAction("Index", "Ore");
            }
            ViewData["validationSummary"] = validation;

            if(!validation.Any())
            {
                Comment target = new Comment();
                target.Content = comment;
                target.Author = author.UserName;
                target.ParentOre = parentOre.Name;
                redisComment.CreateComment(target);

                if(author.CommentsAuthored != null)
                {
                    author.CommentsAuthored.Add(target.Id);
                    redisUser.UpdateUser(author);
                }
                else
                {
                    List<int> commentsAuthored = new List<int>();
                    commentsAuthored.Add(target.Id);
                    author.CommentsAuthored = commentsAuthored;
                    redisUser.UpdateUser(author);
                }

                if(parentOre.Comments != null)
                {
                    parentOre.Comments.Add(target.Id);
                    redisOre.UpdateOre(parentOre);
                }
                else
                {
                    List<int> comments = new List<int>();
                    comments.Add(target.Id);
                    parentOre.Comments = comments;
                    redisOre.UpdateOre(parentOre);
                }
            }

            ViewData["commentSuccess"] = true;
            return View("Info", parentOre);
        }
示例#2
0
 public ActionResult Edit(Ore ore)
 {
     RedisUserRepository redisUser = new RedisUserRepository();
     RedisOreRepository redisOre = new RedisOreRepository();
     User author = JsonConvert.DeserializeObject<User>(Context.Session.GetString("user"));
     // Validation Checks
     List<string> validation = new List<string>();
     if (ore.Name == null)
     {
         validation.Add("Please name your ore");
     }
     if (ore.Description == null)
     {
         validation.Add("Please set a description of your ore");
     }
     if (redisOre.ReadName(ore.Name) != null) // If the name is used in the database
     {
         if(author.OresAuthored != null) // If the author has authored ores
         {
             if (!author.OresAuthored.Contains(ore.Name)) // Ores Authored is populated but does not contain the ore
             {
                 validation.Add("You do not have permission to edit this ore");
             } // No else because if the author.OresAuthored contains the ore, it's good to go
         }
         else // Ores Authored is empty
         {
             validation.Add("You do not have permission to edit this ore");
         }
     }
     ViewData["validationSummary"] = validation;
     if (!validation.Any())
     {
         if(ore.Comments == null)
         {
             List<int> comments = new List<int>();
             ore.Comments = comments;
         }
         // Save ore to DB
         ore.AuthorId = author.Id;
         redisOre.UpdateOre(ore);
         if (author.OresAuthored == null)
         {
             List<string> list = new List<string>();
             list.Add(ore.Name);
             author.OresAuthored = list;
         }
         else
         {
             if(!author.OresAuthored.Contains(ore.Name))
             {
                 author.OresAuthored.Add(ore.Name); // If the ore isn't already in the author's list
             }
         }
         redisUser.UpdateUser(author);
         // Save ore to App Data
         return RedirectToAction("Profile", "Community");
     }
     else
     {
         // Model is not valid, return view to the user
         return View(ore);
     }
 }