44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using Koogle.Domain.Interfaces;
|
|
using KoogleApp.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace KoogleApp.Data.Repository
|
|
{
|
|
public class DayRepository(IDbContextFactory<AppDbContext> contextFactory, ILogger<IDayRepository> logger)
|
|
: IDayRepository
|
|
{
|
|
private readonly ILogger<IDayRepository> _logger = logger;
|
|
|
|
public async Task<List<Day>> GetAllAsync(int year)
|
|
{
|
|
await using var context = await contextFactory.CreateDbContextAsync();
|
|
var res = await context.Days.Where(_ => _.PostDate >= new DateTime(year, 1, 1) && _.PostDate <= new DateTime(year, 12, 31)).ToListAsync();
|
|
return res;
|
|
}
|
|
|
|
public async Task<Day?> GetActiveDayAsync()
|
|
{
|
|
await using var context = await contextFactory.CreateDbContextAsync();
|
|
var res = await context.Days.FirstOrDefaultAsync(_ => _.PostDate == DateTime.Today);
|
|
return res;
|
|
}
|
|
|
|
public async Task<Day> AddAsync(Day day)
|
|
{
|
|
await using var context = await contextFactory.CreateDbContextAsync();
|
|
var res = await context.Days.AddAsync(day);
|
|
await context.SaveChangesAsync();
|
|
return day;
|
|
}
|
|
|
|
public async Task<Day> UpdateAsync(Day day)
|
|
{
|
|
await using var context = await contextFactory.CreateDbContextAsync();
|
|
var res = context.Days.Update(day);
|
|
await context.SaveChangesAsync();
|
|
return res.Entity;
|
|
}
|
|
}
|
|
}
|