123 lines
4.0 KiB
C#
123 lines
4.0 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;
|
|
|
|
namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
|
{
|
|
[Area("MMS")]
|
|
|
|
//[Authorize(Policy = "RoleModulePolicy")]
|
|
public class MarineController : Controller
|
|
{
|
|
private readonly MMSSystemContext _context;
|
|
public MarineController(MMSSystemContext context)
|
|
{
|
|
_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)
|
|
{
|
|
try
|
|
{
|
|
// Retrieve the specific record based on the id
|
|
var tarballData = _context.MarineTarballs
|
|
.Where(t => t.Id == id)
|
|
.Select(t => new
|
|
{
|
|
t.StationID,
|
|
t.DateSample
|
|
})
|
|
.FirstOrDefault();
|
|
|
|
if (tarballData == null)
|
|
{
|
|
return NotFound("The specified record was not found.");
|
|
}
|
|
|
|
// Generate the PDF
|
|
var document = new TarBallPDF(); // Customize the report if needed
|
|
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", 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)
|
|
{
|
|
try
|
|
{
|
|
// Retrieve the specific record based on the id
|
|
var tarballData = _context.MarineTarballs
|
|
.Where(t => t.Id == id)
|
|
.Select(t => new
|
|
{
|
|
t.StationID,
|
|
t.DateSample
|
|
})
|
|
.FirstOrDefault();
|
|
|
|
if (tarballData == null)
|
|
{
|
|
return NotFound("The specified record was not found.");
|
|
}
|
|
|
|
// Generate the PDF
|
|
var document = new TarBallPDF(); // Customize the PDF if needed
|
|
var pdf = document.GeneratePdf();
|
|
|
|
//For 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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|