53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using back.DataModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace back.context;
|
|
|
|
public class EventContext : DbContext
|
|
{
|
|
private DbSet<EventModel> Events { get; set; }
|
|
public EventContext(DbContextOptions<EventContext> options)
|
|
{
|
|
Database.EnsureCreated();
|
|
}
|
|
|
|
public async Task<EventModel?> GetById(string id)
|
|
{
|
|
return await GetById(Guid.Parse(id));
|
|
}
|
|
|
|
public async Task<EventModel?> GetById(Guid id)
|
|
{
|
|
return await Events.FindAsync(id);
|
|
}
|
|
|
|
public async Task<int> GetTotalItems()
|
|
{
|
|
return await Events.CountAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<EventModel>> GetPage(int page = 1, int pageSize = 20)
|
|
{
|
|
if (page < 1) page = 1;
|
|
if (pageSize < 1) pageSize = 20;
|
|
|
|
return await Events
|
|
.OrderByDescending(p => p.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> Exists(EventModel? photo)
|
|
{
|
|
if (photo == null) return false;
|
|
if (string.IsNullOrEmpty(photo.Id)) return false;
|
|
return await Events.AnyAsync(p => p.Id == photo.Id);
|
|
}
|
|
|
|
public async Task<bool> Exists(string id)
|
|
{
|
|
return await Events.AnyAsync(p => p.Id == id);
|
|
}
|
|
}
|