Updaed event logic and event creation system
This commit is contained in:
49
SpotiParty.Web/Services/EventsDashboardRepo.cs
Normal file
49
SpotiParty.Web/Services/EventsDashboardRepo.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user