Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf24691b5e | |||
| dd1cfc15e5 |
@@ -6,7 +6,7 @@ using SpotiParty.Web.Services;
|
|||||||
|
|
||||||
namespace SpotiParty.Web.Components.Pages;
|
namespace SpotiParty.Web.Components.Pages;
|
||||||
|
|
||||||
public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationManager navigator, DatabaseContext context) : ComponentBase {
|
public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationManager navigator, DatabaseContext context, DashboardAuthHandler authContext) : ComponentBase {
|
||||||
|
|
||||||
[Parameter]
|
[Parameter]
|
||||||
public string EventId { get; set; } = string.Empty;
|
public string EventId { get; set; } = string.Empty;
|
||||||
@@ -32,7 +32,7 @@ public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationMan
|
|||||||
}
|
}
|
||||||
|
|
||||||
_event = await context.Events
|
_event = await context.Events
|
||||||
.Include(e => e.Host)
|
.Include(@event => @event.Host)
|
||||||
.FirstOrDefaultAsync(e => e.Id == guid);
|
.FirstOrDefaultAsync(e => e.Id == guid);
|
||||||
|
|
||||||
if (_event is null) {
|
if (_event is null) {
|
||||||
@@ -42,8 +42,10 @@ public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationMan
|
|||||||
|
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
|
var currentUser = await authContext.GetCurrentUser();
|
||||||
|
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
if (_event.Start > now || _event.End < now) {
|
if ((_event.Start > now || _event.End < now) && currentUser?.UserId != _event.Host.UserId) {
|
||||||
navigator.NavigateTo("/", forceLoad: true);
|
navigator.NavigateTo("/", forceLoad: true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,5 +8,13 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
|
|||||||
public DbSet<User> Users { get; set; }
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
public DbSet<Event> Events { get; set; }
|
public DbSet<Event> Events { get; set; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Event>()
|
||||||
|
.HasOne(e => e.Host)
|
||||||
|
.WithMany()
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
22
SpotiParty.Web/Migrations/20260123185122_Delete_handling.cs
Normal file
22
SpotiParty.Web/Migrations/20260123185122_Delete_handling.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace SpotiParty.Web.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class Delete_handling : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ builder.Services.AddDbContextFactory<DatabaseContext>(options => {
|
|||||||
options.UseNpgsql(builder.Configuration.GetConnectionString("SpotiParty"));
|
options.UseNpgsql(builder.Configuration.GetConnectionString("SpotiParty"));
|
||||||
});
|
});
|
||||||
builder.Services.AddScoped<AuthorizationHandler>();
|
builder.Services.AddScoped<AuthorizationHandler>();
|
||||||
|
builder.Services.AddHostedService<CleanupService>();
|
||||||
|
|
||||||
builder.Services.AddScoped<IHopFrameAuthHandler, DashboardAuthHandler>();
|
builder.Services.AddScoped<IHopFrameAuthHandler, DashboardAuthHandler>();
|
||||||
builder.Services.AddScoped<DashboardAuthHandler>();
|
builder.Services.AddScoped<DashboardAuthHandler>();
|
||||||
@@ -48,8 +49,7 @@ builder.Services.AddHopFrame(config => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
context.Table<Event>()
|
context.Table<Event>()
|
||||||
.SetDisplayName(Guid.NewGuid().ToString())
|
.Ignore();
|
||||||
.Ignore(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
config.AddCustomRepository<EventsDashboardRepo, Event, Guid>(e => e.Id, table => {
|
config.AddCustomRepository<EventsDashboardRepo, Event, Guid>(e => e.Id, table => {
|
||||||
@@ -66,6 +66,17 @@ builder.Services.AddHopFrame(config => {
|
|||||||
.SetCreatable(false)
|
.SetCreatable(false)
|
||||||
.SetDisplayedProperty(u => u.DisplayName);
|
.SetDisplayedProperty(u => u.DisplayName);
|
||||||
|
|
||||||
|
table.Property(e => e.Name)
|
||||||
|
.SetValidator((name, _) => {
|
||||||
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
|
return ["Name cannot be empty"];
|
||||||
|
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
|
||||||
|
var innerConf = table.Property(e => e.Name).InnerConfig;
|
||||||
|
innerConf.GetType().GetProperty(nameof(innerConf.IsRequired))!.SetValue(innerConf, true);
|
||||||
|
|
||||||
table.ShowSearchSuggestions(false);
|
table.ShowSearchSuggestions(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using HopFrame.Core.Config;
|
using HopFrame.Core.Config;
|
||||||
|
using HopFrame.Web.Components.Pages;
|
||||||
using HopFrame.Web.Plugins.Events;
|
using HopFrame.Web.Plugins.Events;
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.FluentUI.AspNetCore.Components;
|
using Microsoft.FluentUI.AspNetCore.Components;
|
||||||
@@ -6,10 +7,12 @@ using SpotiParty.Web.Models;
|
|||||||
|
|
||||||
namespace SpotiParty.Web.Services;
|
namespace SpotiParty.Web.Services;
|
||||||
|
|
||||||
public class AdminDashboardPlugin(NavigationManager navigator) {
|
public class AdminDashboardPlugin(NavigationManager navigator, DashboardAuthHandler auth, EventsDashboardRepo repo) {
|
||||||
|
|
||||||
|
private HopFrameTablePage _page;
|
||||||
|
|
||||||
[HopFrame.Web.Plugins.Annotations.EventHandler]
|
[HopFrame.Web.Plugins.Annotations.EventHandler]
|
||||||
public void OnTableInitialized(TableInitializedEvent e) {
|
public async Task OnTableInitialized(TableInitializedEvent e) {
|
||||||
if (e.Table.TableType != typeof(Event)) return;
|
if (e.Table.TableType != typeof(Event)) return;
|
||||||
|
|
||||||
e.AddEntityButton(new IconInfo {
|
e.AddEntityButton(new IconInfo {
|
||||||
@@ -17,11 +20,27 @@ public class AdminDashboardPlugin(NavigationManager navigator) {
|
|||||||
Size = IconSize.Size16,
|
Size = IconSize.Size16,
|
||||||
Name = "Open"
|
Name = "Open"
|
||||||
}, OnButtonClicked);
|
}, OnButtonClicked);
|
||||||
|
|
||||||
|
var user = await auth.GetCurrentUser();
|
||||||
|
|
||||||
|
if (user is not null && user.IsAdmin) {
|
||||||
|
e.AddPageButton("Show all", ToggleAllEvents);
|
||||||
|
}
|
||||||
|
|
||||||
|
_page = e.Sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnButtonClicked(object o, TableConfig config) {
|
private void OnButtonClicked(object o, TableConfig config) {
|
||||||
var entity = (Event)o;
|
var entity = (Event)o;
|
||||||
navigator.NavigateTo(navigator.BaseUri + $"enqueue/{entity.Id}");
|
navigator.NavigateTo(navigator.BaseUri + $"enqueue/{entity.Id}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task ToggleAllEvents() {
|
||||||
|
if (repo.ShowAllEvents)
|
||||||
|
return;
|
||||||
|
|
||||||
|
repo.ShowAllEvents = true;
|
||||||
|
await _page.Reload();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
27
SpotiParty.Web/Services/CleanupService.cs
Normal file
27
SpotiParty.Web/Services/CleanupService.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace SpotiParty.Web.Services;
|
||||||
|
|
||||||
|
public class CleanupService(IServiceProvider services, ILogger<CleanupService> logger) : BackgroundService {
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
|
||||||
|
while (!stoppingToken.IsCancellationRequested) {
|
||||||
|
await using (var scope = services.CreateAsyncScope()) {
|
||||||
|
var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
|
||||||
|
var now = DateTime.Now;
|
||||||
|
var oldEvents = await context.Events
|
||||||
|
.Where(e => e.End < now)
|
||||||
|
.ToArrayAsync(stoppingToken);
|
||||||
|
|
||||||
|
context.Events.RemoveRange(oldEvents);
|
||||||
|
await context.SaveChangesAsync(stoppingToken);
|
||||||
|
|
||||||
|
if (oldEvents.Length > 0)
|
||||||
|
logger.LogInformation("Deleted {count} old events", oldEvents.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
var nextRun = DateTime.Today.AddDays(1).AddHours(3) - DateTime.Now;
|
||||||
|
await Task.Delay(nextRun, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@ public class DashboardAuthHandler(ClientSideStorage storage, IDbContextFactory<D
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
await using var context = await contextFactory.CreateDbContextAsync();
|
await using var context = await contextFactory.CreateDbContextAsync();
|
||||||
|
|
||||||
return await context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.RefreshToken == token);
|
return await context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.RefreshToken == token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,23 +4,50 @@ using SpotiParty.Web.Models;
|
|||||||
|
|
||||||
namespace SpotiParty.Web.Services;
|
namespace SpotiParty.Web.Services;
|
||||||
|
|
||||||
public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler handler) : IHopFrameRepository<Event, Guid> {
|
public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler handler, IDbContextFactory<DatabaseContext> factory) : IHopFrameRepository<Event, Guid> {
|
||||||
|
|
||||||
|
public bool ShowAllEvents { get; set; } = false;
|
||||||
|
|
||||||
public async Task<IEnumerable<Event>> LoadPage(int page, int perPage) {
|
public async Task<IEnumerable<Event>> LoadPage(int page, int perPage) {
|
||||||
var user = await handler.GetCurrentUser();
|
var user = await handler.GetCurrentUser();
|
||||||
if (user is null) return [];
|
if (user is null) return [];
|
||||||
|
|
||||||
return await context.Events
|
IQueryable<Event> baseQuery = context.Events
|
||||||
.AsNoTracking()
|
|
||||||
.Include(e => e.Host)
|
.Include(e => e.Host)
|
||||||
.Where(e => e.Host.UserId == user.UserId)
|
.OrderBy(e => e.Id);
|
||||||
|
|
||||||
|
if (!ShowAllEvents) {
|
||||||
|
baseQuery = baseQuery.Where(e => e.Host.UserId == user.UserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await baseQuery
|
||||||
.Skip(page * perPage)
|
.Skip(page * perPage)
|
||||||
.Take(perPage)
|
.Take(perPage)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SearchResult<Event>> Search(string searchTerm, int page, int perPage) {
|
public async Task<SearchResult<Event>> Search(string searchTerm, int page, int perPage) {
|
||||||
var entries = await LoadPage(page, perPage);
|
var user = await handler.GetCurrentUser();
|
||||||
return new(entries, await GetTotalPageCount(perPage));
|
if (user is null) return new(Enumerable.Empty<Event>(), 0);
|
||||||
|
|
||||||
|
IQueryable<Event> baseQuery = context.Events
|
||||||
|
.Include(e => e.Host)
|
||||||
|
.OrderBy(e => e.Id);
|
||||||
|
|
||||||
|
if (!ShowAllEvents) {
|
||||||
|
baseQuery = baseQuery.Where(e => e.Host.UserId == user.UserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
baseQuery = baseQuery
|
||||||
|
.Where(e => e.Name.ToLower().Contains(searchTerm.ToLower()));
|
||||||
|
|
||||||
|
var totalEntries = await baseQuery.CountAsync();
|
||||||
|
var entries = await baseQuery
|
||||||
|
.Skip(page * perPage)
|
||||||
|
.Take(perPage)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return new(entries, (int)Math.Ceiling(totalEntries / (double)perPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> GetTotalPageCount(int perPage) {
|
public async Task<int> GetTotalPageCount(int perPage) {
|
||||||
@@ -29,12 +56,14 @@ public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler h
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateItem(Event item) {
|
public async Task CreateItem(Event item) {
|
||||||
|
await using var tempContext = await factory.CreateDbContextAsync();
|
||||||
|
|
||||||
var creator = await handler.GetCurrentUser();
|
var creator = await handler.GetCurrentUser();
|
||||||
context.Attach(creator!);
|
tempContext.Attach(creator!);
|
||||||
item.Host = creator!;
|
item.Host = creator!;
|
||||||
|
|
||||||
await context.Events.AddAsync(item);
|
await tempContext.Events.AddAsync(item);
|
||||||
await context.SaveChangesAsync();
|
await tempContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task EditItem(Event item) {
|
public async Task EditItem(Event item) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.5.2" />
|
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.5.2" />
|
||||||
<PackageReference Include="HopFrame.Web" Version="3.3.0" />
|
<PackageReference Include="HopFrame.Web" Version="3.3.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.12" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.12" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.12">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.12">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
Reference in New Issue
Block a user