Introduced a new TimeController with a POST endpoint to insert and retrieve the latest time record. Added ITimeRepository, TimeRepository, and TimeRecord entity. Implemented MediatR command and handler for time insertion. Updated ApplicationDbContext and DI configuration to support the new feature.
29 lines
806 B
C#
29 lines
806 B
C#
using DbFirst.Application.Repositories;
|
|
using DbFirst.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DbFirst.Infrastructure.Repositories;
|
|
|
|
public class TimeRepository : ITimeRepository
|
|
{
|
|
private readonly ApplicationDbContext _db;
|
|
|
|
public TimeRepository(ApplicationDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public async Task InsertAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
await _db.Database.ExecuteSqlRawAsync("INSERT INTO [TIME] (NOW) VALUES (GETDATE())", cancellationToken);
|
|
}
|
|
|
|
public async Task<TimeRecord?> GetLastAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return await _db.Times
|
|
.AsNoTracking()
|
|
.OrderByDescending(t => t.Now)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
}
|