public void DeleteLink(Link link)
 {
     link.LinkComments.ToList().ForEach(x => _entityContainer.LinkComments.Remove(x));
     link.LinkVotes.ToList().ForEach(x => _entityContainer.LinkVotes.Remove(x));
     link.LinkClicks.ToList().ForEach(x => _entityContainer.LinkClicks.Remove(x));
     link.Tags.ToList().ForEach(x => link.Tags.Remove(x));
     _entityContainer.Links.Remove(link);
     _entityContainer.SaveChanges();
 }
 public void AddLink(Link link)
 {
     _entityContainer.Links.Add(link);
     _entityContainer.SaveChanges();
 }
 public void UpdateLink(Link link)
 {
     _entityContainer.SaveChanges();
 }
Example #4
0
        public void Invoke(string[] args)
        {
            bool showHelp = false;
            string sortBy = null;
            string tags = null;
            string searchTerms = null;
            bool newLink = false;
            bool refresh = false;

            var options = new OptionSet();
            options.Add(
                "?|help",
                "Show help information.",
                x => showHelp = x != null
            );
            options.Add(
                "nl|newLink",
                "Create a new link.",
                x => newLink = x != null
            );
            options.Add(
                "sb|sortBy=",
                "Sort by DATE|RATING|CLICKS|REPLIES.",
                x => sortBy = x
            );
            options.Add(
                "t|tags=",
                "The tags to include. Ex: -t=games,news,food",
                x => tags = x
            );
            options.Add(
                "s|search=",
                "Filter links by search terms. Ex: -search=anime,pizza",
                x => searchTerms = x
            );
            options.Add(
                "R|refresh",
                "Refresh the current page.",
                x => refresh = x != null
            );

            if (args == null)
            {
                WriteLinks(0, null, null, sortBy);
            }
            else
            {
                try
                {
                    var parsedArgs = options.Parse(args).ToArray();

                    if (args.Length == 0)
                    {
                        WriteLinks(0, null, null, sortBy);
                    }
                    else
                    {
                        if (showHelp)
                        {
                            HelpUtility.WriteHelpInformation(
                                this.CommandResult,
                                this.Name,
                                this.Parameters,
                                this.Description,
                                options
                            );
                        }
                        else if (refresh)
                        {
                            WriteLinks(
                                this.CommandResult.CommandContext.CurrentPage,
                                this.CommandResult.CommandContext.CurrentLinkTags,
                                this.CommandResult.CommandContext.CurrentSearchTerms,
                                this.CommandResult.CommandContext.CurrentSortOrder
                            );
                        }
                        else if (newLink)
                        {
                            if (this.CommandResult.CommandContext.PromptData == null)
                            {
                                this.CommandResult.WriteLine("Enter the title of your topic.");
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "NEW LINK Title");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                            {
                                this.CommandResult.WriteLine("Enter the URL of your link.");
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "NEW LINK Url");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 2)
                            {
                                this.CommandResult.WriteLine("Enter a description of your link.");
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "NEW LINK Description");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 3)
                            {
                                var allTags = _linkRepository.GetTags();
                                var tagString = new StringBuilder();
                                foreach (var tag in allTags)
                                    tagString.Append(tag.Name).Append(", ");
                                this.CommandResult.WriteLine("Available Tags: {0}", tagString.ToString().Trim(',', ' '));
                                this.CommandResult.WriteLine();
                                this.CommandResult.WriteLine("Enter up to five tags for your link (comma-delimited).");
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "NEW LINK Tags");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 4)
                            {
                                var link = new Link
                                {
                                    Title = this.CommandResult.CommandContext.PromptData[0],
                                    URL = this.CommandResult.CommandContext.PromptData[1],
                                    Description = BBCodeUtility.SimplifyComplexTags(
                                        this.CommandResult.CommandContext.PromptData[2],
                                        _replyRepository,
                                        this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator
                                    ),
                                    Username = this.CommandResult.CurrentUser.Username,
                                    Date = DateTime.UtcNow
                                };
                                var tagList = this.CommandResult.CommandContext.PromptData[3].Replace(" ", "").Split(',').ToList();
                                if (tagList.Count > 5)
                                {
                                    tagList.RemoveRange(5, tagList.Count - 5);
                                    this.CommandResult.WriteLine("You supplied more than five tags. Only the first five were used.");
                                }
                                foreach (var tagName in tagList)
                                {
                                    var tag = _linkRepository.GetTag(tagName);
                                    if (tag != null)
                                        link.Tags.Add(tag);
                                    else
                                        this.CommandResult.WriteLine("'{0}' was not a valid tag and was not added.", tagName.ToUpper());
                                }
                                _linkRepository.AddLink(link);
                                this.CommandResult.CommandContext.Restore();
                                //var LINK = this.AvailableCommands.SingleOrDefault(x => x.Name.Is("LINK"));
                                //LINK.Invoke(new string[] { link.LinkID.ToString() });
                                //this.CommandResult.WriteLine("New topic succesfully posted.");
                            }
                        }
                        else
                        {
                            var page = 0;
                            bool displayLinks = true;
                            if (parsedArgs.Length == 1)
                            {
                                if (parsedArgs[0].IsInt())
                                    page = parsedArgs[0].ToInt();
                                else if (PagingUtility.Shortcuts.Any(x => parsedArgs[0].Is(x)))
                                {
                                    page = PagingUtility.TranslateShortcut(parsedArgs[0], this.CommandResult.CommandContext.CurrentPage);
                                    if (parsedArgs[0].Is("last") || parsedArgs[0].Is("prev"))
                                        this.CommandResult.ScrollToBottom = false;
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid page number.", parsedArgs[0]);
                                    displayLinks = false;
                                }
                            }

                            if (displayLinks)
                            {
                                List<string> tagList = null;
                                if (tags != null)
                                {
                                    tagList = new List<string>();
                                    foreach (var tagName in tags.Split(',').ToList())
                                        if (_linkRepository.GetTag(tagName) != null)
                                            tagList.Add(tagName);

                                }
                                else
                                    tagList = this.CommandResult.CommandContext.CurrentLinkTags;
                                List<string> searchTermList = null;
                                if (searchTerms != null)
                                    searchTermList = searchTerms.Split(',').ToList();
                                else
                                    searchTermList = this.CommandResult.CommandContext.CurrentSearchTerms;
                                var sortOptions = new List<string>
                                {
                                    "DATE",
                                    "RATINGS",
                                    "CLICKS",
                                    "REPLIES"
                                };
                                if (sortBy != null && !sortOptions.Any(x => sortBy.ToUpper() == x))
                                    sortBy = this.CommandResult.CommandContext.CurrentSortOrder;
                                WriteLinks(page, tagList, searchTermList, sortBy);
                            }
                        }
                    }
                }
                catch (OptionException ex)
                {
                    this.CommandResult.WriteLine(ex.Message);
                }
            }
        }