/// <summary>
 /// Adds a topic to the data context.
 /// </summary>
 /// <param name="topic">The topic to be added.</param>
 public void AddTopic(Topic topic)
 {
     _entityContainer.Topics.Add(topic);
 }
 /// <summary>
 /// Deletes a topic from the data context.
 /// </summary>
 /// <param name="topic">The topic to be deleted.</param>
 public void DeleteTopic(Topic topic)
 {
     topic.Replies.ToList().ForEach(x => _entityContainer.Replies.Remove(x));
     _entityContainer.Topics.Remove(topic);
 }
 /// <summary>
 /// Updates an existing topic in the data context.
 /// </summary>
 /// <param name="topic">The topic to be updated.</param>
 public void UpdateTopic(Topic topic)
 {
 }
Ejemplo n.º 4
0
        public void Invoke(string[] args)
        {
            bool showHelp = false;
            bool newTopic = false;
            bool modTopic = false;
            bool refresh = false;
            bool? lockBoard = null;

            var options = new OptionSet();
            options.Add(
                "?|help",
                "Show help information.",
                x => showHelp = x != null
            );
            options.Add(
                "R|refresh",
                "Refresh the current board.",
                x => refresh = x != null
            );
            if (CommandResult.IsUserLoggedIn)
            {
                options.Add(
                    "nt|newTopic",
                    "Create new topic on the specified board.",
                    x => newTopic = x != null
                );
            }
            if (CommandResult.UserLoggedAndModOrAdmin())
            {
                options.Add(
                    "mt|modTopic",
                    "Create a topic that only moderators can see.",
                    x =>
                    {
                        newTopic = x != null;
                        modTopic = x != null;
                    }
                );
            }
            if (CommandResult.UserLoggedAndAdmin())
            {
                options.Add(
                    "l|lock",
                    "Lock the board to prevent creation of topics.",
                    x => lockBoard = x != null
                );
            }

            if (args == null)
            {
                CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
            }
            else
            {
                try
                {
                    var parsedArgs = options.Parse(args).ToArray();

                    if (parsedArgs.Length == args.Length)
                    {
                        if (parsedArgs.Length == 1)
                        {
                            if (parsedArgs[0].IsShort())
                            {
                                var boardId = parsedArgs[0].ToShort();
                                var page = 1;
                                WriteTopics(boardId, page);
                            }
                            else
                                CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                        }
                        else if (parsedArgs.Length == 2)
                        {
                            if (parsedArgs[0].IsShort())
                            {
                                var boardId = parsedArgs[0].ToShort();
                                if (parsedArgs[1].IsInt())
                                {
                                    var page = parsedArgs[1].ToInt();
                                    WriteTopics(boardId, page);
                                }
                                else if (PagingUtility.Shortcuts.Any(x => parsedArgs[1].Is(x)))
                                {
                                    var page = PagingUtility.TranslateShortcut(parsedArgs[1], CommandResult.CommandContext.CurrentPage);
                                    WriteTopics(boardId, page);
                                    if (parsedArgs[1].Is("last") || parsedArgs[1].Is("prev"))
                                        CommandResult.ScrollToBottom = false;
                                }
                                else
                                    CommandResult.WriteLine("'{0}' is not a valid page number.", parsedArgs[1]);
                            }
                            else
                                CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                        }
                        else
                            CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
                    }
                    else
                    {
                        if (showHelp)
                        {
                            HelpUtility.WriteHelpInformation(this, options);
                        }
                        else if (newTopic)
                        {
                            if (parsedArgs.Length >= 1)
                            {
                                if (parsedArgs[0].IsShort())
                                {
                                    var boardId = parsedArgs[0].ToShort();
                                    var board = _dataBucket.BoardRepository.GetBoard(boardId);
                                    if (board != null)
                                    {
                                        if (!board.Locked || CommandResult.CurrentUser.IsModeratorOrAdministrator())
                                        {
                                            if (!board.ModsOnly || CommandResult.CurrentUser.IsModeratorOrAdministrator())
                                            {
                                                if (CommandResult.CommandContext.PromptData == null)
                                                {
                                                    CommandResult.WriteLine("Create a title for your topic.");
                                                    CommandResult.SetPrompt(Name, args, string.Format("{0} NEW TOPIC Title", boardId));
                                                }
                                                else if (CommandResult.CommandContext.PromptData.Length == 1)
                                                {
                                                    CommandResult.WriteLine("Create the body for your topic.");
                                                    CommandResult.SetPrompt(Name, args, string.Format("{0} NEW TOPIC Body", boardId));
                                                }
                                                else if (CommandResult.CommandContext.PromptData.Length == 2)
                                                {
                                                    var topic = new Topic
                                                    {
                                                        BoardID = boardId,
                                                        Title = CommandResult.CommandContext.PromptData[0],
                                                        Body = BBCodeUtility.SimplifyComplexTags(
                                                            CommandResult.CommandContext.PromptData[1],
                                                            _dataBucket.ReplyRepository,
                                                            CommandResult.CurrentUser.IsModeratorOrAdministrator()
                                                        ),
                                                        Username = CommandResult.CurrentUser.Username,
                                                        PostedDate = DateTime.UtcNow,
                                                        LastEdit = DateTime.UtcNow,
                                                        ModsOnly = modTopic && !board.ModsOnly
                                                    };
                                                    _dataBucket.TopicRepository.AddTopic(topic);
                                                    _dataBucket.SaveChanges();
                                                    CommandResult.RestoreContext();
                                                    var TOPIC = AvailableCommands.SingleOrDefault(x => x.Name.Is("TOPIC"));
                                                    TOPIC.Invoke(new string[] { topic.TopicID.ToString() });
                                                    CommandResult.WriteLine("New topic succesfully posted.");
                                                }
                                            }
                                            else
                                                CommandResult.WriteLine("Board '{0}' is for moderators only.", boardId);
                                        }
                                        else
                                            CommandResult.WriteLine("Board '{0}' is locked. You cannot create topics on this board.", boardId);
                                    }
                                    else
                                        CommandResult.WriteLine("There is no board with ID '{0}'.", boardId);

                                }
                                else
                                    CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                            }
                            else
                                CommandResult.WriteLine("You must supply a board ID.");
                        }
                        else
                        {
                            if (lockBoard != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsShort())
                                    {
                                        var boardId = parsedArgs[0].ToShort();
                                        var board = _dataBucket.BoardRepository.GetBoard(boardId);
                                        if (board != null)
                                        {
                                            board.Locked = (bool)lockBoard;
                                            _dataBucket.BoardRepository.UpdateBoard(board);
                                            _dataBucket.SaveChanges();
                                            string status = (bool)lockBoard ? "locked" : "unlocked";
                                            CommandResult.WriteLine("Board '{0}' was successfully {1}.", boardId, status);
                                        }
                                        else
                                            CommandResult.WriteLine("There is no board with ID '{0}'.", boardId);
                                    }
                                    else
                                        CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                                else
                                    CommandResult.WriteLine("You must supply a board ID.");
                            }
                            if (refresh)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsShort())
                                    {
                                        var boardId = parsedArgs[0].ToShort();
                                        WriteTopics(boardId, CommandResult.CommandContext.CurrentPage);
                                    }
                                    else
                                        CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                                else
                                    CommandResult.WriteLine("You must supply a board ID.");
                            }
                        }
                    }
                }
                catch (OptionException ex)
                {
                    CommandResult.WriteLine(ex.Message);
                }
            }
        }