From 2fd40b67ceed4b7a71031b6de9cd4c3a43014e39 Mon Sep 17 00:00:00 2001 From: Russell Camo Date: Tue, 28 May 2024 09:20:53 +0800 Subject: [PATCH] :bug: Fix sqlite publish bug, revert to local storage --- Timely/Pages/Home.razor | 8 +++ Timely/Services/Data/ShiftManager.cs | 81 +++++++++++++--------------- Timely/Timely.csproj | 4 +- Timely/wwwroot/index.html | 2 +- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Timely/Pages/Home.razor b/Timely/Pages/Home.razor index ef09899..a50abf1 100644 --- a/Timely/Pages/Home.razor +++ b/Timely/Pages/Home.razor @@ -120,6 +120,14 @@ Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter; } + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + await ShiftManager.GetData(); + } + } + async Task StartShift() { _currentShift = new Shift diff --git a/Timely/Services/Data/ShiftManager.cs b/Timely/Services/Data/ShiftManager.cs index 7ce8c71..0a26827 100644 --- a/Timely/Services/Data/ShiftManager.cs +++ b/Timely/Services/Data/ShiftManager.cs @@ -1,65 +1,60 @@ using Blazored.LocalStorage; -using Microsoft.EntityFrameworkCore; using Timely.Models; namespace Timely.Services.Data; public class ShiftManager { + private readonly ILocalStorageService _storage; private Shift? _activeShift; + private List? _shifts = new(); - public ShiftManager() + public ShiftManager(ILocalStorageService storage) { - using var db = new AppDbContext(); - db.Database.EnsureCreated(); + _storage = storage; } - public async Task AddTimeRecord(Shift shift) + public async Task GetData() { - await using var db = new AppDbContext(); - await db.Shifts.AddAsync(shift); - await db.SaveToCacheAsync(); + _shifts = await _storage.GetItemAsync>("shifts"); + _activeShift = await _storage.GetItemAsync("activeShift"); } - - public async Task?> GetTimeRecords() + + public async Task SaveData() { - await using var db = new AppDbContext(); - return await db.Shifts.Where(shift => !shift.Active).ToListAsync(); + await _storage.SetItemAsync("activeShift", _activeShift); + await _storage.SetItemAsync("shifts", _shifts); } - public async Task ClearTimeRecords() + public async Task AddTimeRecord(Shift shift) { - await using var db = new AppDbContext(); - await db.Shifts.AsQueryable().ExecuteDeleteAsync(); - await db.SaveToCacheAsync(); + await GetData(); + + _shifts ??= new(); + _shifts.Add(shift); + + await SaveData(); } public async Task GetCurrentShift() { - if (_activeShift is not null) return _activeShift; - - await using var db = new AppDbContext(); - _activeShift = await db.Shifts.FirstOrDefaultAsync(shift => shift.Active); - + await GetData(); return _activeShift; } - public async Task AddShift(TimeSpan start, TimeSpan end, DateTime? date = null) + public async Task?> GetTimeRecords() { - - var shift = new Shift() - { - ShiftStart = start, - ShiftEnd = end, - Date = date ?? DateTime.Today - }; - - await using var db = new AppDbContext(); - await db.Shifts.AddAsync(shift); - await db.SaveToCacheAsync(); + await GetData(); + return _shifts; + } + public async Task ClearTimeRecords() + { + _shifts = null; + await _storage.RemoveItemAsync("shifts"); + await SaveData(); } - + public async Task StartShift() { if (_activeShift is not null) return; @@ -71,27 +66,25 @@ public async Task StartShift() Active = true }; - await using var db = new AppDbContext(); - var entry = await db.Shifts.AddAsync(shift); - await db.SaveToCacheAsync(); - - _activeShift = entry.Entity; + _activeShift = shift; + await SaveData(); } public async Task EndShift() { - await GetCurrentShift(); + await GetData(); - await using var db = new AppDbContext(); if (_activeShift is null) return; _activeShift.Active = false; _activeShift.ShiftEnd = DateTime.Now.TimeOfDay; - - db.Shifts.Update(_activeShift); - await db.SaveToCacheAsync(); + _shifts ??= new(); + + _shifts.Add(_activeShift); _activeShift = null; + + await SaveData(); } } \ No newline at end of file diff --git a/Timely/Timely.csproj b/Timely/Timely.csproj index 22de9c4..6c51449 100644 --- a/Timely/Timely.csproj +++ b/Timely/Timely.csproj @@ -8,13 +8,13 @@ - - + + diff --git a/Timely/wwwroot/index.html b/Timely/wwwroot/index.html index 82e04a8..555ad18 100644 --- a/Timely/wwwroot/index.html +++ b/Timely/wwwroot/index.html @@ -5,7 +5,7 @@ Timely - Time Tracker - +