194 lines
8.0 KiB
C#
194 lines
8.0 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Aberwyn.Models;
|
|
|
|
namespace Aberwyn.Data
|
|
{
|
|
public class MenuService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IHostEnvironment _env;
|
|
|
|
public MenuService(IConfiguration configuration, IHostEnvironment env)
|
|
{
|
|
_configuration = configuration;
|
|
_env = env;
|
|
}
|
|
|
|
private MySqlConnection GetConnection()
|
|
{
|
|
var connectionString = _env.IsDevelopment()
|
|
? _configuration.GetConnectionString("DefaultConnection")
|
|
: _configuration.GetConnectionString("ProductionConnection");
|
|
|
|
return new MySqlConnection(connectionString);
|
|
}
|
|
|
|
public List<WeeklyMenu> GetWeeklyMenu(int weekNumber, int year)
|
|
{
|
|
var weeklyMenu = new List<WeeklyMenu>();
|
|
using (var connection = GetConnection())
|
|
{
|
|
connection.Open();
|
|
string query = @"
|
|
SELECT wm.Id, wm.DayOfWeek, wm.DinnerMealId, wm.LunchMealId, wm.WeekNumber, wm.Year,
|
|
dm.Name AS DinnerMealName, lm.Name AS LunchMealName
|
|
FROM WeeklyMenu wm
|
|
LEFT JOIN Meals dm ON wm.DinnerMealId = dm.Id
|
|
LEFT JOIN Meals lm ON wm.LunchMealId = lm.Id
|
|
WHERE wm.WeekNumber = @weekNumber AND wm.Year = @year";
|
|
|
|
using (var cmd = new MySqlCommand(query, connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@weekNumber", weekNumber);
|
|
cmd.Parameters.AddWithValue("@year", year);
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
weeklyMenu.Add(new WeeklyMenu
|
|
{
|
|
Id = reader.GetInt32("Id"),
|
|
DayOfWeek = reader.GetInt32("DayOfWeek"),
|
|
DinnerMealId = reader.IsDBNull(reader.GetOrdinal("DinnerMealId")) ? (int?)null : reader.GetInt32("DinnerMealId"),
|
|
LunchMealId = reader.IsDBNull(reader.GetOrdinal("LunchMealId")) ? (int?)null : reader.GetInt32("LunchMealId"),
|
|
WeekNumber = reader.GetInt32("WeekNumber"),
|
|
Year = reader.GetInt32("Year"),
|
|
DinnerMealName = reader.IsDBNull(reader.GetOrdinal("DinnerMealName")) ? null : reader.GetString("DinnerMealName"),
|
|
LunchMealName = reader.IsDBNull(reader.GetOrdinal("LunchMealName")) ? null : reader.GetString("LunchMealName"),
|
|
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return weeklyMenu;
|
|
}
|
|
|
|
public List<Meal> GetMeals()
|
|
{
|
|
var meals = new List<Meal>();
|
|
using (var connection = GetConnection())
|
|
{
|
|
connection.Open();
|
|
string query = "SELECT Id, Name FROM Meals";
|
|
using (var cmd = new MySqlCommand(query, connection))
|
|
{
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
meals.Add(new Meal
|
|
{
|
|
Id = reader.GetInt32("Id"),
|
|
Name = reader.GetString("Name")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return meals;
|
|
}
|
|
|
|
public void UpdateWeeklyMenu(MenuViewModel menuData)
|
|
{
|
|
if (menuData == null || menuData.WeeklyMenus == null)
|
|
throw new ArgumentNullException(nameof(menuData), "Menu data or weekly menus cannot be null.");
|
|
|
|
using (var connection = GetConnection())
|
|
{
|
|
connection.Open();
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
// Loop over each WeeklyMenu in the WeeklyMenus list
|
|
foreach (var weeklyMenu in menuData.WeeklyMenus)
|
|
{
|
|
int dayOfWeek = weeklyMenu.DayOfWeek;
|
|
var menu = weeklyMenu;
|
|
|
|
// SQL query to update the existing records
|
|
string query = @"
|
|
UPDATE WeeklyMenu
|
|
SET
|
|
DinnerMealId = @dinnerMealId,
|
|
LunchMealId = @lunchMealId,
|
|
Cook = @cook
|
|
WHERE DayOfWeek = @dayOfWeek
|
|
AND WeekNumber = @weekNumber
|
|
AND Year = @year;";
|
|
|
|
using (var cmd = new MySqlCommand(query, connection, transaction))
|
|
{
|
|
cmd.Parameters.AddWithValue("@dayOfWeek", dayOfWeek);
|
|
cmd.Parameters.AddWithValue("@dinnerMealId", menu.DinnerMealId ?? (object)DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@lunchMealId", menu.LunchMealId ?? (object)DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@weekNumber", menu.WeekNumber);
|
|
cmd.Parameters.AddWithValue("@year", menu.Year);
|
|
cmd.Parameters.AddWithValue("@cook", menu.Cook ?? (object)DBNull.Value);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
Console.WriteLine($"Error updating weekly menu: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public int AddMeal(Meal meal)
|
|
{
|
|
using (var connection = GetConnection())
|
|
{
|
|
connection.Open();
|
|
string query = @"
|
|
INSERT INTO Meals (Name, Description, ProteinType, CarbType, RecipeUrl, CreatedAt)
|
|
VALUES (@name, @description, @proteinType, @carbType, @recipeUrl, NOW());
|
|
SELECT LAST_INSERT_ID();";
|
|
|
|
using (var cmd = new MySqlCommand(query, connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@name", meal.Name);
|
|
cmd.Parameters.AddWithValue("@description", string.IsNullOrWhiteSpace(meal.Description) ? (object)DBNull.Value : meal.Description);
|
|
cmd.Parameters.AddWithValue("@proteinType", string.IsNullOrWhiteSpace(meal.ProteinType) ? (object)DBNull.Value : meal.ProteinType);
|
|
cmd.Parameters.AddWithValue("@carbType", string.IsNullOrWhiteSpace(meal.CarbType) ? (object)DBNull.Value : meal.CarbType);
|
|
cmd.Parameters.AddWithValue("@recipeUrl", string.IsNullOrWhiteSpace(meal.RecipeUrl) ? (object)DBNull.Value : meal.RecipeUrl);
|
|
|
|
var result = cmd.ExecuteScalar();
|
|
return result != null ? Convert.ToInt32(result) : 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private int GetDayOfWeek(string day)
|
|
{
|
|
return day switch
|
|
{
|
|
"Monday" => 1,
|
|
"Tuesday" => 2,
|
|
"Wednesday" => 3,
|
|
"Thursday" => 4,
|
|
"Friday" => 5,
|
|
"Saturday" => 6,
|
|
"Sunday" => 7,
|
|
_ => throw new System.ArgumentException("Invalid day name")
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
// Models for Meals and WeeklyMenu
|