From 58434ccc1d9d2fcdab8b69db04b6d44572302898 Mon Sep 17 00:00:00 2001 From: misya Date: Wed, 16 Apr 2025 11:43:52 +0800 Subject: [PATCH] connect tarball to db_mms. improve sorting, filtering and search --- Areas/MMS/Controllers/MarineController.cs | 81 ++++++++++++++++++++--- Areas/MMS/Views/Marine/TarBallForm.cshtml | 71 ++++++-------------- DBContext/MMSSystemContext.cs | 42 ++++++++++++ Models/MarineTarball.cs | 21 ++++++ PSTW_CentralSystem.csproj | 1 + Program.cs | 12 ++++ appsettings.json | 3 +- 7 files changed, 171 insertions(+), 60 deletions(-) create mode 100644 DBContext/MMSSystemContext.cs create mode 100644 Models/MarineTarball.cs diff --git a/Areas/MMS/Controllers/MarineController.cs b/Areas/MMS/Controllers/MarineController.cs index c3fe67f..a2fcafc 100644 --- a/Areas/MMS/Controllers/MarineController.cs +++ b/Areas/MMS/Controllers/MarineController.cs @@ -1,7 +1,10 @@ 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 @@ -11,24 +14,64 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers //[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(); - return View(); + // 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() + public IActionResult GenerateReport(int id) { try { - // Retrieve specific data based on the id, if required - var document = new TarBallPDF(); // Use id to customize the report if needed + // 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(); - var fileName = $"TarBallReport_{DateTime.Now:yyyyMMdd_HHmmss}.pdf"; + + // 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) @@ -38,13 +81,34 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers } } - public IActionResult ViewPDF() + public IActionResult ViewPDF(int id) { try { - // Retrieve specific data based on the id, if required - var document = new TarBallPDF(); // Use id to customize the PDF if needed + // 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) @@ -53,5 +117,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message); } } + } } diff --git a/Areas/MMS/Views/Marine/TarBallForm.cshtml b/Areas/MMS/Views/Marine/TarBallForm.cshtml index ecf3353..cfd611d 100644 --- a/Areas/MMS/Views/Marine/TarBallForm.cshtml +++ b/Areas/MMS/Views/Marine/TarBallForm.cshtml @@ -57,7 +57,6 @@ color: #333; /* Dark grey for active state */ font-weight: bold; /* Optional: Make it bold for emphasis */ } - @@ -76,7 +75,7 @@
- +
@@ -85,34 +84,32 @@ No. - + Date - + Station - Approval Status + Status PDF - - - {{ data.no }} - {{ data.date }} + + {{ index + 1 }} + {{ new Date(data.date).toLocaleDateString('en-GB') }} {{ data.station }} - - View PDF - Download PDF + View PDF + Download PDF @@ -122,44 +119,31 @@ -@section Scripts{ - +@section Scripts { - -} \ No newline at end of file +} diff --git a/DBContext/MMSSystemContext.cs b/DBContext/MMSSystemContext.cs new file mode 100644 index 0000000..e751051 --- /dev/null +++ b/DBContext/MMSSystemContext.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using PSTW_CentralSystem.Models; // Add this to use the MarineTarball class + +namespace PSTW_CentralSystem.DBContext +{ + public class MMSSystemContext : DbContext + { + public MMSSystemContext(DbContextOptions options) : base(options) + { + } + + // DbSet for tbl_marine_tarball + public DbSet MarineTarballs { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + // Map MarineTarball to tbl_marine_tarball + modelBuilder.Entity().ToTable("tbl_marine_tarball"); + + // Configure properties if needed + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.Id); // Primary key + entity.Property(e => e.Id).HasColumnName("id"); + entity.Property(e => e.ReportID).HasColumnName("reportID").HasMaxLength(50); + entity.Property(e => e.FirstSampler).HasColumnName("firstSampler").HasMaxLength(50); + entity.Property(e => e.SecondSampler).HasColumnName("secondSampler").HasMaxLength(50); + entity.Property(e => e.DateSample).HasColumnName("dateSample"); + entity.Property(e => e.TimeSample).HasColumnName("timeSample"); + entity.Property(e => e.StationID).HasColumnName("stationID").HasMaxLength(20); + entity.Property(e => e.ClassifyID).HasColumnName("classifyID").HasMaxLength(20); + entity.Property(e => e.Latitude).HasColumnName("latitude"); + entity.Property(e => e.Longitude).HasColumnName("longitude"); + entity.Property(e => e.GetLatitude).HasColumnName("getLatitude"); + entity.Property(e => e.GetLongitude).HasColumnName("getLongitude"); + entity.Property(e => e.Timestamp).HasColumnName("timestamp"); + }); + } + } +} diff --git a/Models/MarineTarball.cs b/Models/MarineTarball.cs new file mode 100644 index 0000000..9f7322c --- /dev/null +++ b/Models/MarineTarball.cs @@ -0,0 +1,21 @@ +using System; + +namespace PSTW_CentralSystem.Models +{ + public class MarineTarball + { + public int Id { get; set; } // Maps to 'id' + public string ReportID { get; set; } // Maps to 'reportID' + public string FirstSampler { get; set; } // Maps to 'firstSampler' + public string SecondSampler { get; set; } // Maps to 'secondSampler' + public DateTime DateSample { get; set; } // Maps to 'dateSample' + public TimeSpan TimeSample { get; set; } // Maps to 'timeSample' + public string StationID { get; set; } // Maps to 'stationID' + public string ClassifyID { get; set; } // Maps to 'classifyID' + public double Latitude { get; set; } // Maps to 'latitude' + public double 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' + } +} diff --git a/PSTW_CentralSystem.csproj b/PSTW_CentralSystem.csproj index 85e3d6f..da776fe 100644 --- a/PSTW_CentralSystem.csproj +++ b/PSTW_CentralSystem.csproj @@ -21,6 +21,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Program.cs b/Program.cs index 3adf3ba..546d518 100644 --- a/Program.cs +++ b/Program.cs @@ -50,6 +50,12 @@ internal class Program mysqlOptions => mysqlOptions.CommandTimeout(120) ); }); + + builder.Services.AddDbContext(options => + options.UseMySql(builder.Configuration.GetConnectionString("MMSDatabase"), + new MySqlServerVersion(new Version(8, 0, 0)))); + + //builder.Services.AddDbContext(options => //{ // options.UseMySql(inventoryConnectionString, new MySqlServerVersion(new Version(8, 0, 39)), @@ -95,6 +101,12 @@ internal class Program app.MapControllerRoute( name: "root", pattern: "{controller=Home}/{action=Index}/{id?}"); + app.MapControllerRoute( + name: "areas", + pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); + app.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); } diff --git a/appsettings.json b/appsettings.json index 5b9735b..6802751 100644 --- a/appsettings.json +++ b/appsettings.json @@ -3,9 +3,10 @@ //"DefaultConnection": "Server=localhost;uid=root;Password='';Database=web_interface;" //"DefaultConnection": "server=175.136.244.102;user id=root;password=tw_mysql_root;port=3306;database=web_interface" //"CentralConnnection": "Server=192.168.12.12;Port=3306;uid=installer;password='pstw_mysql_installer';database=pstw_cs;", //DB_dev Local connection - "CentralConnnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs;" //DB_dev Public connection + "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;" }, "Logging": { "LogLevel": {