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); }
public virtual async Task <IEntityViewModel <T> > List([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { authorizeOption = EntityDomainAuthorizeOption.View; } 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); } queryable = context.Order(queryable); if (EntityQuery != null) { var e = new EntityQueryEventArgs <T>(queryable); await EntityQuery(Context, e); queryable = e.Queryable; } EntityViewModel <T> model = new EntityViewModel <T>(queryable); model.Properties = authorizeOption.GetProperties(Metadata, auth); model.Items = await context.ToArrayAsync(queryable); return(model); }
public virtual async Task <IEntityUpdateModel <T> > Update([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromService] IValueProvider valueProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { object index = valueProvider.GetValue("id", Metadata.KeyProperty.ClrType); var context = database.GetContext <T>(); bool isNew = index == null || (Metadata.KeyProperty.ClrType.GetTypeInfo().IsValueType ? index.Equals(Activator.CreateInstance(Metadata.KeyProperty.ClrType)) : false); var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { if (isNew) { authorizeOption = EntityDomainAuthorizeOption.Create; } else { authorizeOption = EntityDomainAuthorizeOption.Edit; } } authorizeOption.Validate(Metadata, auth); T entity; if (isNew) { entity = context.Create(); entity.OnCreating(); } else { entity = await context.GetAsync(index); if (entity == null) { throw new DomainServiceException(new EntityNotFoundException(typeof(T), index)); } } var result = await UpdateCore(valueProvider, auth, entity, authorizeOption.GetProperties(Metadata, auth).ToArray()); if (result.IsSuccess) { if (isNew) { context.Add(entity); } else { context.Update(entity); } await database.SaveAsync(); } return(result); }
public virtual async Task <IEntityViewModel <T> > List([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption) { var auth = authenticationProvider.GetAuthentication(); if (authorizeOption == null) { authorizeOption = EntityDomainAuthorizeOption.View; } 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); } var e = new EntityQueryEventArgs <T>(queryable); await RaiseAsyncEvent(EntityQueryEvent, e); queryable = e.Queryable; if (!e.IsOrdered) { queryable = context.Order(queryable); } EntityViewModel <T> model = new EntityViewModel <T>(queryable); model.Properties = authorizeOption.GetProperties(Metadata, auth); EntityPagerOption pagerOption = Context.DomainContext.Options.GetOption <EntityPagerOption>(); if (pagerOption != null) { model.CurrentSize = pagerOption.CurrentSize; model.CurrentPage = pagerOption.CurrentPage; model.PageSizeOption = pagerOption.PageSizeOption; } await model.UpdateTotalPageAsync(); await model.UpdateItemsAsync(); return(model); }