Beispiel #1
0
        public static string SetContent(IDataDictionary data, Func <string> getFilePath)
        {
            var filePath = string.Empty;

            data.TryGetValue <string>("Content", content =>
            {
                var rawType = data.GetValue <string>("ContentType", null);

                if (string.IsNullOrEmpty(content) || content.Length < 500)
                {
                    //调整内容类型为嵌入格式
                    data.TrySetValue("ContentType", Utility.GetContentType(rawType, true));

                    return;
                }

                //设置内容文件的存储路径
                filePath = getFilePath();

                //将内容文本写入到文件中
                Utility.WriteTextFile(filePath, content);

                //更新内容文件的存储路径
                data.SetValue("Content", filePath);

                //更新内容类型为非嵌入格式(即外部文件)
                data.SetValue("ContentType", Utility.GetContentType(rawType, false));
            });

            return(filePath);
        }
Beispiel #2
0
        protected override int OnUpdate(IDataDictionary <Post> data, ICondition condition, ISchema schema, IDictionary <string, object> states)
        {
            //更新内容到文本文件中
            data.TryGetValue(p => p.Content, (key, value) =>
            {
                if (string.IsNullOrWhiteSpace(value) || value.Length < 500)
                {
                    return;
                }

                //根据当前反馈编号,获得其对应的内容文件存储路径
                var filePath = this.GetContentFilePath(data.GetValue(p => p.PostId), data.GetValue(p => p.ContentType));

                //将反馈内容写入到对应的存储文件中
                Utility.WriteTextFile(filePath, value);

                //更新当前反馈的内容文件存储路径属性
                data.SetValue(p => p.Content, filePath);

                //更新内容类型为非嵌入格式(即外部文件)
                data.SetValue(p => p.ContentType, Utility.GetContentType(data.GetValue(p => p.ContentType), false));
            });

            //调用基类同名方法
            var count = base.OnUpdate(data, condition, schema, states);

            if (count < 1)
            {
                return(count);
            }

            return(count);
        }
        protected override int OnInsert(IDataDictionary <UserProfile> data, ISchema schema, IDictionary <string, object> states)
        {
            //调用基类同名方法(新增用户配置信息)
            if (base.OnInsert(data, schema, states) > 0)
            {
                var user = Model.Build <IUser>(u =>
                {
                    u.UserId = data.GetValue(p => p.UserId);
                    u.Name   = data.GetValue(p => p.Name);
                });

                //默认设置用户状态为可用
                user.Status = UserStatus.Active;

                //如果未显式指定用户的命名空间,则使用当前用户的命名空间
                if (string.IsNullOrWhiteSpace(user.Namespace))
                {
                    user.Namespace = this.Credential.User.Namespace;
                }

                //创建基础用户账户
                if (!this.UserProvider.Create(user, user.Name.Trim().ToLowerInvariant()))
                {
                    throw new InvalidOperationException($"The '{user.Name}' user create failed.");
                }

                //更新用户编号
                data.SetValue(p => p.UserId, user.UserId);
            }

            return(1);
        }
        protected override int OnUpdate(IDataDictionary <UserProfile> data, ICondition condition, ISchema schema, IDictionary <string, object> states)
        {
            //如果没有指定用户编号或指定的用户编号为零,则显式指定为当前用户编号
            if (!data.TryGetValue(p => p.UserId, out var userId) || userId == 0)
            {
                data.SetValue(p => p.UserId, userId = this.Credential.User.UserId);
            }

            //调用基类同名方法
            return(base.OnUpdate(data, condition, schema, states));
        }
