public static void DeleteContentFile(IDataDictionary data) { if (data.TryGetValue <string>("ContentType", out var contentType) && Utility.IsContentFile(contentType) && data.TryGetValue <string>("Content", out var content)) { Utility.DeleteFile(content); } }
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); }
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 <Thread> data, ISchema schema, IDictionary <string, object> states) { if (!data.TryGetValue(p => p.Post, out var post) || string.IsNullOrEmpty(post.Content)) { throw new InvalidOperationException("Missing content of the thread."); } //确保数据模式含有“主题内容贴”复合属性 schema.Include("Post{*}"); //更新主题内容贴的相关属性 post.Visible = false; using (var transaction = new Zongsoft.Transactions.Transaction()) { //调用基类同名方法,插入主题数据 var count = base.OnInsert(data, schema, states); if (count < 1) { return(count); } //更新发帖人关联的主题统计信息 this.SetMostRecentThread(data); //提交事务 transaction.Commit(); return(count); } }
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)); }
protected override int OnUpdate(IDataDictionary <Folder> data, ICondition condition, ISchema schema, IDictionary <string, object> states) { using (var transaction = new Transactions.Transaction()) { //调用基类同名方法 var count = base.OnUpdate(data, condition, schema, states); if (count < 1) { return(count); } //获取新增的文件夹用户集,并尝试插入该用户集 data.TryGetValue(p => p.Users, (key, users) => { if (users == null) { return; } var folderId = data.GetValue(p => p.FolderId); //首先清除该文件夹的所有用户集 this.DataAccess.Delete <Folder.FolderUser>(Condition.Equal("FolderId", folderId)); //新增该文件夹的用户集 this.DataAccess.InsertMany(users.Where(p => p.FolderId == folderId)); }); //提交事务 transaction.Commit(); //返回主表插入的记录数 return(count); } }
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; } }
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); } }