/// <summary> /// Creates a new thread in the given forum and places the passed in message as the first message in the thread. /// Caller should validate input parameters. /// </summary> /// <param name="forumID">Forum wherein the new thread will be placed</param> /// <param name="userID">User who started this thread</param> /// <param name="subject">Subject of the thread</param> /// <param name="messageText">First message of the thread</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="isSticky">Flag if the thread is sticky / pinned (true) or not (false)</param> /// <param name="userIDIPAddress">IP address of user calling this method</param> /// <param name="defaultSupportQueueID">The ID of the default support queue for this forum. If not null, the thread created will be /// added to the support queue with the ID specified.</param> /// <param name="messageID">The message ID of the new message, which is the start message in the thread.</param> /// <returns>ThreadID if succeeded, 0 if not.</returns> public static int CreateNewThreadInForum(int forumID, int userID, string subject, string messageText, string messageAsHTML, bool isSticky, string userIDIPAddress, int?defaultSupportQueueID, bool subscribeToThread, out int messageID) { Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "NewThread"); try { DateTime postDate = DateTime.Now; ThreadEntity newThread = new ThreadEntity(); newThread.ForumID = forumID; newThread.IsClosed = false; newThread.IsSticky = isSticky; newThread.StartedByUserID = userID; newThread.Subject = subject; newThread.ThreadLastPostingDate = postDate; if (defaultSupportQueueID.HasValue) { // a support queue has been specified as the default support queue for this forum. Add the new thread to this support queue. SupportQueueThreadEntity supportQueueThread = new SupportQueueThreadEntity(); supportQueueThread.QueueID = defaultSupportQueueID.Value; supportQueueThread.PlacedInQueueByUserID = userID; supportQueueThread.PlacedInQueueOn = DateTime.Now; // assign the Thread property to the newly created thread entity, so this supportqueuethreadentity will be part of the graph // of objects which will be saved. supportQueueThread.Thread = newThread; } DateTime postingDate = DateTime.Now; MessageEntity newMessage = new MessageEntity(); newMessage.MessageText = messageText; newMessage.MessageTextAsHTML = messageAsHTML; newMessage.PostedByUserID = userID; newMessage.PostingDate = postingDate; newMessage.PostedFromIP = userIDIPAddress; newMessage.Thread = newThread; // add the newMessage entity to the transaction object. All entities saved recursively will be added automatically trans.Add(newMessage); // save the complete graph newMessage.Save(true); messageID = newMessage.MessageID; int threadID = newMessage.ThreadID; // update thread statistics, this is the task for the message manager, and we pass the transaction object so the actions will run in // the same transaction. MessageManager.UpdateStatisticsAfterMessageInsert(threadID, userID, trans, postingDate, false, subscribeToThread); trans.Commit(); return(newThread.ThreadID); } catch (Exception) { trans.Rollback(); throw; } finally { trans.Dispose(); } }
/// <summary> /// Creates a new thread in the given forum and places the passed in message as the first message in the thread. /// Caller should validate input parameters. /// </summary> /// <param name="forumId">Forum wherein the new thread will be placed</param> /// <param name="userId">User who started this thread</param> /// <param name="subject">Subject of the thread</param> /// <param name="messageText">First message of the thread</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="isSticky">Flag if the thread is sticky / pinned (true) or not (false)</param> /// <param name="userIdIPAddress">IP address of user calling this method</param> /// <param name="defaultSupportQueueId">The ID of the default support queue for this forum. If not null, the thread created will be /// added to the support queue with the ID specified.</param> /// <param name="subscribeToThread">If true, the user with userid is automatically subscribed to the new thread created</param> /// <returns>tuple with ThreadID and messageid. ThreadId, if succeeded, is set to the threadid of the new thread, or 0 if failed. /// The message ID is the id of the new message, which is the start message in the thread.</returns> public static async Task <(int threadId, int messageId)> CreateNewThreadInForumAsync(int forumId, int userId, string subject, string messageText, string messageAsHTML, bool isSticky, string userIdIPAddress, int?defaultSupportQueueId, bool subscribeToThread) { var newThread = new ThreadEntity { ForumID = forumId, IsClosed = false, IsSticky = isSticky, StartedByUserID = userId, Subject = subject, }; if (defaultSupportQueueId.HasValue) { // a support queue has been specified as the default support queue for this forum. Add the new thread to this support queue. newThread.SupportQueueThread = new SupportQueueThreadEntity { QueueID = defaultSupportQueueId.Value, PlacedInQueueByUserID = userId, PlacedInQueueOn = DateTime.Now, // No need to set the Thread property, as it's auto-assigned due to the assignment of newThread.SupportQueueThread. }; } var newMessage = new MessageEntity { MessageText = messageText, MessageTextAsHTML = messageAsHTML, PostedByUserID = userId, PostingDate = DateTime.Now, PostedFromIP = userIdIPAddress, Thread = newThread }; // Assign a new statistics entity so we can track what the last message is. newThread.Statistics = new ThreadStatisticsEntity() { LastMessage = newMessage, NumberOfMessages = 1, NumberOfViews = 1 }; using (var adapter = new DataAccessAdapter()) { await adapter.StartTransactionAsync(IsolationLevel.ReadCommitted, "NewThread").ConfigureAwait(false); try { // save the complete graph await adapter.SaveEntityAsync(newMessage, true).ConfigureAwait(false); var messageId = newMessage.MessageID; var threadId = newMessage.ThreadID; // update thread statistics, this is the task for the message manager, and we pass the adapter so the actions will run in // the same transaction. Pass -1 as newMessageId as we already have inserted this. await MessageManager.UpdateStatisticsAfterMessageInsert(threadId, userId, -1, adapter, DateTime.Now, false, subscribeToThread); adapter.Commit(); return(newThread.ThreadID, messageId); } catch { adapter.Rollback(); throw; } } }