예제 #1
0
        public async Task DeletePost([FromService] IDatabaseContext databaseContext, [FromService] IAuthenticationProvider authenticationProvider, [FromValue] string id)
        {
            var context = databaseContext.GetWrappedContext <IPost>();
            var post    = await context.GetAsync(id);

            if (post == null)
            {
                throw new DomainServiceException(new KeyNotFoundException(id));
            }
            var e = new EntityFilterEventArgs <IPost>(post);

            await RaiseAsyncEvent(PostDeleteEvent, e);

            await post.LoadAsync(t => t.Member);

            if (authenticationProvider.GetAuthentication().GetUserId() == post.Member.Index.ToString())
            {
                context.Remove(post);
            }
            else
            {
                post.IsDeleted = true;
                context.Update(post);
            }
            await databaseContext.SaveAsync();
        }
예제 #2
0
        public async Task <IPost> CreatePost([FromService] IDatabaseContext databaseContext, [FromService] IAuthenticationProvider authenticationProvider, [FromValue] string id)
        {
            var            converter     = TypeDescriptor.GetConverter(EntityDescriptor.GetMetadata <IThread>().KeyType);
            var            idObj         = converter.ConvertFrom(id);
            IValueProvider valueProvider = Context.DomainContext.GetRequiredService <IValueProvider>();
            IThread        thread;
            IPost          post;

            {
                var context = databaseContext.GetWrappedContext <IThread>();
                thread = await context.Include(t => t.Forum.Board).SingleOrDefaultAsync(t => t.Index == idObj);

                if (thread == null)
                {
                    throw new DomainServiceException(new KeyNotFoundException(id));
                }
            }
            {
                var context = databaseContext.GetWrappedContext <IPost>();
                post        = context.Create();
                post.Thread = thread;
                var e = new EntityFilterEventArgs <IPost>(post);
                await RaiseAsyncEvent(PostCreateEvent, e);
            }
            return(post);
        }
예제 #3
0
        public async Task <IPost> EditThread([FromService] IDatabaseContext databaseContext, [FromValue] string id)
        {
            var            converter     = TypeDescriptor.GetConverter(EntityDescriptor.GetMetadata <IThread>().KeyType);
            var            idObj         = converter.ConvertFrom(id);
            IValueProvider valueProvider = Context.DomainContext.GetRequiredService <IValueProvider>();
            IThread        thread;
            {
                var context = databaseContext.GetWrappedContext <IThread>();
                thread = await context.Include(t => t.Forum.Board).SingleOrDefaultAsync(t => t.Index == idObj);

                if (thread == null)
                {
                    throw new DomainServiceException(new KeyNotFoundException(id));
                }
                var e = new EntityFilterEventArgs <IThread>(thread);
                await RaiseAsyncEvent(ThreadEditEvent, e);
            }
            {
                var context = databaseContext.GetWrappedContext <IPost>();
                var posts   = await thread.LoadAsync(t => t.Replies);

                var post = await posts.OrderBy(t => t.CreateDate).FirstOrDefaultAsync();

                if (post == null)
                {
                    throw new InvalidOperationException("回贴不存在。");
                }
                post.Thread = thread;
                return(post);
            }
        }
예제 #4
0
        public async Task <IThread> CreateThread([FromService] IDatabaseContext databaseContext, [FromValue] string id)
        {
            var            converter     = TypeDescriptor.GetConverter(EntityDescriptor.GetMetadata <IForum>().KeyType);
            var            idObj         = converter.ConvertFrom(id);
            IValueProvider valueProvider = Context.DomainContext.GetRequiredService <IValueProvider>();
            IForum         forum;
            IThread        thread;

            {
                var context = databaseContext.GetWrappedContext <IForum>();
                forum = await context.Query().Include(t => t.Board).SingleOrDefaultAsync(t => t.Index == idObj.Wrap());

                if (forum == null)
                {
                    throw new DomainServiceException(new KeyNotFoundException(id));
                }
            }
            {
                var context = databaseContext.GetWrappedContext <IThread>();
                thread       = context.Create();
                thread.Forum = forum;
                var e = new EntityFilterEventArgs <IThread>(thread);
                await RaiseAsyncEvent(ThreadCreateEvent, e);
            }
            return(thread);
        }
