From c7b398374e03272f8d6d5350ec5ae355a045a37c Mon Sep 17 00:00:00 2001 From: misya Date: Mon, 21 Apr 2025 16:13:49 +0800 Subject: [PATCH] generate location, longitude & latitude, and fixed format of date/time generated --- Areas/MMS/Controllers/MarineController.cs | 81 +++++++++++++-------- Areas/MMS/Models/PDFGenerator/TarBallPDF.cs | 26 ++++--- DBContext/MMSSystemContext.cs | 27 +++++-- Models/MarineTarball.cs | 17 ++++- appsettings.json | 2 +- 5 files changed, 100 insertions(+), 53 deletions(-) diff --git a/Areas/MMS/Controllers/MarineController.cs b/Areas/MMS/Controllers/MarineController.cs index e009836..7000cbe 100644 --- a/Areas/MMS/Controllers/MarineController.cs +++ b/Areas/MMS/Controllers/MarineController.cs @@ -7,6 +7,7 @@ using PSTW_CentralSystem.Areas.MMS; using System.Linq; using QuestPDF.Fluent; using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator; +using System.Globalization; namespace PSTW_CentralSystem.Areas.MMS.Controllers { @@ -21,13 +22,15 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine } + //what to require from database into front-end (here is logic for calling, etc.) public IActionResult TarBallForm() { var marineTarballs = _context.MarineTarballs + .Where(t=>t.StationID != "1") //Exclude test data with station named "1" .Select(t => new { t.Id, // Include Id property - Date = t.DateSample, + Date = t.DateSample.ToString("yyyy/MM/dd"), // Format DateSample as needed Station = t.StationID }) .ToList(); @@ -42,24 +45,29 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return View(marineTarballs); } + //what to do for generating report public IActionResult GenerateReport(int id) { - Console.WriteLine($"Requested ID in GenerateReport: {id}"); // Log the ID + Console.WriteLine($"Requested ID in GenerateReport: {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 + join station in _context.MarineStations on marine.StationID equals station.StationID join state in _context.States on station.StateID equals state.StateID where marine.Id == id select new { + state.StateName, marine.StationID, + station.LocationName, + marine.Longitude, + marine.Latitude, marine.DateSample, - state.StateName // Get the full state name + marine.TimeSample + }).FirstOrDefault(); if (tarballData == null) @@ -67,47 +75,53 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return NotFound("The specified record was not found."); } - Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}"); + Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}"); - // Generate the PDF - var pdfdocument = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator - var pdf = pdfdocument.GeneratePdf(); + var pdfDocument = new TarBallPDF( + tarballData.StateName, + tarballData.StationID, + tarballData.LocationName, + tarballData.Longitude, + tarballData.Latitude, + tarballData.DateSample, // Pass DateSample directly (raw value) + tarballData.TimeSample // Pass TimeSample directly (raw value) + ); + var pdf = pdfDocument.GeneratePdf(); + Console.WriteLine("PDF generation completed."); - // Construct the filename - var formattedDate = tarballData.DateSample.ToString("ddMMyyyy"); + var formattedDate = tarballData.DateSample.ToString("yyyyMMdd"); 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); + Console.WriteLine($"Error in GenerateReport: {ex.Message}"); + return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while generating the PDF. Generate PDF error. " + ex.Message); } } public IActionResult ViewPDF(int id) { - Console.WriteLine("testing"); //TESTING DEBUGGING - Console.WriteLine($"Requested ID in ViewReport: {id}"); // Log the ID + Console.WriteLine($"Requested ID in ViewReport: {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 station in _context.MarineStations + on marine.StationID equals station.StationID join state in _context.States on station.StateID equals state.StateID where marine.Id == id select new { + state.StateName, marine.StationID, + station.LocationName, + marine.Longitude, + marine.Latitude, marine.DateSample, - StationExists = station != null, - state.StateName // Get the full state name + marine.TimeSample }).FirstOrDefault(); if (tarballData == null) @@ -115,22 +129,27 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return NotFound("The specified record was not found."); } - Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}"); + Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}"); + + // Generate the PDF - Make sure parameter order matches the constructor + var document = new TarBallPDF( + tarballData.StateName, + tarballData.StationID, + tarballData.LocationName, + tarballData.Longitude, + tarballData.Latitude, + tarballData.DateSample, + tarballData.TimeSample + ); - // 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 the file without forcing download return File(pdf, "application/pdf"); } catch (Exception ex) { - Console.WriteLine(ex.Message); + Console.WriteLine($"Error in ViewPDF: {ex.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message); } } diff --git a/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs b/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs index 6ef8747..767a2de 100644 --- a/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs +++ b/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs @@ -1,20 +1,21 @@ using QuestPDF.Fluent; using QuestPDF.Infrastructure; using QuestPDF.Helpers; +using Google.Protobuf.WellKnownTypes; +using PSTW_CentralSystem.Models; namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator { - public class TarBallPDF : IDocument + public class TarBallPDF(string stationID, string stateName, string locationName, string longitude, string latitude, DateTime dateSample, TimeSpan timeSample) : IDocument { - private readonly string _stationId; - private readonly string _stateName; + private readonly string _stationId = stationID; + private readonly string _stateName = stateName; + private readonly string _locationName = locationName; + private readonly string _longitude = longitude; + private readonly string _latitude = latitude; + private readonly DateTime _dateSample = dateSample; + private readonly TimeSpan _timeSample = timeSample; - // Constructor - public TarBallPDF(string stationId, string stateName) - { - _stationId = stationId; - _stateName = stateName; - } // Metadata for the PDF document public DocumentMetadata GetMetadata() => new() @@ -117,17 +118,18 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator table.Cell() .Background(Colors.Grey.Lighten2).Element(CellStyle).Text("LOCATION") .Bold(); - table.Cell().Element(CellStyle).Text(""); + table.Cell().Element(CellStyle).Text(_locationName); table.Cell() .Background(Colors.Grey.Lighten2).Element(CellStyle).Text("TARBALL SURVEY LOCATION LONGITUDE & LATITUDE") .Bold(); - table.Cell().Element(CellStyle).Text(""); + table.Cell().Element(CellStyle).Text($"{_longitude}, {_latitude}"); + // Display DateSample and TimeSample together in the table table.Cell() .Background(Colors.Grey.Lighten2).Element(CellStyle).Text("DATE / TIME") .Bold(); - table.Cell().Element(CellStyle).Text(""); + table.Cell().Element(CellStyle).Text($"{_dateSample:yyyy-MM-dd} {_timeSample:hh\\:mm\\:ss}"); }); column.Spacing(3); diff --git a/DBContext/MMSSystemContext.cs b/DBContext/MMSSystemContext.cs index d328fd6..a2e6256 100644 --- a/DBContext/MMSSystemContext.cs +++ b/DBContext/MMSSystemContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; using PSTW_CentralSystem.Models; // Add this to use the MarineTarball class namespace PSTW_CentralSystem.DBContext @@ -11,9 +12,9 @@ namespace PSTW_CentralSystem.DBContext // DbSet for tbl_marine_tarball public DbSet MarineTarballs { get; set; } - public DbSet TarballStations { get; set; } + public DbSet MarineStations { get; set; } public DbSet States { get; set; } - + public DbSet ClassifyID { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -41,16 +42,22 @@ namespace PSTW_CentralSystem.DBContext entity.Property(e => e.Timestamp).HasColumnName("timestamp"); //Configure relationship with TarballStation - entity.HasOne(m => m.TarballStation) + entity.HasOne(m => m.MarineStation) .WithMany() .HasForeignKey(m => m.StationID) .HasPrincipalKey(t => t.StationID); + + //Configure relationship with TarballStation + entity.HasOne(m => m.MarineClassify) + .WithMany() + .HasForeignKey(m => m.ClassifyID) + .HasPrincipalKey(t => t.ClassifyID); }); // Map TarballStation to tbl_tarball_station - modelBuilder.Entity().ToTable("tbl_tarball_station"); + modelBuilder.Entity().ToTable("tbl_marine_station"); - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { entity.HasKey(e => e.Id); // Primary key entity.Property(e => e.Id).HasColumnName("id"); @@ -79,6 +86,16 @@ namespace PSTW_CentralSystem.DBContext entity.Property(e => e.StateName).HasColumnName("stateName").HasMaxLength(50); }); + modelBuilder.Entity().ToTable("tbl_classify"); + + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.Id); // Primary key + entity.Property(e => e.Id).HasColumnName("id"); + entity.Property(e => e.ClassifyID).HasColumnName("classifyID").HasMaxLength(2); + entity.Property(e => e.ClassifyName).HasColumnName("classifyName").HasMaxLength(50); + }); + } } } diff --git a/Models/MarineTarball.cs b/Models/MarineTarball.cs index af318e6..404ed9f 100644 --- a/Models/MarineTarball.cs +++ b/Models/MarineTarball.cs @@ -12,17 +12,20 @@ namespace PSTW_CentralSystem.Models public TimeSpan TimeSample { get; set; } // Maps to 'timeSample' public required string StationID { get; set; } // Maps to 'stationID' public required string ClassifyID { get; set; } // Maps to 'classifyID' - public double Latitude { get; set; } // Maps to 'latitude' - public double Longitude { get; set; } // Maps to 'longitude' + public required string Latitude { get; set; } // Maps to 'latitude' + public required string Longitude { get; set; } // Maps to 'longitude' public double GetLatitude { get; set; } // Maps to 'getLatitude' public double GetLongitude { get; set; } // Maps to 'getLongitude' public DateTime Timestamp { get; set; } // Maps to 'timestamp' [ForeignKey("StationID")] - public required TarballStation TarballStation { get; set; } + public required MarineStation MarineStation { get; set; } + + [ForeignKey("ClassifyID")] + public required MarineClassify MarineClassify { get; set; } } - public class TarballStation + public class MarineStation { public int Id { get; set; } // Maps to 'id' public required string StationID { get; set; } // Maps to 'stationID' @@ -44,5 +47,11 @@ namespace PSTW_CentralSystem.Models public required string StateName { get; set; } // Maps to 'stateName' } + public class MarineClassify + { + public int Id { get; set; } // Maps to 'id' + public required string ClassifyID{ get; set; } // Maps to classification ID + public required string ClassifyName { get; set; } // Maps to classification name + } } diff --git a/appsettings.json b/appsettings.json index 6802751..81b3254 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,7 +6,7 @@ "CentralConnnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs;", //DB_dev Public connection //"InventoryConnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs_inventory;" //DB_dev connection //"DefaultConnection": "Server=219.92.7.60;Port=3307;uid=intern;password='intern_mysql_acct';database=web_interface;"//DB_dev connection - "MMSDatabase": "Server=192.168.12.42;Port=3306;Uid=mmsuser;password=mms@pstw_mysql_root;database=db_mms;" + "MMSDatabase": "Server=192.168.12.42;Port=3306;Uid=mmsuser;password=mms@pstw_mysql_root;database=db_mms;ConvertZeroDateTime=True;" }, "Logging": { "LogLevel": {