Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf24691b5e | |||
| dd1cfc15e5 | |||
| e937cecc81 |
@@ -2,13 +2,86 @@ using Projects;
|
||||
|
||||
var builder = DistributedApplication.CreateBuilder(args);
|
||||
|
||||
var compose = builder.AddDockerComposeEnvironment("compose")
|
||||
.WithProperties(env => {
|
||||
env.DefaultNetworkName = "system";
|
||||
})
|
||||
.ConfigureComposeFile(file => {
|
||||
file.Networks.Add("main", new() {
|
||||
Name = "main",
|
||||
External = true
|
||||
});
|
||||
|
||||
file.Volumes.Clear();
|
||||
})
|
||||
.ConfigureEnvFile(env => {
|
||||
env["CLIENT_ID"] = new() {
|
||||
Name = "CLIENT_ID",
|
||||
DefaultValue = "SpotifyClientId"
|
||||
};
|
||||
|
||||
env["CLIENT_SECRET"] = new() {
|
||||
Name = "CLIENT_SECRET",
|
||||
DefaultValue = "SpotifyClientSecret"
|
||||
};
|
||||
})
|
||||
.WithDashboard(dashboard => {
|
||||
dashboard.WithForwardedHeaders();
|
||||
dashboard.PublishAsDockerComposeService((_, service) => {
|
||||
service.Name = "dashboard";
|
||||
service.ContainerName = "spotiparty-dashboard";
|
||||
service.Networks.Add("main");
|
||||
service.Restart = "unless-stopped";
|
||||
|
||||
service.Labels.Add("traefik.enable", "true");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-dashboard.tls", "true");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-dashboard.tls.certresolver", "cloudflare");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-dashboard.entrypoints", "websecure");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-dashboard.rule", "Host(`dash.spotiparty.leon-hoppe.de`)");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-dashboard.service", "spotiparty-dashboard");
|
||||
service.Labels.Add("traefik.http.services.spotiparty-dashboard.loadbalancer.server.port", "18888");
|
||||
service.Labels.Add("traefik.docker.network", "main");
|
||||
service.Labels.Add("com.centurylinklabs.watchtower.enable", "true");
|
||||
|
||||
service.Ports.Clear();
|
||||
service.Expose.Clear();
|
||||
});
|
||||
});
|
||||
|
||||
var dbServer = builder.AddPostgres("database")
|
||||
.WithDataVolume();
|
||||
.WithDataVolume()
|
||||
.PublishAsDockerComposeService((_, service) => {
|
||||
service.ContainerName = "spotiparty-db";
|
||||
service.Restart = "unless-stopped";
|
||||
service.Expose.Clear();
|
||||
});
|
||||
|
||||
var database = dbServer.AddDatabase("SpotiParty");
|
||||
|
||||
var web = builder.AddProject<SpotiParty_Web>("web")
|
||||
.WithReference(database)
|
||||
.WaitForStart(database);
|
||||
.WaitForStart(database)
|
||||
.PublishAsDockerComposeService((_, service) => {
|
||||
service.Name = "web";
|
||||
service.ContainerName = "spotiparty-web";
|
||||
service.Image = "registry.leon-hoppe.de/leon.hoppe/spotiparty:latest";
|
||||
service.Networks.Add("main");
|
||||
service.Restart = "unless-stopped";
|
||||
service.Environment.Add("ClientId", "${CLIENT_ID}");
|
||||
service.Environment.Add("ClientSecret", "${CLIENT_SECRET}");
|
||||
|
||||
service.Labels.Add("traefik.enable", "true");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-web.tls", "true");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-web.tls.certresolver", "cloudflare");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-web.entrypoints", "websecure");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-web.rule", "Host(`spotiparty.leon-hoppe.de`)");
|
||||
service.Labels.Add("traefik.http.routers.spotiparty-web.service", "spotiparty-web");
|
||||
service.Labels.Add("traefik.http.services.spotiparty-web.loadbalancer.server.port", "${WEB_PORT}");
|
||||
service.Labels.Add("traefik.docker.network", "main");
|
||||
service.Labels.Add("com.centurylinklabs.watchtower.enable", "true");
|
||||
|
||||
service.Ports.Clear();
|
||||
service.Expose.Clear();
|
||||
});
|
||||
|
||||
builder.Build().Run();
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Hosting.AppHost" Version="13.0.1" />
|
||||
<PackageReference Include="Aspire.Hosting.Docker" Version="13.1.0-preview.1.25616.3" />
|
||||
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -32,7 +32,7 @@ public partial class EnqueuePage(AuthorizationHandler authHandler, NavigationMan
|
||||
}
|
||||
|
||||
_event = await context.Events
|
||||
.Include(e => e.Host)
|
||||
.Include(@event => @event.Host)
|
||||
.FirstOrDefaultAsync(e => e.Id == guid);
|
||||
|
||||
if (_event is null) {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -8,5 +8,13 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
|
||||
public DbSet<User> Users { 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);
|
||||
}
|
||||
}
|
||||
97
SpotiParty.Web/Migrations/20260118172802_Initial.Designer.cs
generated
Normal file
97
SpotiParty.Web/Migrations/20260118172802_Initial.Designer.cs
generated
Normal file
@@ -0,0 +1,97 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using SpotiParty.Web;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SpotiParty.Web.Migrations
|
||||
{
|
||||
[DbContext(typeof(DatabaseContext))]
|
||||
[Migration("20260118172802_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.12")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("End")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<DateTime>("Start")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<Guid>("host")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("host");
|
||||
|
||||
b.ToTable("Events");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.User", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<bool>("IsAdmin")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<string>("SpotifyUserId")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("UserId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||
{
|
||||
b.HasOne("SpotiParty.Web.Models.User", "Host")
|
||||
.WithMany()
|
||||
.HasForeignKey("host")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Host");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
66
SpotiParty.Web/Migrations/20260118172802_Initial.cs
Normal file
66
SpotiParty.Web/Migrations/20260118172802_Initial.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SpotiParty.Web.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Initial : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
UserId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
SpotifyUserId = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
|
||||
DisplayName = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
|
||||
RefreshToken = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
|
||||
IsAdmin = table.Column<bool>(type: "boolean", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.UserId);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Events",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
host = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
|
||||
Start = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
|
||||
End = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Events", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Events_Users_host",
|
||||
column: x => x.host,
|
||||
principalTable: "Users",
|
||||
principalColumn: "UserId",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Events_host",
|
||||
table: "Events",
|
||||
column: "host");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Events");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
94
SpotiParty.Web/Migrations/DatabaseContextModelSnapshot.cs
Normal file
94
SpotiParty.Web/Migrations/DatabaseContextModelSnapshot.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using SpotiParty.Web;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SpotiParty.Web.Migrations
|
||||
{
|
||||
[DbContext(typeof(DatabaseContext))]
|
||||
partial class DatabaseContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.12")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("End")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<DateTime>("Start")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<Guid>("host")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("host");
|
||||
|
||||
b.ToTable("Events");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.User", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<bool>("IsAdmin")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<string>("SpotifyUserId")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("UserId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SpotiParty.Web.Models.Event", b =>
|
||||
{
|
||||
b.HasOne("SpotiParty.Web.Models.User", "Host")
|
||||
.WithMany()
|
||||
.HasForeignKey("host")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Host");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>();
|
||||
@@ -48,8 +49,7 @@ builder.Services.AddHopFrame(config => {
|
||||
});
|
||||
|
||||
context.Table<Event>()
|
||||
.SetDisplayName(Guid.NewGuid().ToString())
|
||||
.Ignore(true);
|
||||
.Ignore();
|
||||
});
|
||||
|
||||
config.AddCustomRepository<EventsDashboardRepo, Event, Guid>(e => e.Id, table => {
|
||||
@@ -66,6 +66,17 @@ builder.Services.AddHopFrame(config => {
|
||||
.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,11 +20,27 @@ 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) {
|
||||
var entity = (Event)o;
|
||||
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;
|
||||
|
||||
await using var context = await contextFactory.CreateDbContextAsync();
|
||||
|
||||
return await context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.RefreshToken == token);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,50 @@ using SpotiParty.Web.Models;
|
||||
|
||||
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) {
|
||||
var user = await handler.GetCurrentUser();
|
||||
if (user is null) return [];
|
||||
|
||||
return await context.Events
|
||||
.AsNoTracking()
|
||||
IQueryable<Event> baseQuery = context.Events
|
||||
.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)
|
||||
.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) {
|
||||
@@ -29,12 +56,14 @@ public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler h
|
||||
}
|
||||
|
||||
public async Task CreateItem(Event item) {
|
||||
await using var tempContext = await factory.CreateDbContextAsync();
|
||||
|
||||
var creator = await handler.GetCurrentUser();
|
||||
context.Attach(creator!);
|
||||
tempContext.Attach(creator!);
|
||||
item.Host = creator!;
|
||||
|
||||
await context.Events.AddAsync(item);
|
||||
await context.SaveChangesAsync();
|
||||
await tempContext.Events.AddAsync(item);
|
||||
await tempContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task EditItem(Event item) {
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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.Design" Version="9.0.12">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
Reference in New Issue
Block a user