示例#1
0
        public async Task <ActionResult <MetadataValidationResponse> > GenerateCode()
        {
            if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementationContainer, null, ActionTypeEnum.ManageMetadata)))
            {
                return(Unauthorized());
            }
            var bundle = await MetadataBundle.FromDbWithoutNavigations(_dbContext
                                                                       , _implementationContainer.InstanceInfo.AppTypeId, _implementationContainer.InstanceInfo.AppInstanceId);

            bundle.FixupRelationships();
            var metadataCache = new MetadataCache(bundle);
            var errorList     = await GenerateCodeValidation();

            var result = new MetadataValidationResponse();

            if (errorList.Value?.Success != true)
            {
                return(errorList);
            }

            var(succeeded, diagnostics) = await _assemblyGenerator.GenerateAssembly(metadataCache, _implementationContainer.InstanceInfo
                                                                                    , _globalConfiguration.ImplementationsDirectory, _implementationContainer.InstanceInfo.InstanceName);

            if (!succeeded)
            {
                result.Errors.AddRange(diagnostics.Select(x => x.Message));
                result.Success = false;
                return(result);
            }
            result.Success = true;
            return(result);
        }
        public async Task <ActionResult <MetadataValidationResponse> > GenerateCode()
        {
            if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementationContainer, null, ActionTypeEnum.ManageMetadata)))
            {
                return(Unauthorized());
            }
            var bundle = await MetadataBundle.FromDbWithoutNavigations(_dbContext
                                                                       , _implementationContainer.InstanceInfo.AppTypeId, _implementationContainer.InstanceInfo.AppInstanceId);

            bundle.FixupRelationships();
            var metadataCache = new MetadataCache(bundle);
            var errorList     = await GenerateCodeValidation();

            var result = new MetadataValidationResponse();

            if (errorList.Value?.Success != true)
            {
                return(errorList);
            }

            var r = await _assemblyGenerator.GenerateCode(metadataCache, _implementationContainer.InstanceInfo
                                                          , _globalConfiguration.ImplementationsDirectory, _implementationContainer.InstanceInfo.InstanceName);

            if (!r.Succeeded)
            {
                result.Errors.AddRange(r.Diagnostics.Select(x => x.Message));
                result.Success = false;
                return(result);
            }
            var copyFileName = _implementationContainer.InstanceInfo.InstanceSettings?.CopyAutoGeneratedEntitiesTo;

            if (!string.IsNullOrWhiteSpace(copyFileName))
            {
                try
                {
                    System.IO.File.Copy(r.SourceCodeFileName, copyFileName, true);
                }
                catch (Exception ex)
                {
                    result.Errors.Add("Could not copy source code to: " + copyFileName + "Reson: " + ex.GetFullMessage());
                }
            }
            result.Success = true;
            return(result);
        }
        public async Task <ActionResult <MetadataValidationResponse> > GenerateCodeValidation()
        {
            if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementationContainer, null, ActionTypeEnum.ManageMetadata)))
            {
                return(Unauthorized());
            }
            //var errors = new List<string>();
            var errors         = new MetadataValidationResponse();
            var appTypeId      = _implementationContainer.InstanceInfo.AppTypeId;
            var duplicateTypes = await _dbContext.EntityTypes.Where(x => x.AppTypeId == appTypeId)
                                 .GroupBy(x => x.Name).Where(g => g.Count() > 1)
                                 .Select(x => x.Key)
                                 .ToListAsync();

            if (duplicateTypes.Count() > 0)
            {
                errors.Errors.AddRange(duplicateTypes.Select(g => $"Entity {g} has repeated more than once"));
            }

            var entityTypes = await _dbContext.EntityTypes.Where(x => x.AppTypeId == _implementationContainer.InstanceInfo.AppTypeId).ToListAsync();

            foreach (var entityType in entityTypes)
            {
                var properties = await _dbContext.Properties.Where(x => x.OwnerEntityTypeId == entityType.Id).ToListAsync();

                var duplicateProperties = properties.GroupBy(x => x.Name).Where(g => g.Count() > 1).ToList();
                if (duplicateProperties.Count() > 0)
                {
                    errors.Errors.AddRange(duplicateProperties.Select(g => $"Properties { g.Key } of entity {entityType.Name} has repeated more than once"));
                }


                var noPrimaryKey = !properties.Any(x => x.GeneralUsageCategoryId == 2);
                if (noPrimaryKey)
                {
                    errors.Warnings.Add($"There is no primary key in the entity {entityType.Name}");
                }
                else
                {
                    var primaryKeyDuplicate = properties.Where(x => x.GeneralUsageCategoryId == 2)
                                              .GroupBy(g => g.GeneralUsageCategoryId)
                                              .Where(x => x.Count() > 1).ToList();
                    if (primaryKeyDuplicate.Count() > 0)
                    {
                        errors.Errors.Add($"Primary key has repeated more than once in entity { entityType.Name }");
                    }
                    else
                    {
                        var primaryKeyNullable = properties.Where(x => x.GeneralUsageCategoryId == 2 && x.IsNullable).ToList();
                        if (primaryKeyNullable.Count() > 0)
                        {
                            errors.Errors.Add($"Primary key can't be nullable type {entityType.Name}");
                        }
                    }
                }

                if (properties.Where(x => x.DataTypeId == DataTypeEnum.ForeignKey).FirstOrDefault() != null)
                {
                    string propertyName       = properties.Where(x => x.DataTypeId == DataTypeEnum.ForeignKey).FirstOrDefault().Name;
                    var    propertyLastDigits = propertyName.Substring(propertyName.Length - 2);
                    if (propertyLastDigits != "Id")
                    {
                        errors.Warnings.Add($"Foreign key should end with Id keyword in entity {entityType.Name}");
                    }
                }
            }
            if (errors.Errors.Count() > 0)
            {
                errors.Success = false;
            }
            return(errors);
        }