Skip to content

Commit

Permalink
#30 Enable placing the SQLite databse in a subfolder.
Browse files Browse the repository at this point in the history
Also add preperatory work for #32.
  • Loading branch information
BasJ93 committed Aug 21, 2023
1 parent 7c2702b commit 2b39cc9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions ApiDockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ RUN dotnet publish -c Release -o out ./MyFoodLog.API/MyFoodLog.API.csproj -p:Sou

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /App
MKDIR Database
COPY --from=build-env /App/out ./
ENTRYPOINT ["dotnet", "MyFoodLog.API.dll"]
2 changes: 1 addition & 1 deletion MyFoodLog.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
builder.Services.AddControllers();
builder.Services.AddOpenFoodFacts();

builder.Services.AddDatabase();
builder.Services.AddDatabase(builder.Configuration);
builder.Services.AddCoreServices();

builder.Services.AddAutoMapper(typeof(Profiles));
Expand Down
6 changes: 5 additions & 1 deletion MyFoodLog.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"DatabaseProvider": "sqlite",
"ConnectionStrings": {
"SQLite": "Filename=Database/MyFoodLog.db"
}
}
14 changes: 11 additions & 3 deletions MyFoodLog.Database/DependencyInjection/DatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyFoodLog.Database.Repositories;
using MyFoodLog.Database.Repositories.Interfaces;
Expand All @@ -6,15 +8,21 @@ namespace MyFoodLog.Database.DependencyInjection;

public static class DatabaseHelper
{
public static IServiceCollection AddDatabase(this IServiceCollection services)
public static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<MyFoodLogDbContext>();
switch (configuration["DatabaseProvider"])
{
default:
services.AddDbContext<MyFoodLogDbContext>(options =>
options.UseSqlite(configuration["ConnectionStrings:SQLite"]));
break;
}

services.AddScoped<IFoodItemConsumptionRepository, FoodItemConsumptionRepository>();
services.AddScoped<IFoodItemRepository, FoodItemRepository>();
services.AddScoped<IMealRepository, MealRepository>();
services.AddScoped<IMealTypeRepository, MealTypeRepository>();

return services;
}
}
8 changes: 1 addition & 7 deletions MyFoodLog.Database/MyFoodLogDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using MyFoodLog.Database.Configuration;
using MyFoodLog.Database.Models;

Expand All @@ -21,7 +22,6 @@ public MyFoodLogDbContext()

public MyFoodLogDbContext(DbContextOptions<MyFoodLogDbContext> options) : base(options)
{
//Database.EnsureCreated();
if (Database.GetPendingMigrations().Any())
{
Database.Migrate();
Expand All @@ -33,10 +33,4 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(FoodItemConfiguration).Assembly);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=MyFoodLog.db");
base.OnConfiguring(optionsBuilder);
}
}

0 comments on commit 2b39cc9

Please sign in to comment.