using System; using System.Linq; using System.Threading.Tasks; using UtopiaCanteenSystem.Data; using UtopiaCanteenSystem.Models; namespace UtopiaCanteenSystem.Services; /// /// Persists simple admin login audit records to the local SQLite database. /// public class AdminAuditService : IAdminAuditService { private readonly DbContextFactory _dbFactory; public AdminAuditService(DbContextFactory dbFactory) { _dbFactory = dbFactory; } public async Task RecordLoginAsync(string username, string employeeId) { try { await using var db = _dbFactory.CreateDbContext(); var record = new AdminLoginRecord { Username = username ?? string.Empty, EmployeeId = employeeId ?? string.Empty, LoginTimeUtc = DateTime.UtcNow }; db.AdminLoginRecords.Add(record); await db.SaveChangesAsync().ConfigureAwait(false); } catch { // Audit failures should never block login; ignore errors. } } /// /// Returns the most recent admin login record for this kiosk, or null if none exist. /// public AdminLoginRecord? GetLastLogin() { try { using var db = _dbFactory.CreateDbContext(); return db.AdminLoginRecords .OrderByDescending(x => x.LoginTimeUtc) .FirstOrDefault(); } catch { return null; } } }