Example #1
0
        public ApiModule(IDataServiceHostSettings dshs, IAuthorisation authorisation, IDataServiceDesignerDataService dsdDataService, ISchemaBrowserRepository sbRepository)
            : base("api/dataservicedesigner")
        {
            _dshs           = dshs;
            _authorisation  = authorisation;
            _dsdDataService = dsdDataService;
            _sbRepository   = sbRepository;

            Get[@"/availableschemas/{dataServiceName}", true] = async(args, ct) =>
            {
                string dataServiceName = args.dataServiceName;

                var builder = new QueryBuilder <DomainDataService>()
                              .Filter(d => d.Property(p => p.Name).EqualTo(dataServiceName))
                              .Expand(d => d.Connection);

                var roleIds = await _authorisation.GetAdministratorRoleIdsAsync();

                var queryResult = _dsdDataService.Query(new Query(builder.GetQuery()), BwfSystemUser, roleIds, string.Empty, _dshs.SystemToken, out var fault);

                var dataService = queryResult?.Records?.SingleOrDefault() as DomainDataService;

                if (dataService is null)
                {
                    return(Response.AsJson(Enumerable.Empty <DbSchema>(), HttpStatusCode.BadRequest));
                }

                var schemas = _sbRepository.GetWhere <DbSchema>(s => s.Connection.Name == dataService.Connection.Name);
                return(Response.AsJson(schemas));
            };

            Get[@"generatetemplate/{solutionName}", true] = async(args, ct) => {
                string solutionName = args.solutionName;

                var solution = await GetSolution(solutionName);

                return(await Task.Run(() =>
                {
                    var templateGenerator = new TemplateGenerator();
                    var baseDir = AppDomain.CurrentDomain.BaseDirectory;
                    var zipPath = Path.Combine(baseDir, $"DownLoads\\{solutionName}");
                    var zipFile = templateGenerator.GenerateAllAndZip(solution, zipPath);

                    var stream = new FileStream(zipFile, FileMode.Open);
                    var response = new StreamResponse(() => stream, MimeTypes.GetMimeType(zipFile));
                    var fileName = Path.GetFileName(zipFile);
                    var attachment = response.AsAttachment(fileName);
                    return attachment;
                }));
            };

            async Task <DataServiceSolution> GetSolution(string solutionName)
            {
                var builder = new QueryBuilder <DataServiceSolution>()
                              .Expand(p => p.DataServices[0].Solution)
                              .Expand(p => p.DataServices[0].Connection)
                              .Expand(p => p.DataServices[0].Schemas)
                              .Expand(p => p.DataServices[0].Schemas[0].Objects)
                              .Expand(p => p.DataServices[0].Schemas[0].Objects[0].Properties)
                              .Expand(p => p.DataServices[0].Schemas[0].References)
                              .Expand(p => p.DataServices[0].Schemas[0].References[0].Properties)
                              .Filter(d => d.Property(p => p.Name).EqualTo(solutionName));

                var roleIds = await _authorisation.GetAdministratorRoleIdsAsync();

                var queryResult = _dsdDataService.Query(new Query(builder.GetQuery()), BwfSystemUser, roleIds, string.Empty, _dshs.SystemToken, out var fault);

                var solution = queryResult?.Records?.SingleOrDefault() as DataServiceSolution;

                return(solution);
            }
        }
Example #2
0
        public void MyTestMethod()
        {
            TemplateGenerator generator   = new TemplateGenerator();
            DomainDataService dataService = new DomainDataService();

            dataService.Connection = new DataServiceConnection()
            {
                InitialCatalog = "TestDB"
            };
            dataService.Name    = "Test";
            dataService.Schemas = new List <DomainSchema>()
            {
                new DomainSchema()
                {
                    SchemaName = "schema1",
                    IsDefault  = true,
                    Objects    = new List <DomainObject> {
                        new DomainObject()
                        {
                            TableName   = "table1",
                            ObjectName  = "Object1",
                            DisplayName = "Object 1",
                            Properties  = new List <DomainObjectProperty>()
                            {
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Property1",
                                    ColumnName   = "prop1",
                                    PropertyType = PropertyType.Int64
                                },
                                new DomainObjectProperty()
                                {
                                    IsPartOfKey  = true,
                                    PropertyName = "Property2",
                                    ColumnName   = "prop2",
                                    PropertyType = PropertyType.String
                                },
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Property3",
                                    ColumnName   = "prop3",
                                    PropertyType = PropertyType.DateTime
                                },
                                new DomainObjectProperty()
                                {
                                    PropertyName = "property4",
                                    ColumnName   = "prop4",
                                    PropertyType = PropertyType.Boolean
                                },
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Property5",
                                    ColumnName   = "prop5",
                                    PropertyType = PropertyType.Decimal
                                }
                            }
                        },
                        new DomainObject()
                        {
                            TableName   = "table2",
                            ObjectName  = "Object2",
                            DisplayName = "Object 2",
                            Properties  = new List <DomainObjectProperty>()
                            {
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Id",
                                    PropertyType = PropertyType.Int64,
                                    IsPartOfKey  = true
                                },
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Property2",
                                    PropertyType = PropertyType.String,
                                    Length       = 64
                                },
                                new DomainObjectProperty()
                                {
                                    PropertyName = "Property2",
                                    PropertyType = PropertyType.String,
                                    IsNullable   = true,
                                    Length       = 64
                                }
                            }
                        }
                    }
                }
            };

            DataServiceSolution solution = new DataServiceSolution()
            {
                NamespacePrefix    = "Prefix",
                ServiceName        = "Test.Service",
                ServiceDisplayName = "Test Service",
                ServiceDescription = "Test Service",
                CompanyName        = "Test Co",
                DataServices       = new List <DomainDataService> {
                    dataService
                }
            };

            dataService.Solution = solution;

            var outputPath = Path.Combine(Environment.CurrentDirectory, "Test");

            generator.GenerateAllAndZip(solution, outputPath);
        }