Skip to content

Commit 8277bda

Browse files
author
CyberReady
committed
Implement SQLite database support and refactor InventoryService for EF Core integration; update Dockerfile and docker-compose for data persistence
1 parent 6b98bf0 commit 8277bda

6 files changed

Lines changed: 97 additions & 35 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ WORKDIR /app
1111
EXPOSE 8080
1212
ENV ASPNETCORE_URLS=http://+:8080
1313
COPY --from=build /app/publish .
14+
RUN mkdir -p /app/Data && chown app:app /app/Data
1415
ENTRYPOINT ["dotnet", "MoveIT.dll"]

MoveIT/Data/MoveItDbContext.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using MoveIT.Models;
3+
4+
namespace MoveIT.Data;
5+
6+
public class MoveItDbContext : DbContext
7+
{
8+
public MoveItDbContext(DbContextOptions<MoveItDbContext> options) : base(options) { }
9+
10+
public DbSet<MovingItem> Items => Set<MovingItem>();
11+
12+
protected override void OnModelCreating(ModelBuilder modelBuilder)
13+
{
14+
modelBuilder.Entity<MovingItem>(entity =>
15+
{
16+
entity.HasKey(x => x.Id);
17+
// Computed properties on the POCO — not stored.
18+
entity.Ignore(x => x.TotalMinutes);
19+
entity.Ignore(x => x.CubicFeet);
20+
entity.Ignore(x => x.DimensionsDisplay);
21+
});
22+
}
23+
}

MoveIT/MoveIT.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.0" />
12+
</ItemGroup>
13+
1014
</Project>

MoveIT/Program.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1+
using Microsoft.EntityFrameworkCore;
12
using MoveIT.Components;
3+
using MoveIT.Data;
4+
using MoveIT.Models;
25
using MoveIT.Services;
36

47
var builder = WebApplication.CreateBuilder(args);
58

6-
// Add services to the container.
79
builder.Services.AddRazorComponents()
810
.AddInteractiveServerComponents();
11+
12+
var dbDir = Path.Combine(builder.Environment.ContentRootPath, "Data");
13+
Directory.CreateDirectory(dbDir);
14+
var dbPath = Path.Combine(dbDir, "inventory.db");
15+
16+
builder.Services.AddDbContextFactory<MoveItDbContext>(opts =>
17+
opts.UseSqlite($"Data Source={dbPath}"));
18+
919
builder.Services.AddSingleton<InventoryService>();
1020

1121
var app = builder.Build();
1222

13-
// Configure the HTTP request pipeline.
23+
// Ensure schema, then one-time seed from legacy inventory.json if DB is empty.
24+
using (var scope = app.Services.CreateScope())
25+
{
26+
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<MoveItDbContext>>();
27+
using var db = factory.CreateDbContext();
28+
db.Database.EnsureCreated();
29+
30+
if (!db.Items.Any())
31+
{
32+
var jsonPath = Path.Combine(app.Environment.ContentRootPath, "inventory.json");
33+
if (File.Exists(jsonPath))
34+
{
35+
try
36+
{
37+
var json = File.ReadAllText(jsonPath);
38+
var items = System.Text.Json.JsonSerializer.Deserialize<List<MovingItem>>(json);
39+
if (items is { Count: > 0 })
40+
{
41+
db.Items.AddRange(items);
42+
db.SaveChanges();
43+
}
44+
}
45+
catch
46+
{
47+
// Legacy file unreadable — skip seeding, app continues with empty DB.
48+
}
49+
}
50+
}
51+
}
52+
1453
if (!app.Environment.IsDevelopment())
1554
{
1655
app.UseExceptionHandler("/Error", createScopeForErrors: true);
17-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
1856
app.UseHsts();
1957
}
2058
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
using System.Text.Json;
2-
using Microsoft.AspNetCore.Hosting;
1+
using Microsoft.EntityFrameworkCore;
2+
using MoveIT.Data;
33
using MoveIT.Models;
44

55
namespace MoveIT.Services;
66

77
public class InventoryService
88
{
9-
private readonly string _filePath;
10-
private List<MovingItem> _items;
11-
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
9+
private readonly IDbContextFactory<MoveItDbContext> _dbFactory;
10+
private readonly List<MovingItem> _items;
1211

13-
public InventoryService(IWebHostEnvironment env)
12+
public InventoryService(IDbContextFactory<MoveItDbContext> dbFactory)
1413
{
15-
_filePath = Path.Combine(env.ContentRootPath, "inventory.json");
16-
_items = Load();
14+
_dbFactory = dbFactory;
15+
using var db = _dbFactory.CreateDbContext();
16+
_items = db.Items.AsNoTracking().ToList();
1717
}
1818

1919
public IReadOnlyList<MovingItem> Items => _items.AsReadOnly();
@@ -25,40 +25,31 @@ public InventoryService(IWebHostEnvironment env)
2525

2626
public void Add(MovingItem item)
2727
{
28+
using var db = _dbFactory.CreateDbContext();
29+
db.Items.Add(item);
30+
db.SaveChanges();
2831
_items.Add(item);
29-
Save();
3032
}
3133

3234
public void Update(MovingItem updated)
3335
{
36+
using var db = _dbFactory.CreateDbContext();
37+
var existing = db.Items.Find(updated.Id);
38+
if (existing is null) return;
39+
db.Entry(existing).CurrentValues.SetValues(updated);
40+
db.SaveChanges();
41+
3442
var idx = _items.FindIndex(x => x.Id == updated.Id);
35-
if (idx >= 0)
36-
{
37-
_items[idx] = updated;
38-
Save();
39-
}
43+
if (idx >= 0) _items[idx] = updated;
4044
}
4145

4246
public void Remove(Guid id)
4347
{
48+
using var db = _dbFactory.CreateDbContext();
49+
var existing = db.Items.Find(id);
50+
if (existing is null) return;
51+
db.Items.Remove(existing);
52+
db.SaveChanges();
4453
_items.RemoveAll(x => x.Id == id);
45-
Save();
4654
}
47-
48-
private List<MovingItem> Load()
49-
{
50-
if (!File.Exists(_filePath)) return [];
51-
try
52-
{
53-
var json = File.ReadAllText(_filePath);
54-
return JsonSerializer.Deserialize<List<MovingItem>>(json, _jsonOptions) ?? [];
55-
}
56-
catch
57-
{
58-
return [];
59-
}
60-
}
61-
62-
private void Save() =>
63-
File.WriteAllText(_filePath, JsonSerializer.Serialize(_items, _jsonOptions));
6455
}

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ services:
55
ports:
66
- "8080:8080"
77
restart: unless-stopped
8+
volumes:
9+
- moveit-data:/app/Data
10+
11+
volumes:
12+
moveit-data:

0 commit comments

Comments
 (0)