Added multiple qol imporvements
This commit is contained in:
@@ -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;
|
||||||
@@ -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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,7 +46,7 @@ namespace SpotiParty.Web.Migrations
|
|||||||
|
|
||||||
b.HasIndex("host");
|
b.HasIndex("host");
|
||||||
|
|
||||||
b.ToTable("Events", (string)null);
|
b.ToTable("Events");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("SpotiParty.Web.Models.User", b =>
|
modelBuilder.Entity("SpotiParty.Web.Models.User", b =>
|
||||||
@@ -75,7 +75,7 @@ namespace SpotiParty.Web.Migrations
|
|||||||
|
|
||||||
b.HasKey("UserId");
|
b.HasKey("UserId");
|
||||||
|
|
||||||
b.ToTable("Users", (string)null);
|
b.ToTable("Users");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||||
|
|||||||
@@ -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>();
|
||||||
@@ -62,7 +63,19 @@ builder.Services.AddHopFrame(config => {
|
|||||||
table.Property(e => e.Host)
|
table.Property(e => e.Host)
|
||||||
.List(false)
|
.List(false)
|
||||||
.SetEditable(false)
|
.SetEditable(false)
|
||||||
.SetCreatable(false);
|
.SetCreatable(false)
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,22 +6,48 @@ namespace SpotiParty.Web.Services;
|
|||||||
|
|
||||||
public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler handler, IDbContextFactory<DatabaseContext> factory) : 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
|
||||||
.Include(e => e.Host)
|
.Include(e => e.Host)
|
||||||
.OrderBy(e => e.Id)
|
.OrderBy(e => e.Id);
|
||||||
.Where(e => e.Host.UserId == user.UserId)
|
|
||||||
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user