using MySqlConnector; using UtopiaCanteenSystem.Models; namespace UtopiaCanteenSystem.Services; /// /// Fetches lunch menu from HRMS: lunch_menu_week (by location_site_id) → lunch_menu_item → menu_item. /// Uses IConfigService.GetHrmsLookupConnectionString(). Only current week is considered. /// public class MenuLookupService : IMenuLookupService { private readonly IConfigService _configService; public MenuLookupService(IConfigService configService) { _configService = configService; } /// public async Task> GetMenuItemsForSiteAsync(int siteIdNumeric, CancellationToken cancellationToken = default) { var connectionString = _configService.GetHrmsLookupConnectionString(); if (string.IsNullOrWhiteSpace(connectionString)) return Array.Empty(); const string sql = @" SELECT DISTINCT mi.id, mi.item_name, mi.item_type, mi.price FROM lunch_menu_week w JOIN lunch_menu_item li ON li.lunch_menu_week_id = w.id JOIN menu_item mi ON mi.id = li.menu_item_id WHERE w.location_site_id = @siteId AND CURDATE() BETWEEN w.week_start_date AND w.week_end_date ORDER BY mi.item_type, mi.item_name"; await using var conn = new MySqlConnection(connectionString); await conn.OpenAsync(cancellationToken).ConfigureAwait(false); await using var cmd = new MySqlCommand(sql, conn); cmd.Parameters.AddWithValue("@siteId", siteIdNumeric); var list = new List(); await using var reader = await cmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) { list.Add(new HrmsMenuItem { MenuItemId = reader.IsDBNull(0) ? 0 : reader.GetInt32(0), ItemName = GetString(reader, 1), ItemType = GetString(reader, 2), Price = GetDecimal(reader, 3) }); } return list; } private static string GetString(MySqlDataReader reader, int ordinal) { if (reader.IsDBNull(ordinal)) return string.Empty; var v = reader.GetValue(ordinal); return v?.ToString() ?? string.Empty; } private static decimal GetDecimal(MySqlDataReader reader, int ordinal) { if (reader.IsDBNull(ordinal)) return 0m; var v = reader.GetValue(ordinal); return v is decimal d ? d : Convert.ToDecimal(v); } }