Skip to content

Commit

Permalink
🐛 Fix sqlite publish bug, revert to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
russkyc committed May 28, 2024
1 parent 63c2806 commit 2fd40b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
8 changes: 8 additions & 0 deletions Timely/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 37 additions & 44 deletions Timely/Services/Data/ShiftManager.cs
Original file line number Diff line number Diff line change
@@ -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<Shift>? _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<List<Shift>>("shifts");
_activeShift = await _storage.GetItemAsync<Shift>("activeShift");
}
public async Task<IEnumerable<Shift>?> 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<Shift?> 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<IEnumerable<Shift>?> 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;
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions Timely/Timely.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazor.DB" Version="1.2.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Majorsoft.Blazor.Components.Timer" Version="1.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.5" />
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="PublishSPAforGitHubPages.Build" Version="2.2.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Timely/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Timely - Time Tracker</title>
<base href="/timely/" />
<base href="/" />
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="apple-touch-icon" sizes="512x512" href="icon-512.png" />
<link rel="apple-touch-icon" sizes="192x192" href="icon-192.png" />
Expand Down

0 comments on commit 2fd40b6

Please sign in to comment.