PSTW_CentralizeSystem/Areas/MMS/Controllers/MarineController.cs

139 lines
5.7 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models;
using PSTW_CentralSystem.Areas.MMS;
using System.Linq;
using QuestPDF.Fluent;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
namespace PSTW_CentralSystem.Areas.MMS.Controllers
{
[Area("MMS")]
//[Authorize(Policy = "RoleModulePolicy")]
public class MarineController(MMSSystemContext context) : Controller
{
private readonly MMSSystemContext _context = context;
public IActionResult Index()
{
return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine
}
public IActionResult TarBallForm()
{
var marineTarballs = _context.MarineTarballs
.Select(t => new
{
t.Id, // Include Id property
Date = t.DateSample,
Station = t.StationID
})
.ToList();
// For debugging
Console.WriteLine("Fetched Data:");
foreach (var item in marineTarballs)
{
Console.WriteLine($"Date: {item.Date}, Station: {item.Station}");
}
return View(marineTarballs);
}
public IActionResult GenerateReport(int id)
{
Console.WriteLine($"Requested ID in GenerateReport: {id}"); // Log the ID
try
{
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
on marine.StationID equals station.StationID
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
marine.StationID,
marine.DateSample,
state.StateName // Get the full state name
}).FirstOrDefault();
if (tarballData == null)
{
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
// Generate the PDF
var pdfdocument = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = pdfdocument.GeneratePdf();
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf";
// Return the file
return File(pdf, "application/pdf", fileName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while generating the PDF. " + ex.Message);
}
}
public IActionResult ViewPDF(int id)
{
Console.WriteLine("testing"); //TESTING DEBUGGING
Console.WriteLine($"Requested ID in ViewReport: {id}"); // Log the ID
try
{
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
on marine.StationID equals station.StationID into StationGroup
from station in StationGroup.DefaultIfEmpty() //Handle missing
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
marine.StationID,
marine.DateSample,
StationExists = station != null,
state.StateName // Get the full state name
}).FirstOrDefault();
if (tarballData == null)
{
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
// Generate the PDF
var document = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = document.GeneratePdf();
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf";
// Return the file
return File(pdf, "application/pdf");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message);
}
}
}
}