70 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using ReportingServices.Shared.HelperClasses;
using ReportingServices.Shared.Models.ProductionReport;
using ReportingServices.Shared.ViewModels.ProductionReport;
namespace ReportingServices.UI.Controllers
{
public class ProductionReportController : Controller
{
private readonly ILogger<ProductionReportController> _logger;
private readonly int _reportIndex = (int)DateTime.Now.DayOfWeek;
private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json";
private readonly string _toolStateOwnerFilePath = "wwwroot/Assets/ToolStates.json";
public ProductionReportController(ILogger<ProductionReportController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult DailyReport()
{
try
{
DailyReport dailyReport = DailyReportHelper.SetUpDailyReport().Result;
Dictionary<string, List<string>> toolStateOwners = JsonFileHandler.LoadJSONFile<Dictionary<string, List<string>>>(_toolStateOwnerFilePath);
dailyReport.ToolStatesByOwner = toolStateOwners;
return View(dailyReport);
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Failed to load report");
return View();
}
}
public IActionResult EditDailyReport()
{
Dictionary<string, List<ManualReportEntries>> entries = JsonFileHandler.LoadJSONFile<Dictionary<string, List<ManualReportEntries>>>(_dailyRptFilePath);
ManualReportEntries rpt = entries["Current Week"][_reportIndex];
return View(rpt);
}
[HttpPost]
public IActionResult EditDailyReport(ManualReportEntries rpt)
{
Dictionary<string, List<ManualReportEntries>> report = JsonFileHandler.LoadJSONFile<Dictionary<string, List<ManualReportEntries>>>(_dailyRptFilePath);
rpt.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
rpt.Day = DateTime.Now.DayOfWeek;
report["Current Week"][_reportIndex] = rpt;
JsonFileHandler.SaveJSONFile(report, _dailyRptFilePath);
return RedirectToAction("DailyReport");
}
}
}