Fixed user reference problem in event creation

This commit is contained in:
2026-01-19 20:39:18 +01:00
parent e937cecc81
commit dd1cfc15e5
6 changed files with 15 additions and 13 deletions

View File

@@ -4,14 +4,15 @@ 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 async Task<IEnumerable<Event>> LoadPage(int page, int perPage) {
var user = await handler.GetCurrentUser();
if (user is null) return [];
return await context.Events
.AsNoTracking()
.Include(e => e.Host)
.OrderBy(e => e.Id)
.Where(e => e.Host.UserId == user.UserId)
.Skip(page * perPage)
.Take(perPage)
@@ -29,12 +30,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) {