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 <IEntityEditModel <T> > Create([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { authorizeOption = EntityDomainAuthorizeOption.Create; } authorizeOption.Validate(Metadata, auth); var context = database.GetContext <T>(); var item = context.Create(); item.OnCreating(); EntityEditModel <T> model = new EntityEditModel <T>(item); model.Properties = authorizeOption.GetProperties(Metadata, auth); if (EntityCreateModelCreated != null) { EntityModelCreatedEventArgs <T> arg = new EntityModelCreatedEventArgs <T>(model); await EntityCreateModelCreated(Context, arg); model = arg.Model; } return(model); }