Esempio n. 1
0
        public async Task <IHttpActionResult> AddEntity([FromBody] EntityTreeViewModel entityTreeViewModel)
        {
            return(await HttpActionResultWithErrorHandling(SecurityActions.ManageEntities, async() =>
            {
                var entityType = _adminRepo.GetEntityTypes().FirstOrDefault(t => t.Id == entityTreeViewModel.EntityTypeId);

                if (entityType != null)
                {
                    entityTreeViewModel.EntityTypeName = entityType.Name;
                }

                var entity = new Entity
                {
                    TenantId = Tenant.Id,
                    EntityName = entityTreeViewModel.EntityName,
                    EntityType = entityType
                                 //TODO: EntityProperties
                };
                _adminRepo.AddEntity(entity);

                var entityTree = new EntityTree
                {
                    TenantId = entity.TenantId,
                    Entity = entity,
                    EntityId = entity.Id,
                    ParentId = entityTreeViewModel.ParentId,
                    IsParent = false, //entityTreeViewModel.ParentId == 0,
                    ReportSequence = entityTreeViewModel.Reports.Count == 0 ? null : string.Join(",", entityTreeViewModel.Reports.Select(x => x.Id))
                };
                _adminRepo.AddEntityTree(entityTree);

                entityTreeViewModel.EntityId = entity.Id;
                addOrDeleteEntityTypes(entityTreeViewModel);

                await _adminRepo.SaveAllAsync();

                entityTreeViewModel.EntityId = entity.Id;
                entityTreeViewModel.EntityTypeId = entity.EntityTypeId;
                entityTreeViewModel.Id = entityTree.Id;

                await _adminRepo.UpdateReports(entityTree, entityTreeViewModel.Reports);

                return Json(entityTreeViewModel);
            }));
        }