53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using HopFrame.Core.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SpotiParty.Web.Models;
|
|
|
|
namespace SpotiParty.Web.Services;
|
|
|
|
public class EventsDashboardRepo(DatabaseContext context, DashboardAuthHandler handler) : 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)
|
|
.Where(e => e.Host.UserId == user.UserId)
|
|
.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));
|
|
}
|
|
|
|
public async Task<int> GetTotalPageCount(int perPage) {
|
|
double count = await context.Events.CountAsync();
|
|
return Convert.ToInt32(Math.Ceiling(count / perPage));
|
|
}
|
|
|
|
public async Task CreateItem(Event item) {
|
|
var creator = await handler.GetCurrentUser();
|
|
context.Attach(creator!);
|
|
item.Host = creator!;
|
|
|
|
await context.Events.AddAsync(item);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task EditItem(Event item) {
|
|
context.Events.Update(item);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteItem(Event item) {
|
|
context.Events.Remove(item);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<Event?> GetOne(Guid key) {
|
|
return await context.Events.FindAsync(key);
|
|
}
|
|
} |