Added multiple qol imporvements
This commit is contained in:
@@ -6,7 +6,7 @@ using SpotiParty.Web.Services;
|
||||
|
||||
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]
|
||||
public string EventId { get; set; } = string.Empty;
|
||||
@@ -42,8 +42,10 @@ public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationMan
|
||||
|
||||
StateHasChanged();
|
||||
|
||||
var currentUser = await authContext.GetCurrentUser();
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -9,4 +9,12 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
|
||||
|
||||
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.ToTable("Events", (string)null);
|
||||
b.ToTable("Events");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.User", b =>
|
||||
@@ -75,7 +75,7 @@ namespace SpotiParty.Web.Migrations
|
||||
|
||||
b.HasKey("UserId");
|
||||
|
||||
b.ToTable("Users", (string)null);
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||
|
||||
@@ -26,6 +26,7 @@ builder.Services.AddDbContextFactory<DatabaseContext>(options => {
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("SpotiParty"));
|
||||
});
|
||||
builder.Services.AddScoped<AuthorizationHandler>();
|
||||
builder.Services.AddHostedService<CleanupService>();
|
||||
|
||||
builder.Services.AddScoped<IHopFrameAuthHandler, DashboardAuthHandler>();
|
||||
builder.Services.AddScoped<DashboardAuthHandler>();
|
||||
@@ -62,7 +63,19 @@ builder.Services.AddHopFrame(config => {
|
||||
table.Property(e => e.Host)
|
||||
.List(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);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Pages;
|
||||
using HopFrame.Web.Plugins.Events;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.FluentUI.AspNetCore.Components;
|
||||
@@ -6,10 +7,12 @@ using SpotiParty.Web.Models;
|
||||
|
||||
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]
|
||||
public void OnTableInitialized(TableInitializedEvent e) {
|
||||
public async Task OnTableInitialized(TableInitializedEvent e) {
|
||||
if (e.Table.TableType != typeof(Event)) return;
|
||||
|
||||
e.AddEntityButton(new IconInfo {
|
||||
@@ -17,6 +20,14 @@ public class AdminDashboardPlugin(NavigationManager navigator) {
|
||||
Size = IconSize.Size16,
|
||||
Name = "Open"
|
||||
}, 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) {
|
||||
@@ -24,4 +35,12 @@ public class AdminDashboardPlugin(NavigationManager navigator) {
|
||||
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 bool ShowAllEvents { get; set; } = false;
|
||||
|
||||
public async Task<IEnumerable<Event>> LoadPage(int page, int perPage) {
|
||||
var user = await handler.GetCurrentUser();
|
||||
if (user is null) return [];
|
||||
|
||||
return await context.Events
|
||||
IQueryable<Event> baseQuery = context.Events
|
||||
.Include(e => e.Host)
|
||||
.OrderBy(e => e.Id)
|
||||
.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)
|
||||
.Take(perPage)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<SearchResult<Event>> Search(string searchTerm, int page, int perPage) {
|
||||
var entries = await LoadPage(page, perPage);
|
||||
return new(entries, await GetTotalPageCount(perPage));
|
||||
var user = await handler.GetCurrentUser();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user