예제 #1
0
        public IActionResult Comment(CommentViewModel commentVM)
        {
            // find game by gameID
            var game = _gameRepository.GetGameById(commentVM.GameID);

            // if model is not valid
            if (!ModelState.IsValid)
            {
                // redirect to same page
                return(RedirectToAction(game.Name, "Game/Single"));
            }

            // if it is no main comment
            if (commentVM.MainCommentID == 0)
            {
                // create comments collection
                game.MainComments = game.MainComments ?? new List <MainComment>();

                // add new main comment into collection
                game.MainComments.Add(new MainComment
                {
                    Message  = commentVM.Message,
                    UserName = commentVM.UserName,
                    Created  = DateTime.Now
                });

                // add main comment into context
                _gameRepository.AddMainComment(game);

                //var comments = _gameRepository.Games.FirstOrDefault(g => g.Name == game.Name).MainComments;
            }

            // if maincommentID !=0 -> so we leave sub comment under main comment
            else
            {
                // create new sub comment
                var comment = new SubComment
                {
                    MainCommentID = commentVM.MainCommentID,
                    Message       = commentVM.Message,
                    UserName      = commentVM.UserName,
                    Created       = DateTime.Now
                };

                // add sub comment into context
                _gameRepository.AddSubComment(comment);
            }

            // redirect to the same page
            return(RedirectToAction("Single", new { gameName = game.Name }));
        }