SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_Folder_GetSummary]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_Folder_GetSummary]
GO

CREATE PROC [dbo].cs_Folder_GetSummary
	@UserId INT,
	@SettingsID INT,
	@FolderId INT = NULL

AS
BEGIN

IF (@FolderId IS NULL)
BEGIN
	SET @FolderId = -1
END

-- Give me the top 10 most active blog posts
SELECT 	TOP 10
	fp.FeedPostId,
	fp.FeedId,
	fp.Author,
	fp.Title,
	fp.Description,
	fp.Source,
	fp.GuidName,
	fp.GuidIsPermaLink,
	fp.Link,
	fp.PubDate,
	fp.CommentsUrl,
	fp.EnclosureUrl,
	fp.EnclosureLength,
	fp.EnclosureType,
	fp.Creator,
	fp.CommentApiUrl,
	fp.CommentRssUrl,
	fp.CommentCount
FROM 	cs_Feed f,
	cs_FeedPost fp,
	cs_FolderFeed ff
WHERE f.FeedId = fp.FeedId
  AND fp.CommentCount > 0
  AND fp.PubDate >= DATEADD(day, -30, GetDate())
  AND f.FeedId = ff.FeedId
  AND ff.UserId = @UserId
  AND IsNull(ff.FolderId,-1) = @FolderId
  AND f.SettingsID = @SettingsID AND ff.SettingsID = @SettingsID
  ORDER BY fp.CommentCount DESC



SELECT top 100 	fp.FeedPostId,
	fp.FeedId,
	fp.Author,
	fp.Title,
	fp.Description,
	fp.Source,
	fp.GuidName,
	fp.GuidIsPermaLink,
	fp.Link,
	fp.PubDate,
	fp.CommentsUrl,
	fp.EnclosureUrl,
	fp.EnclosureLength,
	fp.EnclosureType,
	fp.Creator,
	fp.CommentApiUrl,
	fp.CommentRssUrl,
	fp.CommentCount
FROM 	cs_Feed f,
	cs_FeedPost fp,
	cs_FolderFeed ff
WHERE f.FeedId = fp.FeedId
  AND f.FeedId = ff.FeedId
  AND ff.UserId = @UserId
   AND IsNull(ff.FolderId,-1) = @FolderId
  AND f.SettingsID = @SettingsID AND ff.SettingsID = @SettingsID
  ORDER BY fp.PubDate DESC




  END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT EXECUTE ON [dbo].cs_Folder_GetSummary  TO PUBLIC
GO
