示例#1
0
 /// <summary>
 /// Returns a list of snippets for a given user's id
 /// </summary>
 /// <param name="userId">User id to return snippets for</param>
 /// <param name="count">Number of records to return. 0 returns 10 times default list size.</param>
 /// <returns></returns>
 public List <CodeSnippetListItem> GetSnippetsForUser(string userId, int count)
 {
     using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
     {
         return(codeSnippet.GetSnippetsForUser(userId, count));
     }
 }
示例#2
0
 /// <summary>
 /// Allows searching of snippets by providing a search parameter structure
 /// </summary>
 /// <returns></returns>
 public List <CodeSnippetListItem> SearchSnippets(CodeSnippetSearchParameters searchParameters)
 {
     using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
     {
         return(codeSnippet.GetSearchList(searchParameters).ToList());
     }
 }
示例#3
0
 public List <CodeSnippetListItem> ListSnippets(string filter, string filterParameter)
 {
     using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
     {
         List <CodeSnippetListItem> snippets = codeSnippet.GetSnippetList(filter, filterParameter);
         return(snippets);
     }
 }
示例#4
0
 /// <summary>
 /// Returns a list of recent snippets
 /// </summary>
 /// <returns></returns>
 public List <CodeSnippetListItem> GetRecentSnippets()
 {
     using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
     {
         List <CodeSnippetListItem> items = codeSnippet.GetSnippetList("recent", null);
         return(items);
     }
 }
示例#5
0
 /// <summary>
 /// Returns a new empty snippet to the client. The snippet
 /// contains a new id
 /// </summary>
 /// <returns></returns>
 public CodeSnippet GetNewSnippet()
 {
     using (busCodeSnippet codesnippet = CodePasteFactory.GetCodeSnippet())
     {
         if (codesnippet.NewEntity() == null)
         {
             this.ThrowException("Unable to load new snippet: " + codesnippet.ErrorMessage);
         }
         return(codesnippet.Entity);
     }
 }
示例#6
0
 /// <summary>
 /// Returns an individual snippet based on an id
 /// </summary>
 /// <param name="snippetId"></param>
 /// <returns></returns>
 public CodeSnippet GetSnippet(string id)
 {
     using (busCodeSnippet codesnippet = CodePasteFactory.GetCodeSnippet())
     {
         if (codesnippet.Load(id) == null)
         {
             this.ThrowException("Invalid code snippet id");
         }
         codesnippet.GetComments();
         codesnippet.StripSensitiveUserInformation();
         return(codesnippet.Entity);
     }
 }
示例#7
0
        public ActionResult ClearAnonymousSnippets()
        {
            busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet();
            int            result      = codeSnippet.ClearAnonymousSnippets(App.Configuration.HoursToDeleteAnonymousSnippets,
                                                                            App.Configuration.MinViewBeforeDeleteAnonymousSnippets);

            if (result < 0)
            {
                ErrorDisplay.ShowError(codeSnippet.ErrorMessage);
            }
            else
            {
                ErrorDisplay.ShowMessage((result).ToString() + " old snippets have been cleared out.");
            }

            return(View("Index", ViewModel));
        }
示例#8
0
        /// <summary>
        /// Allows deletion of an individual snippet by the author.
        /// </summary>
        /// <param name="snippetId"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool DeleteSnippet(string snippetId, string sessionKey)
        {
            User user = this.ValidateToken(sessionKey);

            using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
            {
                if (codeSnippet.Load(snippetId) == null)
                {
                    this.ThrowException("Invalid snippet specified");
                }
                if (codeSnippet.Entity.UserId != user.Id)
                {
                    this.ThrowException("Access denied: You can only delete snippets you posted with this user account");
                }
                return(codeSnippet.Delete());
            }
        }
示例#9
0
        /// <summary>
        /// Allows posting of a new code snippet.
        /// </summary>
        /// <param name="snippet"></param>
        /// <param name="sessionKey"></param>
        /// <returns></returns>
        public CodeSnippet PostNewCodeSnippet(CodeSnippet snippet, string sessionKey)
        {
            User user = this.ValidateToken(sessionKey);

            using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
            {
                if (snippet == null)
                {
                    this.ThrowException("Invalid snippet instance data passed");
                }
                CodeSnippet newSnippet = codeSnippet.NewEntity();
                // Force userId regardless of what the user has set
                newSnippet.UserId = user.Id;
                newSnippet.Author = user.Name;
                if (string.IsNullOrEmpty(newSnippet.Author))
                {
                    newSnippet.Author = snippet.Author;
                }

                if (string.IsNullOrEmpty(snippet.Language))
                {
                    snippet.Language = "NoFormat";
                }

                DataUtils.CopyObjectData(snippet, newSnippet, "Id,UserId,Entered,Views,UserId,User,Author,Comments");

                if (!codeSnippet.Validate())
                {
                    this.ThrowException("Snippet validation failed: " + codeSnippet.ValidationErrors.ToString());
                }

                if (codeSnippet.IsSpam())
                {
                    this.ThrowException("Invalid Content.");
                }

                if (!codeSnippet.Save())
                {
                    this.ThrowException("Failed to save snippet: " + codeSnippet.ErrorMessage);
                }
                return(newSnippet);
            }
        }