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); }
private bool SetMostRecentThread(IDataDictionary <Thread> data) { var count = 0; var userId = data.GetValue(p => p.CreatorId, this.User.UserId); var user = userId == this.User.UserId ? this.User : this.DataAccess.Select <UserProfile>(Condition.Equal(nameof(UserProfile.UserId), userId)).FirstOrDefault(); //更新当前主题所属论坛的最后发帖信息 count += this.DataAccess.Update <Forum>(new { SiteId = data.GetValue(p => p.SiteId), ForumId = data.GetValue(p => p.ForumId), MostRecentThreadId = data.GetValue(p => p.ThreadId), MostRecentThreadTitle = data.GetValue(p => p.Title), MostRecentThreadTime = data.GetValue(p => p.CreatedTime), MostRecentThreadAuthorId = userId, MostRecentThreadAuthorName = user?.Nickname, MostRecentThreadAuthorAvatar = user?.Avatar, }); //递增当前发帖人的累计主题数及最后发表的主题信息 count += this.DataAccess.Update <UserProfile>(new { UserId = userId, TotalThreads = Interval.One, MostRecentThreadId = data.GetValue(p => p.ThreadId), MostRecentThreadTitle = data.GetValue(p => p.Title), MostRecentThreadTime = data.GetValue(p => p.CreatedTime), }); return(count > 0); }
protected override int OnInsert(IDataDictionary <Feedback> data, ISchema schema, IDictionary <string, object> states) { //更新内容及内容类型 var contentFile = Utility.SetContent(data, () => this.GetContentFilePath(data.GetValue(p => p.FeedbackId))); try { //调用基类同名方法 var count = base.OnInsert(data, schema, states); if (count < 1) { //如果新增记录失败则删除刚创建的内容文件 if (contentFile != null && contentFile.Length > 0) { Utility.DeleteFile(contentFile); } } return(count); } catch { //删除新建的内容文件 if (contentFile != null && contentFile.Length > 0) { Utility.DeleteFile(contentFile); } throw; } }
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 OnInsert(IDataDictionary <File> data, ISchema schema, IDictionary <string, object> states) { var filePath = data.GetValue(p => p.Path); try { //调用基类同名方法 var count = base.OnInsert(data, schema, states); if (count < 1) { //如果新增记录失败则删除刚创建的文件 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 OnUpdate(IDataDictionary <Feedback> data, ICondition condition, ISchema schema, IDictionary <string, object> states) { //更新内容及内容类型 Utility.SetContent(data, () => this.GetContentFilePath(data.GetValue(p => p.FeedbackId))); //调用基类同名方法 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 void OnValidate(Method method, IDataDictionary <Feedback> data) { if (method.IsWriting) { //更新内容及内容类型 var contentFile = Utility.SetContent(data, () => this.GetContentFilePath(data.GetValue(p => p.FeedbackId))); } base.OnValidate(method, data); }
private bool SetMostRecentPost(IDataDictionary <Post> data) { //注意:如果当前帖子是主题内容贴则不需要更新对应的统计信息 if (data == null) { return(false); } //如果当前帖子没有指定对应的主题编号,则返回失败 var threadId = data.GetValue(p => p.ThreadId, (ulong)0); if (threadId == 0) { return(false); } //如果当前帖子对应的主题是不存在的,则返回失败 var thread = this.DataAccess.Select <Thread>(Condition.Equal("ThreadId", threadId)).FirstOrDefault(); if (thread == null) { return(false); } //递增新增贴所属的主题的累计回帖总数 if (this.DataAccess.Increment <Thread>("TotalReplies", Condition.Equal("ThreadId", threadId)) < 0) { return(false); } var userId = data.GetValue(p => p.CreatorId); var user = this.DataAccess.Select <UserProfile>(Condition.Equal("UserId", userId)).FirstOrDefault(); var count = 0; //更新当前帖子所属主题的最后回帖信息 count += this.DataAccess.Update(this.DataAccess.Naming.Get <Thread>(), new { ThreadId = threadId, MostRecentPostId = data.GetValue(p => p.PostId), MostRecentPostTime = data.GetValue(p => p.CreatedTime), MostRecentPostAuthorId = userId, MostRecentPostAuthorName = user?.Nickname, MostRecentPostAuthorAvatar = user?.Avatar, }); //更新当前帖子所属论坛的最后回帖信息 count += this.DataAccess.Update(this.DataAccess.Naming.Get <Forum>(), new { SiteId = thread.SiteId, ForumId = thread.ForumId, MostRecentPostId = data.GetValue(p => p.PostId), MostRecentPostTime = data.GetValue(p => p.CreatedTime), MostRecentPostAuthorId = userId, MostRecentPostAuthorName = user?.Nickname, MostRecentPostAuthorAvatar = user?.Avatar, }); //递增当前发帖人的累计回帖数,并且更新发帖人的最后回帖信息 if (this.DataAccess.Increment <UserProfile>("TotalPosts", Condition.Equal("UserId", data.GetValue(p => p.CreatorId))) > 0) { count += this.DataAccess.Update(this.DataAccess.Naming.Get <UserProfile>(), new { UserId = data.GetValue(p => p.CreatorId), MostRecentPostId = data.GetValue(p => p.PostId), MostRecentPostTime = data.GetValue(p => p.CreatedTime), }); } return(count > 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; } }
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); } }