Files
Aberwyn/Aberwyn/Controllers/HomeController.cs
Elias Jansson 1a3418cf37 Torrent v1 done
2026-01-24 16:52:56 +01:00

96 lines
3.2 KiB
C#

using Aberwyn.Data;
using Aberwyn.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace Aberwyn.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
//private readonly BudgetService _budgetService;
private readonly MenuService _menuService;
private readonly ApplicationDbContext _context;
private readonly TorrentService _torrentService;
// Constructor to inject dependencies
public HomeController(ApplicationDbContext applicationDbContext, ILogger<HomeController> logger, MenuService menuService, TorrentService torrentService)
{
_logger = logger;
_menuService = menuService;
_context = applicationDbContext;
_torrentService = torrentService;
}
public async Task<IActionResult> Index()
{
var isOpen = _context.AppSettings.FirstOrDefault(x => x.Key == "RestaurantIsOpen")?.Value == "True";
ViewBag.RestaurantIsOpen = isOpen;
var now = DateTime.Now;
var showDate = now.Hour >= 20 ? now.Date.AddDays(1) : now.Date;
var todaysMenu = _menuService.GetMenuForDate(showDate);
var userId = User.Identity?.Name ?? "guest";
// Awaita async-metoden
var newCount = await _torrentService.GetUnseenTorrentCountAsync(userId);
ViewBag.NewTorrentCount = newCount;
ViewBag.ShowDate = showDate;
return View(todaysMenu);
}
public IActionResult Privacy()
{
return View();
}
public IActionResult RealEstate()
{
return View();
}
public IActionResult Menu()
{
ViewData["HideSidebar"] = true;
// Get the current date and week
var currentDate = DateTime.Now;
var currentYear = currentDate.Year;
var calendar = new GregorianCalendar();
var currentWeek = calendar.GetWeekOfYear(currentDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
// Fetch weekly menu items for the current week and year
var weeklyMenus = _menuService.GetWeeklyMenu(currentWeek, currentYear);
var meals = _menuService.GetMeals(); // Fetch all meals
var model = new MenuViewModel
{
WeeklyMenus = weeklyMenus, // List of WeeklyMenu items
Meals = meals, // List of available meals
WeekNumber = currentWeek, // Dynamically set the current week number
Year = currentYear // Dynamically set the current year
};
return View(model);
}
public IActionResult Budget()
{
ViewData["HideSidebar"] = true;
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}