Esempio n. 1
0
 /// <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);
     _entityContainer.SaveChanges();
 }
Esempio n. 2
0
 /// <summary>
 /// Updates an existing topic in the data context.
 /// </summary>
 /// <param name="topic">The topic to be updated.</param>
 public void UpdateTopic(Topic topic)
 {
     _entityContainer.SaveChanges();
 }
Esempio n. 3
0
 /// <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);
     _entityContainer.SaveChanges();
 }
Esempio n. 4
0
File: BOARD.cs Progetto: xcjs/u413
        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
            );
            options.Add(
                "nt|newTopic",
                "Create new topic on the specified board.",
                x => newTopic = x != null
            );
            if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "mt|modTopic",
                    "Create a topic that only moderators can see.",
                    x =>
                    {
                        newTopic = x != null;
                        modTopic = x != null;
                    }
                );
            }
            if (this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "l|lock",
                    "Lock the board to prevent creation of topics.",
                    x => lockBoard = x != null
                );
            }

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

                                }
                                else
                                    this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                            }
                            else
                                this.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 = _boardRepository.GetBoard(boardId);
                                        if (board != null)
                                        {
                                            board.Locked = (bool)lockBoard;
                                            _boardRepository.UpdateBoard(board);
                                            string status = (bool)lockBoard ? "locked" : "unlocked";
                                            this.CommandResult.WriteLine("Board '{0}' was successfully {1}.", boardId, status);
                                        }
                                        else
                                            this.CommandResult.WriteLine("There is no board with ID '{0}'.", boardId);
                                    }
                                    else
                                        this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                                else
                                    this.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, this.CommandResult.CommandContext.CurrentPage);
                                    }
                                    else
                                        this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                                else
                                    this.CommandResult.WriteLine("You must supply a board ID.");
                            }
                        }
                    }
                }
                catch (OptionException ex)
                {
                    this.CommandResult.WriteLine(ex.Message);
                }
            }
        }