Fixed user reference problem in event creation
This commit is contained in:
@@ -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,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) {
|
||||
|
||||
Reference in New Issue
Block a user