예제 #5
0
        public async Task <IPost> UpdatePost([FromService] IDatabaseContext databaseContext, [FromService] IAuthenticationProvider authenticationProvider, [FromValue(false)] string id, [FromValue(false)] string threadId)
        {
            IValueProvider valueProvider = Context.DomainContext.GetRequiredService <IValueProvider>();
            IPost          post;

            if (string.IsNullOrEmpty(id))
            {
                IThread thread;
                {
                    var context = databaseContext.GetWrappedContext <IThread>();
                    thread = await context.GetAsync(threadId);

                    if (thread == null)
                    {
                        throw new DomainServiceException(new KeyNotFoundException(threadId));
                    }
                }
                {
                    var context = databaseContext.GetWrappedContext <IPost>();
                    post        = context.Create();
                    post.Thread = thread;
                    post.Member = await authenticationProvider.GetAuthentication().GetUserAsync <IMember>();

                    post.Content = valueProvider.GetValue <string>("content");
                    var e = new EntityFilterEventArgs <IPost>(post);
                    await RaiseAsyncEvent(PostUpdateEvent, e);

                    context.Add(post);
                }
            }
            else
            {
                var context = databaseContext.GetWrappedContext <IPost>();
                post = await context.GetAsync(id);

                if (post == null)
                {
                    throw new DomainServiceException(new KeyNotFoundException(id));
                }
                post.Content = valueProvider.GetValue <string>("content");
                var e = new EntityFilterEventArgs <IPost>(post);
                await RaiseAsyncEvent(PostUpdateEvent, e);

                context.Update(post);
            }
            await databaseContext.SaveAsync();

            return(post);
        }
예제 #6
0
        public async Task <IForum> GetForum([FromService] IDatabaseContext databaseContext, [FromValue] string id)
        {
            var context = databaseContext.GetWrappedContext <IForum>();
            var item    = await context.GetAsync(id);

            if (item == null)
            {
                throw new DomainServiceException(new KeyNotFoundException(id));
            }
            await item.LoadAsync(t => t.Board);

            var e = new EntityFilterEventArgs <IForum>(item);

            await RaiseAsyncEvent(ForumDetailEvent, e);

            return(item);
        }
예제 #7
0
        public async Task <IThread> UpdateThread([FromService] IDatabaseContext databaseContext, [FromService] IAuthenticationProvider authenticationProvider, [FromValue(false)] string id, [FromValue(false)] string forumId)
        {
            IValueProvider valueProvider = Context.DomainContext.GetRequiredService <IValueProvider>();
            IThread        thread;
            {
                var context = databaseContext.GetWrappedContext <IThread>();
                if (string.IsNullOrEmpty(id))
                {
                    if (string.IsNullOrEmpty(forumId))
                    {
                        throw new DomainServiceException(new ArgumentNullException(nameof(forumId)));
                    }
                    var forumContext = databaseContext.GetWrappedContext <IForum>();
                    var forum        = await forumContext.GetAsync(forumId);

                    if (forum == null)
                    {
                        throw new DomainServiceException(new KeyNotFoundException(forumId));
                    }
                    thread        = context.Create();
                    thread.Forum  = forum;
                    thread.Member = await authenticationProvider.GetAuthentication().GetUserAsync <IMember>();
                }
                else
                {
                    thread = await context.GetAsync(id);

                    if (thread == null)
                    {
                        throw new DomainServiceException(new KeyNotFoundException(id));
                    }
                }
                thread.Title = valueProvider.GetValue <string>("title");
                var e = new EntityFilterEventArgs <IThread>(thread);
                await RaiseAsyncEvent(ThreadUpdateEvent, e);

                if (string.IsNullOrEmpty(id))
                {
                    context.Add(thread);
                }
                else
                {
                    context.Update(thread);
                }
            }
            {
                var   context = databaseContext.GetWrappedContext <IPost>();
                IPost post;
                if (string.IsNullOrEmpty(id))
                {
                    post            = context.Create();
                    post.CreateDate = thread.CreateDate;
                    post.Thread     = thread;
                    post.Member     = thread.Member;
                }
                else
                {
                    var posts = await thread.LoadAsync(t => t.Replies);

                    post = await posts.OrderBy(t => t.CreateDate).FirstOrDefaultAsync();

                    if (post == null)
                    {
                        throw new InvalidOperationException("回贴不存在。");
                    }
                }
                post.Content = valueProvider.GetRequiredValue <string>("content");
                if (string.IsNullOrEmpty(id))
                {
                    context.Add(post);
                }
                else
                {
                    context.Update(post);
                }
            }
            await databaseContext.SaveAsync();

            return(thread);
        }