Пример #1
0
        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);
        }
Пример #2
0
        public virtual async Task <IEntityViewModel <TViewModel> > ListViewModel <TViewModel>([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption, [FromOptions(true)] EntityQuerySelectOption <T, TViewModel> selectOption)
        {
            var auth = authenticationProvider.GetAuthentication();

            if (authorizeOption == null)
            {
                authorizeOption = EntityDomainAuthorizeOption.View;
            }
            authorizeOption.Validate(Metadata, auth);
            var context   = database.GetContext <T>();
            var queryable = context.Query();
            var e         = new EntityQueryEventArgs <T>(queryable);

            await RaiseAsyncEvent(EntityQueryEvent, e);

            queryable = e.Queryable;
            var convertQueryable = selectOption.Select(queryable);
            EntityViewModel <TViewModel> model       = new EntityViewModel <TViewModel>(convertQueryable);
            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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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();
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
            EntityModelCreatedEventArgs <T> arg = new EntityModelCreatedEventArgs <T>(model);

            await RaiseAsyncEvent(EntityCreateModelCreatedEvent, arg);

            model = arg.Model;
            return(model);
        }
Пример #7
0
        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);
        }