192 lines
7.8 KiB
C#
192 lines
7.8 KiB
C#
using InnovEnergy.App.Backend.DataTypes;
|
|
using InnovEnergy.App.Backend.Relations;
|
|
namespace InnovEnergy.App.Backend.Database;
|
|
|
|
|
|
public static partial class Db
|
|
{
|
|
//In this file, we provide all the methods that can be used in order to retrieve information from the database (read)
|
|
public static Folder? GetFolderById(Int64? id)
|
|
{
|
|
return Folders
|
|
.FirstOrDefault(f => f.Id == id);
|
|
}
|
|
|
|
public static Installation? GetInstallationById(Int64? id)
|
|
{
|
|
return Installations
|
|
.FirstOrDefault(i => i.Id == id);
|
|
}
|
|
|
|
public static UserAction? GetActionById(Int64? id)
|
|
{
|
|
return UserActions
|
|
.FirstOrDefault(i => i.Id == id);
|
|
}
|
|
|
|
public static User? GetUserById(Int64? id)
|
|
{
|
|
return Users
|
|
.FirstOrDefault(u => u.Id == id);
|
|
}
|
|
|
|
public static User? GetUserByEmail(String email)
|
|
{
|
|
return Users
|
|
.FirstOrDefault(u => u.Email == email);
|
|
}
|
|
|
|
public static Session? GetSession(String token)
|
|
{
|
|
//This method is called in almost every controller function.
|
|
//After logging in, the frontend receives a session object which contains a token. For all the future REST API calls, this token is used for session authentication.
|
|
var session = Sessions
|
|
.FirstOrDefault(s => s.Token == token);
|
|
|
|
if (session is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!session.Valid)
|
|
{
|
|
Delete(session);
|
|
return null;
|
|
}
|
|
|
|
return session;
|
|
}
|
|
|
|
// ── Report Queries ────────────────────────────────────────────────
|
|
|
|
public static List<WeeklyReportSummary> GetWeeklyReports(Int64 installationId)
|
|
=> WeeklyReports
|
|
.Where(r => r.InstallationId == installationId)
|
|
.OrderByDescending(r => r.PeriodStart)
|
|
.ToList();
|
|
|
|
public static List<WeeklyReportSummary> GetWeeklyReportsForMonth(Int64 installationId, Int32 year, Int32 month)
|
|
{
|
|
var monthStart = $"{year:D4}-{month:D2}-01";
|
|
var monthEnd = month == 12 ? $"{year + 1:D4}-01-01" : $"{year:D4}-{month + 1:D2}-01";
|
|
return WeeklyReports
|
|
.Where(r => r.InstallationId == installationId)
|
|
.ToList()
|
|
.Where(r => String.Compare(r.PeriodStart, monthStart, StringComparison.Ordinal) >= 0
|
|
&& String.Compare(r.PeriodStart, monthEnd, StringComparison.Ordinal) < 0)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds a cached weekly report whose period overlaps with the given date range.
|
|
/// Uses overlap logic (not exact match) because PeriodStart may be offset
|
|
/// if the first day of the week has no data.
|
|
/// </summary>
|
|
public static WeeklyReportSummary? GetWeeklyReportForWeek(Int64 installationId, String periodStart, String periodEnd)
|
|
=> WeeklyReports
|
|
.Where(r => r.InstallationId == installationId)
|
|
.ToList()
|
|
.FirstOrDefault(r => String.Compare(r.PeriodStart, periodEnd, StringComparison.Ordinal) <= 0
|
|
&& String.Compare(r.PeriodEnd, periodStart, StringComparison.Ordinal) >= 0);
|
|
|
|
public static List<MonthlyReportSummary> GetMonthlyReports(Int64 installationId)
|
|
=> MonthlyReports
|
|
.Where(r => r.InstallationId == installationId)
|
|
.OrderByDescending(r => r.Year)
|
|
.ThenByDescending(r => r.Month)
|
|
.ToList();
|
|
|
|
public static List<MonthlyReportSummary> GetMonthlyReportsForYear(Int64 installationId, Int32 year)
|
|
=> MonthlyReports
|
|
.Where(r => r.InstallationId == installationId && r.Year == year)
|
|
.ToList();
|
|
|
|
public static List<YearlyReportSummary> GetYearlyReports(Int64 installationId)
|
|
=> YearlyReports
|
|
.Where(r => r.InstallationId == installationId)
|
|
.OrderByDescending(r => r.Year)
|
|
.ToList();
|
|
|
|
// ── DailyEnergyRecord Queries ──────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Returns daily records for an installation within [from, to] inclusive, ordered by date.
|
|
/// </summary>
|
|
public static List<DailyEnergyRecord> GetDailyRecords(Int64 installationId, DateOnly from, DateOnly to)
|
|
{
|
|
var fromStr = from.ToString("yyyy-MM-dd");
|
|
var toStr = to.ToString("yyyy-MM-dd");
|
|
return Connection.Query<DailyEnergyRecord>(
|
|
"SELECT * FROM DailyEnergyRecord WHERE InstallationId = ? AND Date >= ? AND Date <= ? ORDER BY Date",
|
|
installationId, fromStr, toStr);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if a daily record already exists for this installation+date (idempotency check).
|
|
/// </summary>
|
|
public static Boolean DailyRecordExists(Int64 installationId, String date)
|
|
=> DailyRecords
|
|
.Any(r => r.InstallationId == installationId && r.Date == date);
|
|
|
|
// ── HourlyEnergyRecord Queries ─────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Returns hourly records for an installation within [from, to] inclusive, ordered by date+hour.
|
|
/// </summary>
|
|
public static List<HourlyEnergyRecord> GetHourlyRecords(Int64 installationId, DateOnly from, DateOnly to)
|
|
{
|
|
var fromStr = from.ToString("yyyy-MM-dd");
|
|
var toStr = to.ToString("yyyy-MM-dd");
|
|
return Connection.Query<HourlyEnergyRecord>(
|
|
"SELECT * FROM HourlyEnergyRecord WHERE InstallationId = ? AND Date >= ? AND Date <= ? ORDER BY Date, Hour",
|
|
installationId, fromStr, toStr);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if an hourly record already exists for this installation+dateHour (idempotency check).
|
|
/// </summary>
|
|
public static Boolean HourlyRecordExists(Int64 installationId, String dateHour)
|
|
=> HourlyRecords
|
|
.Any(r => r.InstallationId == installationId && r.DateHour == dateHour);
|
|
|
|
// ── AiInsightCache Queries ─────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Returns the cached AI insight text for (reportType, reportId, language), or null on miss.
|
|
/// </summary>
|
|
public static String? GetCachedInsight(String reportType, Int64 reportId, String language)
|
|
=> AiInsightCaches
|
|
.FirstOrDefault(c => c.ReportType == reportType
|
|
&& c.ReportId == reportId
|
|
&& c.Language == language)
|
|
?.InsightText;
|
|
|
|
// ── Ticket Queries ──────────────────────────────────────────────────
|
|
|
|
public static Ticket? GetTicketById(Int64 id)
|
|
=> Tickets.FirstOrDefault(t => t.Id == id);
|
|
|
|
public static List<Ticket> GetAllTickets()
|
|
=> Tickets.OrderByDescending(t => t.UpdatedAt).ToList();
|
|
|
|
public static List<Ticket> GetTicketsForInstallation(Int64 installationId)
|
|
=> Tickets
|
|
.Where(t => t.InstallationId == installationId)
|
|
.OrderByDescending(t => t.CreatedAt)
|
|
.ToList();
|
|
|
|
public static List<TicketComment> GetCommentsForTicket(Int64 ticketId)
|
|
=> TicketComments
|
|
.Where(c => c.TicketId == ticketId)
|
|
.OrderBy(c => c.CreatedAt)
|
|
.ToList();
|
|
|
|
public static TicketAiDiagnosis? GetDiagnosisForTicket(Int64 ticketId)
|
|
=> TicketAiDiagnoses.FirstOrDefault(d => d.TicketId == ticketId);
|
|
|
|
public static List<TicketTimelineEvent> GetTimelineForTicket(Int64 ticketId)
|
|
=> TicketTimelineEvents
|
|
.Where(e => e.TicketId == ticketId)
|
|
.OrderBy(e => e.CreatedAt)
|
|
.ToList();
|
|
} |