📄 aspnetforums.sql
字号:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure Statistics_UpdateForumStatistics
(
@ForumID int,
@ThreadID int,
@PostID int
)
AS
BEGIN
DECLARE @UserName nvarchar(50)
DECLARE @PostDate datetime
DECLARE @TotalPosts int
DECLARE @TotalThreads int
-- Get values necessary to update the forum statistics
SELECT
@UserName = username,
@PostDate = PostDate,
@TotalPosts = (SELECT COUNT(*) FROM Posts P2 (nolock) WHERE P2.ForumID = P.ForumID AND P2.Approved=1),
@TotalThreads = (SELECT COUNT(*) FROM Posts P2 (nolock) WHERE P2.ForumID = P.ForumID AND P2.Approved=1 AND P2.PostLevel=1)
FROM
Posts P
WHERE
PostID = @PostID
-- Do the update within a transaction
BEGIN TRAN
UPDATE
Forums
SET
TotalPosts = @TotalPosts,
TotalThreads = @TotalThreads,
MostRecentPostID = @PostID,
MostRecentThreadID = @ThreadID,
MostRecentPostDate = @PostDate,
MostRecentPostAuthor = @UserName
WHERE
ForumID = @ForumID
COMMIT TRAN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE sp_AddForum
(
@Name nvarchar(100),
@Description nvarchar(3000),
@ForumGroupId int,
@Moderated bit,
@DaysToView int,
@Active bit
)
AS
-- insert a new forum
INSERT INTO Forums (ForumGroupId, Name, Description, Moderated, DaysToView, Active)
VALUES (@ForumGroupId, @Name, @Description, @Moderated, @DaysToView, @Active)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE sp_AddForumGroup
(
@ForumGroupName nvarchar(256)
)
AS
-- insert a new forum
INSERT INTO
ForumGroups
(Name)
VALUES
(@ForumGroupName)
-- Reset the sort order
exec Maintenance_ResetForumGroupsForInsert
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
create procedure sp_AddModeratedForumForUser
(
@UserName nvarchar(50),
@ForumID int,
@EmailNotification bit
)
AS
-- add a row to the Moderators table
-- if the user wants to add All Forums, go ahead and delete all of the other forums
IF @ForumID = 0
DELETE FROM Moderators WHERE Username = @UserName
-- now insert the new row into the table
INSERT INTO Moderators (Username, ForumID, EmailNotification)
VALUES (@UserName, @ForumID, @EmailNotification)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE sp_AddPost
(
@ForumID int,
@ReplyToPostID int,
@Subject nvarchar(256),
@UserName nvarchar(50),
@Body text,
@IsLocked bit,
@Pinned datetime
) AS
DECLARE @MaxSortOrder int
DECLARE @ParentLevel int
DECLARE @ThreadID int
DECLARE @ParentSortOrder int
DECLARE @NextSortOrder int
DECLARE @NewPostID int
DECLARE @ApprovedPost bit
DECLARE @ModeratedForum bit
DECLARE @IsPinned bit
-- Is the post pinned?
IF @Pinned IS NULL
BEGIN
SET @IsPinned = 0
SET @Pinned = GetDate()
END
ELSE
SET @IsPinned = 1
-- Is this forum moderated?
IF @ForumID = 0 AND @ReplyToPostID <> 0
-- we need to get the forum ID
SELECT @ForumID = ForumID FROM Posts (nolock) WHERE PostID = @ReplyToPostID
SELECT @ModeratedForum = Moderated FROM Forums (nolock) WHERE ForumID = @ForumID
-- Determine if this post will be approved
-- if the forum is NOT moderated, then the post will be approved
SET NOCOUNT ON
BEGIN TRAN
IF @ModeratedForum = 0
SELECT @ApprovedPost = 1
ELSE
BEGIN
-- ok, this is a moderated forum. Is this user trusted? If he is, then the post is approved ; else it is not
IF (SELECT Trusted FROM Users (nolock) WHERE UserName = @UserName) = 1
SELECT @ApprovedPost = 1
ELSE
SELECT @ApprovedPost = 0
END
IF @ReplyToPostID = 0 -- New Post
BEGIN
-- Do INSERT into Posts table
INSERT
Posts ( ForumID, ThreadID, ParentID, PostLevel, SortOrder, Subject, PinnedDate, IsPinned, UserName, Approved, Body, IsLocked )
VALUES
(@ForumID, 0, 0, 1, 1, @Subject, @Pinned, @IsPinned, @UserName, @ApprovedPost, @Body, @IsLocked)
-- Get the new post id
SELECT
@NewPostID = @@IDENTITY
-- update posts with the new post id
UPDATE
Posts
SET
ThreadID = @NewPostID,
ParentID = @NewPostID
WHERE
PostID = @NewPostID
-- do we need to track the threads for this user?
SELECT @ThreadID = @NewPostID
END
ELSE -- @ReplyToID <> 0 means reply to an existing post
BEGIN
-- Get Post Information for what we are replying to
SELECT
@ParentLevel = PostLevel,
@ThreadID = ThreadID,
@ParentSortOrder = SortOrder,
@ForumID = ForumID
FROM
Posts
WHERE
PostID = @ReplyToPostID
-- Is there another post at the same level or higher
IF EXISTS (SELECT *
FROM Posts
WHERE PostLevel <= @ParentLevel
AND SortOrder > @ParentSortOrder
AND ThreadID = @ThreadID )
BEGIN
-- Find the next post at the same level or higher
SELECT
@NextSortOrder = Min(SortOrder)
FROM
Posts
WHERE
PostLevel <= @ParentLevel
AND SortOrder > @ParentSortOrder
AND ThreadID = @ThreadID
-- Move the existing posts down
UPDATE
Posts
SET
SortOrder = SortOrder + 1
WHERE
ThreadID = @ThreadID
AND SortOrder >= @NextSortOrder
-- And put this one into place
INSERT
Posts (ForumID, ThreadID, ParentID, PostLevel, SortOrder, Subject, PostDate, IsPinned, UserName, Approved, Body, IsLocked )
VALUES
(@ForumID, @ThreadID, @ReplyToPostID, @ParentLevel + 1, @NextSortOrder, @Subject, @Pinned, @IsPinned, @UserName, @ApprovedPost, @Body, @IsLocked )
-- Clean up PostsRead
DELETE PostsRead WHERE PostID = @ThreadID AND UserName != @UserName
END
ELSE -- There are no posts at this level or above
BEGIN
-- Find the highest sort order for this parent
SELECT
@MaxSortOrder = MAX(SortOrder)
FROM
Posts
WHERE
ThreadID = @ThreadID
-- Insert the new post
INSERT
Posts (ForumID, ThreadID, ParentID, PostLevel, SortOrder, Subject, PinnedDate, IsPinned, UserName, Approved, Body, IsLocked )
VALUES
(@ForumID, @ThreadID, @ReplyToPostID, @ParentLevel + 1, @MaxSortOrder + 1, @Subject, @Pinned, @IsPinned, @UserName, @ApprovedPost, @Body, @IsLocked )
-- Clean up PostsRead
DELETE PostsRead WHERE PostID = @ThreadID AND UserName != @UserName
END
SELECT
@NewPostID = @@IDENTITY FROM Posts
-- if this message is approved, update the thread date
IF @ApprovedPost = 1
UPDATE
Posts
SET
ThreadDate = getdate()
WHERE
ThreadID = @ThreadID
END
-- update the users tracking for the new post (if needed)
DECLARE @TrackThread bit
SELECT
@TrackThread = TrackYourPosts
FROM
Users (nolock)
WHERE
UserName = @UserName
IF @TrackThread = 1
-- if a row already exists to track this thread for this user, do nothing - otherwise add the row
IF NOT EXISTS(SELECT ThreadID FROM ThreadTrackings (nolock) WHERE ThreadID = @ThreadID AND UserName = @UserName)
INSERT INTO ThreadTrackings (ThreadID, UserName)
VALUES(@ThreadID, @UserName)
COMMIT TRAN
BEGIN TRAN
-- Update the user's post count
DECLARE @PostCount int
-- Get the current number of posts
SELECT @PostCount = TotalPosts FROM Users WHERE UserName = @UserName
-- Update value
SET @PostCount = @PostCount + 1
UPDATE Users SET TotalPosts = @PostCount WHERE UserName = @UserName
-- Update the forum statitics
IF @ApprovedPost = 1
exec Statistics_UpdateForumStatistics @ForumID, @ThreadID, @NewPostID
-- Clean up unnecessary columns in forumsread
exec Maintenance_CleanForumsRead @ForumID
COMMIT TRAN
SET NOCOUNT OFF
SELECT PostID = @NewPostID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create procedure sp_AddUserToRole
(
@UserName nvarchar(50),
@Rolename nvarchar(256)
)
AS
IF NOT EXISTS (SELECT Username FROM UsersInRoles WHERE Username = @UserName AND Rolename = @Rolename)
INSERT INTO
UsersInRoles
VALUES
(@UserName, @Rolename)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE sp_ApproveModeratedPost
(
@PostID int,
@ApprovedBy nvarchar(50),
@Trusted nvarchar(50)
)
AS
DECLARE @ForumID int
DECLARE @ThreadID int
-- First make sure that the post is ALREADY non-approved
IF (SELECT Approved FROM Posts (nolock) WHERE PostID = @PostID) = 1
-- its already been approved, return 0
SELECT 0
ELSE
BEGIN
-- Approve the post
UPDATE
Posts
SET
Approved = 1
WHERE
PostID = @PostID
-- Get details about the thread and forum this post belongs in
SELECT
@ForumID = ForumID,
@ThreadID = ThreadID
FROM
Posts
WHERE
PostID = @PostID
-- Update the thread date
UPDATE
Posts
SET
ThreadDate = getdate()
WHERE
ThreadID = @ThreadID
-- Update the ModerationAudit table
INSERT INTO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -