コード例 #1
0
        public async Task <OpResult> UpsertForumThread(ForumThreadModel forumThread, Guid forumUserGuid, bool isForumAdmin, bool importFlag = false)
        {
            var retvalue = new OpResult();
            ForumThreadModel existingForumThread = null;

            if (forumThread == null)
            {
                retvalue.LogError(THREAD_ERROR_DATA_MUST_BE_PROVIDED);
                return(retvalue);
            }
            if (!importFlag)
            {
                if (forumThread.Guid != Guid.Empty)
                {
                    existingForumThread = await _forumDal.GetForumThreadById(forumThread.Guid, null);

                    if (existingForumThread == null)
                    {
                        retvalue.LogError(THREAD_ERROR_INVALID_RECORD);
                    }
                    else
                    {
                        forumThread.UniqueName    = existingForumThread.UniqueName;
                        forumThread.CreatedUTC    = existingForumThread.CreatedUTC;
                        forumThread.ModifiedUTC   = existingForumThread.ModifiedUTC;
                        forumThread.ForumUserGuid = existingForumThread.ForumUserGuid;
                        if (!isForumAdmin)
                        {
                            forumThread.ForumGuid   = existingForumThread.ForumGuid;
                            forumThread.LockFlag    = existingForumThread.LockFlag;
                            forumThread.VisibleFlag = existingForumThread.VisibleFlag;
                            if (forumThread.ForumUserGuid != forumUserGuid || !forumThread.VisibleFlag || forumThread.LockFlag)
                            {
                                retvalue.LogError(THREAD_ERROR_NOT_AUTHORIZED);
                            }
                            if (forumThread.CreatedUTC.HasValue &&
                                (DateTime.UtcNow - forumThread.CreatedUTC.Value).TotalMinutes > _settingsProvider.Current.TimeLimitForumPostEdit)
                            {
                                retvalue.LogError(THREAD_ERROR_TOO_LATE_TO_UPDATE);
                            }
                        }
                    }
                }
                else
                {
                    forumThread.ForumUserGuid = forumUserGuid;
                }
            }
            if (string.IsNullOrWhiteSpace(forumThread.Name))
            {
                retvalue.LogError(THREAD_ERROR_TITLE_IS_REQUIRED);
            }
            if (forumThread.Guid == Guid.Empty)
            {
                if (forumThread.InitialPost == null)
                {
                    retvalue.LogError(THREAD_ERROR_INITIAL_POST_IS_REQUIRED);
                }
            }
            if (retvalue.Successful)
            {
                var existingForumThreadUniqueNames = (await _forumDal.ListForumThreads(forumThread.ForumGuid, null))
                                                     .ToDictionary(ft => ft.UniqueName, ft => true, StringComparer.OrdinalIgnoreCase);
                forumThread.GenerateUniqueName(existingForumThreadUniqueNames);

                _forumDal.OpenTransaction(DatabaseType.Forum, false);
                try
                {
                    if (importFlag || forumThread.Guid == Guid.Empty)
                    {
                        await _forumDal.InsertForumThread(forumThread, importFlag);
                    }
                    else
                    {
                        await _forumDal.UpdateForumThread(forumThread);
                    }
                    if (forumThread.InitialPost != null)
                    {
                        forumThread.InitialPost.ForumThreadGuid = forumThread.Guid;
                        retvalue.Inhale(await UpsertForumThreadPost(forumThread.InitialPost, forumUserGuid, isForumAdmin, importFlag));
                    }
                    _forumDal.CommitTransaction(DatabaseType.Forum);
                }
                catch (Exception ex)
                {
                    _forumDal.RollBackTransaction(DatabaseType.Forum);
                    throw new Exception("Forum Thread Transaction Rolled Back Due to Previous Errors", ex);
                }
            }
            return(retvalue);
        }