public virtual async Task <IEntityEditModel <T> > Detail([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromService] IValueProvider valueProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { object index = valueProvider.GetRequiredValue("id", Metadata.KeyProperty.ClrType); var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { authorizeOption = EntityDomainAuthorizeOption.Detail; } authorizeOption.Validate(Metadata, auth); var context = database.GetContext <T>(); var queryable = context.Query(); foreach (var propertyMetadata in Metadata.Properties.Where(t => t.CustomType == "Entity")) { queryable = context.Include(queryable, propertyMetadata.ClrName); } T entity = await context.GetAsync(queryable, index); if (entity == null) { throw new DomainServiceException(new EntityNotFoundException(typeof(T), index)); } var model = new EntityEditModel <T>(entity); model.Properties = authorizeOption.GetProperties(Metadata, auth); var e = new EntityModelCreatedEventArgs <T>(model); await RaiseAsyncEvent(EntityDetailModelCreatedEvent, e); return(model); }
public virtual async Task Remove([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromService] IValueProvider valueProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { object index = valueProvider.GetRequiredValue("id", Metadata.KeyProperty.ClrType); var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { authorizeOption = EntityDomainAuthorizeOption.Remove; } authorizeOption.Validate(Metadata, auth); var context = database.GetContext <T>(); T entity = await context.GetAsync(index); if (entity == null) { throw new DomainServiceException(new EntityNotFoundException(typeof(T), index)); } var e = new EntityRemoveEventArgs <T>(entity); await RaiseAsyncEvent(EntityRemoveEvent, e); if (e.IsCanceled) { return; } context.Remove(entity); await database.SaveAsync(); }
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); }