Beispiel #5
0
        protected override int OnInsert(IDataDictionary <Post> data, ISchema schema, IDictionary <string, object> states)
        {
            string filePath = null;

            //获取原始的内容类型
            var rawType = data.GetValue(p => p.ContentType, null);

            //调整内容类型为嵌入格式
            data.SetValue(p => p.ContentType, Utility.GetContentType(rawType, true));

            //尝试更新帖子内容
            data.TryGetValue(p => p.Content, (key, value) =>
            {
                if (string.IsNullOrWhiteSpace(value) || value.Length < 500)
                {
                    return;
                }

                //设置内容文件的存储路径
                filePath = this.GetContentFilePath(data.GetValue(p => p.PostId), data.GetValue(p => p.ContentType));

                //将内容文本写入到文件中
                Utility.WriteTextFile(filePath, value);

                //更新内容文件的存储路径
                data.SetValue(p => p.Content, filePath);

                //更新内容类型为非嵌入格式(即外部文件)
                data.SetValue(p => p.ContentType, Utility.GetContentType(data.GetValue(p => p.ContentType), false));
            });

            object threadObject = null;

            //附加数据是否包含了关联的主题对象
            if (states != null && states.TryGetValue("Thread", out threadObject) && threadObject != null)
            {
                uint   siteId  = 0;
                ushort forumId = 0;

                if (threadObject is Thread thread)
                {
                    siteId  = thread.SiteId;
                    forumId = thread.ForumId;
                }
                else if (threadObject is IDataDictionary <Thread> dictionary)
                {
                    siteId  = dictionary.GetValue(p => p.SiteId);
                    forumId = dictionary.GetValue(p => p.ForumId);
                }

                //判断当前用户是否是新增主题所在论坛的版主
                var isModerator = this.ServiceProvider.ResolveRequired <ForumService>()
                                  .IsModerator(forumId);

                if (isModerator)
                {
                    data.SetValue(p => p.Approved, true);
                }
                else
                {
                    var forum = this.DataAccess.Select <Forum>(
                        Condition.Equal(nameof(Forum.SiteId), siteId) &
                        Condition.Equal(nameof(Forum.ForumId), forumId)).FirstOrDefault();

                    if (forum == null)
                    {
                        throw new InvalidOperationException("The specified forum is not existed about the new thread.");
                    }

                    data.SetValue(p => p.Approved, forum.Approvable ? false : true);
                }
            }

            try
            {
                using (var transaction = new Zongsoft.Transactions.Transaction())
                {
                    //调用基类同名方法
                    var count = base.OnInsert(data, schema.Include(nameof(Post.Attachments)), states);

                    if (count > 0)
                    {
                        //更新发帖人的关联帖子统计信息
                        //注意:只有当前帖子不是主题贴才需要更新对应的统计信息
                        if (threadObject == null)
                        {
                            this.SetMostRecentPost(data);
                        }

                        //提交事务
                        transaction.Commit();
                    }
                    else
                    {
                        //如果新增记录失败则删除刚创建的文件
                        if (filePath != null && filePath.Length > 0)
                        {
                            Utility.DeleteFile(filePath);
                        }
                    }

                    return(count);
                }
            }
            catch
            {
                //删除新建的文件
                if (filePath != null && filePath.Length > 0)
                {
                    Utility.DeleteFile(filePath);
                }

                throw;
            }
        }
Beispiel #6
0
        protected override int OnInsert(IDataDictionary <Message> data, ISchema schema, IDictionary <string, object> states)
        {
            string filePath = null;

            //获取原始的内容类型
            var rawType = data.GetValue(p => p.ContentType, null);

            //调整内容类型为嵌入格式
            data.SetValue(p => p.ContentType, Utility.GetContentType(rawType, true));

            data.TryGetValue(p => p.Content, (key, value) =>
            {
                if (string.IsNullOrWhiteSpace(value) || value.Length < 500)
                {
                    return;
                }

                //设置内容文件的存储路径
                filePath = this.GetContentFilePath(data.GetValue(p => p.MessageId), data.GetValue(p => p.ContentType));

                //将内容文本写入到文件中
                Utility.WriteTextFile(filePath, value);

                //更新内容文件的存储路径
                data.SetValue(p => p.Content, filePath);

                //更新内容类型为非嵌入格式(即外部文件)
                data.SetValue(p => p.ContentType, Utility.GetContentType(data.GetValue(p => p.ContentType), false));
            });

            using (var transaction = new Zongsoft.Transactions.Transaction())
            {
                var count = base.OnInsert(data, schema, states);

                if (count < 1)
                {
                    //如果新增记录失败则删除刚创建的文件
                    if (filePath != null && filePath.Length > 0)
                    {
                        Utility.DeleteFile(filePath);
                    }

                    return(count);
                }

                data.TryGetValue(p => p.Users, (key, users) =>
                {
                    if (users == null)
                    {
                        return;
                    }

                    IEnumerable <Message.MessageUser> GetMembers(ulong messageId, IEnumerable <Message.MessageUser> members)
                    {
                        foreach (var member in members)
                        {
                            yield return(new Message.MessageUser(messageId, member.UserId));
                        }
                    }

                    this.DataAccess.InsertMany(GetMembers(data.GetValue(p => p.MessageId), users));
                });

                //提交事务
                transaction.Commit();

                return(count);
            }
        }