Skip to content

Commit

Permalink
Added new options:
Browse files Browse the repository at this point in the history
* Enable / Disable ODATA completely
* Enable XML Comments for swagger

Fixed
* Fixed a bug that prevented the service to start when the connectionstring wasn't set despite using InMemory
  • Loading branch information
DeeJayTC committed Mar 29, 2022
1 parent d57c104 commit 226d5f6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
2 changes: 2 additions & 0 deletions sample/ApiGeneratorSampleApp/Model/Person.cs
Expand Up @@ -22,6 +22,8 @@ public class Person : Trackable,
IEntityTypeConfiguration<Person> // Configure Table Options yourself
{
public string Name { get; set; }


public DateTime Date { get; set; }
public string Description { get; set; }
public int Age { get; set; }
Expand Down
11 changes: 3 additions & 8 deletions sample/ApiGeneratorSampleApp/appsettings.json
Expand Up @@ -12,9 +12,7 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform

"CallbackPath": "/signin-oidc"
},
"ConnectionStrings": {
"ApiGeneratorDatabase": "Server=localhost;database=tcdev_dev_222;user=sa;password=Password!23;"
},

"Logging": {
"LogLevel": {
"Default": "Information",
Expand All @@ -25,10 +23,6 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
"AllowedHosts": "*",
//Sample Config for API Generator
"Api": {
"Database": {
"DatabaseType": "SQL"
}
}
"Swagger": {
"EnableProduction": "false", // Enable/Disable for production builds
"Description": "Sample Swagger Config",
Expand All @@ -38,9 +32,10 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
"ContactUri": "https://www.myuri.com"
},
"Database": {
"DatabaseType": "SQL|InMemory|SQLite"
"DatabaseType": "InMemory"
},
"Odata": {
"EnableOData": false,
"EnableSelect": true,
"EnableFilter": true,
"EnableSort": true
Expand Down
11 changes: 6 additions & 5 deletions src/TCDev.APIGenerator/Data/GenericDbContext.cs
Expand Up @@ -43,13 +43,12 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var config = new ApiGeneratorConfig(null);
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("secrets.json")
.Build();
var connectionString = configuration.GetConnectionString("ApiGeneratorDatabase");

var config = new ApiGeneratorConfig(configuration);
// Add Database Context

switch (config.DatabaseOptions.DatabaseType)
Expand All @@ -58,10 +57,12 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseInMemoryDatabase("ApiGeneratorDB");
break;
case DBType.SQL:
optionsBuilder.UseSqlServer(connectionString);
var connectionStringSQL = configuration.GetConnectionString("ApiGeneratorDatabase");
optionsBuilder.UseSqlServer(connectionStringSQL);
break;
case DBType.SQLite:
optionsBuilder.UseSqlite(connectionString);
var connectionStringSQLite = configuration.GetConnectionString("ApiGeneratorDatabase");
optionsBuilder.UseSqlite(connectionStringSQLite);
break;
default:
throw new Exception("Database Type Unkown");
Expand Down
14 changes: 14 additions & 0 deletions src/TCDev.APIGenerator/Extension/ApiGeneratorConfig.cs
Expand Up @@ -24,19 +24,32 @@ public ApiGeneratorConfig(IConfiguration config)
}

//Load Options
configuration.Bind("API:Basic", APIOptions);
configuration.Bind("Api:Cache", CacheOptions);
configuration.Bind("Api:Swagger", SwaggerOptions);
configuration.Bind("Api:Database", DatabaseOptions);
configuration.Bind("Api:Odata", ODataOptions);
}

private readonly IConfigurationRoot Configuration;
public CacheOptions CacheOptions { get; set; } = new CacheOptions();

public APIOptions APIOptions { get; set; } = new APIOptions();

public SwaggerOptions SwaggerOptions { get; set; } = new SwaggerOptions();
public DatabaseOptions DatabaseOptions { get; set; } = new DatabaseOptions();
public ODataFunctions ODataOptions { get; set; } = new ODataFunctions();

public string MetadataRoute { get; set; } = "odata";
}


public class APIOptions
{
public bool UseXMLComments { get; set; } = false;
public string XMLCommentsFile { get; set; } = string.Empty;
}

public class CacheOptions
{
public bool Enabled { get; set; } = true;
Expand All @@ -61,6 +74,7 @@ public class DatabaseOptions

public class ODataFunctions
{
public bool EnableOData = false;
public bool EnableSelect { get; set; } = true;
public bool EnableFilter { get; set; } = true;
public bool EnableSort { get; set; } = true;
Expand Down
22 changes: 17 additions & 5 deletions src/TCDev.APIGenerator/Extension/ApiGeneratorExtension.cs
Expand Up @@ -87,19 +87,31 @@ public static class ApiGeneratorExtension
});
c.DocumentFilter<ShowInSwaggerFilter>();
//c.IncludeXmlComments($"{assembly.GetName().Name}.xml", true);
if(ApiGeneratorConfig.APIOptions.UseXMLComments)
{
if (!string.IsNullOrEmpty(ApiGeneratorConfig.APIOptions.XMLCommentsFile))
{
throw new Exception("You need to set XMLCommentsFile option when using XMl Comments");
} else {
c.IncludeXmlComments(ApiGeneratorConfig.APIOptions.XMLCommentsFile, true);
}
}
});


services.AddControllers().AddOData(opt =>
if(ApiGeneratorConfig.ODataOptions.EnableOData)
{
services.AddControllers().AddOData(opt =>
{
opt.AddRouteComponents("odata", GenericDbContext.EdmModel);
opt.EnableNoDollarQueryOptions = true;
opt.EnableQueryFeatures(20000);
opt.Select().Expand().Filter();
}
);

});
} else {
services.AddControllers();
}

return services;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TCDev.APIGenerator/TCDev.APIGenerator.csproj
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>TCDev.APIGenerator</PackageId>
<Version>0.0.5-alpha</Version>
<Version>0.0.8-alpha</Version>
<Authors>Tim Cadenbach</Authors>
<Company>TCDev</Company>
<Description>Creates fully working CRUD Apis from just class files</Description>
Expand Down

0 comments on commit 226d5f6

Please sign in to comment.