48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using back.DataModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace back.context;
|
|
|
|
public class GalleryContext : DbContext
|
|
{
|
|
public DbSet<GalleryModel> Galleries { get; set; }
|
|
public GalleryContext(DbContextOptions<GalleryContext> options) : base(options)
|
|
{
|
|
// Ensure database is created
|
|
Database.EnsureCreated();
|
|
}
|
|
|
|
public async Task<GalleryModel?> GetById(Guid id)
|
|
{
|
|
return await Galleries.FindAsync(id);
|
|
}
|
|
|
|
public async Task<int> GetTotalItems()
|
|
{
|
|
return await Galleries.CountAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<GalleryModel>> GetPage(int page = 1, int pageSize = 20)
|
|
{
|
|
if (page < 1) page = 1;
|
|
if (pageSize < 1) pageSize = 20;
|
|
|
|
return await Galleries
|
|
.OrderByDescending(p => p.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> Exists(GalleryModel? photo)
|
|
{
|
|
if (photo == null) return false;
|
|
if (string.IsNullOrEmpty(photo.Id)) return false;
|
|
return await Galleries.AnyAsync(p => p.Id == photo.Id);
|
|
}
|
|
|
|
public async Task<bool> Exists(string id)
|
|
{
|
|
return await Galleries.AnyAsync(p => p.Id == id);
|
|
}
|
|
} |