Exemplo n.º 1
0
        public async Task <OpResult> UpsertForum(ForumModel forum, bool importFlag = false)
        {
            var        retvalue      = new OpResult();
            ForumModel existingForum = null;

            if (forum == null)
            {
                retvalue.LogError(FORUM_ERROR_DATA_MUST_BE_PROVIDED);
                return(retvalue);
            }
            if (!importFlag && forum.Guid != Guid.Empty)
            {
                existingForum = await _forumDal.GetForumById(forum.Guid, null);

                if (existingForum == null)
                {
                    retvalue.LogError(FORUM_ERROR_INVALID_RECORD);
                }
                else
                {
                    forum.UniqueName  = existingForum.UniqueName;
                    forum.CreatedUTC  = existingForum.CreatedUTC;
                    forum.ModifiedUTC = existingForum.ModifiedUTC;
                }
            }
            if (string.IsNullOrWhiteSpace(forum.Name))
            {
                retvalue.LogError(FORUM_ERROR_TITLE_IS_REQUIRED);
            }
            if (string.IsNullOrWhiteSpace(forum.Description))
            {
                retvalue.LogError(FORUM_ERROR_DESCRIPTION_IS_REQUIRED);
            }
            if (retvalue.Successful)
            {
                var existingForumUniqueNames = (await _forumDal.ListForums()).ToDictionary(f => f.UniqueName, f => true, StringComparer.OrdinalIgnoreCase);
                forum.GenerateUniqueName(existingForumUniqueNames);

                if (importFlag || forum.Guid == Guid.Empty)
                {
                    await _forumDal.InsertForum(forum, importFlag);
                }
                else
                {
                    await _forumDal.UpdateForum(forum);
                }
            }
            return(retvalue);
        }