diff --git a/Controllers/AgencyReport.Controller.cs b/Controllers/AgencyReport.Controller.cs index d7b2033..c446ea6 100644 --- a/Controllers/AgencyReport.Controller.cs +++ b/Controllers/AgencyReport.Controller.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -15,7 +16,7 @@ using Swashbuckle.AspNetCore.Annotations; namespace rmutr_report.Controllers { [SwaggerTag("สำหรับรายงานผลการดำเนินงานโครงการ")] - public class AgencyReport: Controller + public class AgencyReport : Controller { public readonly Setting _setting; @@ -23,13 +24,24 @@ namespace rmutr_report.Controllers { this._setting = setting; } + [HttpPost, Route("reports/agency_result_report/{type}")] [ApiExplorerSettings(GroupName = "reports")] public IActionResult GetAgencyReport([FromRoute] string type, [FromBody] List agencyReports) { + if (agencyReports == null || agencyReports.Count == 0) + { + return BadRequest("Agency reports cannot be null or empty."); + } + foreach (var agencyReport in agencyReports) { - if (agencyReport.start_date!=null && agencyReport.end_date!=null) + if (agencyReport == null) + { + continue; + } + + if (agencyReport.start_date != null && agencyReport.end_date != null) { agencyReport.startdate = agencyReport.start_date?.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("th-TH")) ?? ""; @@ -37,137 +49,81 @@ namespace rmutr_report.Controllers CultureInfo.CreateSpecificCulture("th-TH")) ?? ""; } - if (agencyReport.bool_11_1==true) - { - agencyReport.bool_11_1_text = "X"; - } - else - { - agencyReport.bool_11_1_text = ""; - } - if (agencyReport.bool_11_2==true) - { - agencyReport.bool_11_2_text = "X"; - } - else - { - agencyReport.bool_11_2_text = ""; - } - if (agencyReport.bool_11_3==true) - { - agencyReport.bool_11_3_text = "X"; - }else - { - agencyReport.bool_11_3_text = ""; - } - // - if (agencyReport.bool_13_1==true) - { - agencyReport.text_13_1 = "X"; - } - else - { - agencyReport.text_13_1 = ""; - } - if (agencyReport.bool_13_2==true) - { - agencyReport.text_13_2 = "X"; - } - else - { - agencyReport.text_13_2 = ""; - } - if (agencyReport.bool_13_3==true) - { - agencyReport.text_13_3 = "X"; - }else - { - agencyReport.text_13_3 = ""; - } - if (agencyReport.bool_13_4==true) - { - agencyReport.text_13_4 = "X"; - }else - { - agencyReport.text_13_4 = ""; - } - if (agencyReport.bool_13_5==true) - { - agencyReport.text_13_5 = "X"; - }else - { - agencyReport.text_13_5 = ""; - } - if (agencyReport.bool_13_6==true) - { - agencyReport.text_13_6 = "X"; - }else - { - agencyReport.text_13_6 = ""; - } - if (agencyReport.bool_13_7==true) - { - agencyReport.text_13_7 = "X"; - }else - { - agencyReport.text_13_7 = ""; - } - // - if (agencyReport.bool_25_1==true) - { - agencyReport.text_25_1 = "X"; - } - else - { - agencyReport.text_25_1 = ""; - } - if (agencyReport.bool_25_2==true) - { - agencyReport.text_25_2 = "X"; - } - else - { - agencyReport.text_25_2 = ""; - } + agencyReport.bool_11_1_text = agencyReport.bool_11_1 == true ? "X" : ""; + agencyReport.bool_11_2_text = agencyReport.bool_11_2 == true ? "X" : ""; + agencyReport.bool_11_3_text = agencyReport.bool_11_3 == true ? "X" : ""; + + agencyReport.text_13_1 = agencyReport.bool_13_1 == true ? "X" : ""; + agencyReport.text_13_2 = agencyReport.bool_13_2 == true ? "X" : ""; + agencyReport.text_13_3 = agencyReport.bool_13_3 == true ? "X" : ""; + agencyReport.text_13_4 = agencyReport.bool_13_4 == true ? "X" : ""; + agencyReport.text_13_5 = agencyReport.bool_13_5 == true ? "X" : ""; + agencyReport.text_13_6 = agencyReport.bool_13_6 == true ? "X" : ""; + agencyReport.text_13_7 = agencyReport.bool_13_7 == true ? "X" : ""; + + agencyReport.text_25_1 = agencyReport.bool_25_1 == true ? "X" : ""; + agencyReport.text_25_2 = agencyReport.bool_25_2 == true ? "X" : ""; } - + + if (_setting == null || string.IsNullOrEmpty(_setting.report_path)) + { + return StatusCode(500, "Report settings are not configured."); + } + Report report = new Report(); - report.Load(_setting.report_path + "agency_report.frx"); - report.RegisterData(agencyReports, "agency_report"); - report.Prepare(); + try + { + report.Load(_setting.report_path + "agency_report.frx"); + report.RegisterData(agencyReports, "agency_report"); + report.Prepare(); + } + catch (Exception ex) + { + return StatusCode(500, $"Report generation failed: {ex.Message}"); + } MemoryStream stream = new MemoryStream(); - switch (type) + try { - case "pdf": - PDFExport pdf = new PDFExport(); - report.Export(pdf, stream); - stream.Seek(0, SeekOrigin.Begin); - return File(stream, "application/pdf"); + switch (type?.ToLower()) + { + case "pdf": + PDFExport pdf = new PDFExport(); + report.Export(pdf, stream); + break; - case "xls": - case "xlsx": - Excel2007Export excel = new Excel2007Export(); - report.Export(excel, stream); - stream.Seek(0, SeekOrigin.Begin); - return File(stream, "application/vnd.ms-excel"); - break; - case "mht": - MHTExport mht = new MHTExport(); - report.Export(mht, stream); - stream.Seek(0, SeekOrigin.Begin); - return File(stream, "multipart/related"); - break; - case "doc": case "docx": - Word2007Export word = new Word2007Export(); - report.Export(word, stream); - stream.Seek(0, SeekOrigin.Begin); - return File(stream, "appllication/vnd.ms-word"); - break; + case "xls": + case "xlsx": + Excel2007Export excel = new Excel2007Export(); + report.Export(excel, stream); + break; + + case "mht": + MHTExport mht = new MHTExport(); + report.Export(mht, stream); + break; + + case "doc": + case "docx": + Word2007Export word = new Word2007Export(); + report.Export(word, stream); + break; + + default: + return BadRequest("Unsupported export type."); + } + + stream.Seek(0, SeekOrigin.Begin); + string contentType = type == "pdf" ? "application/pdf" : + type == "xls" || type == "xlsx" ? "application/vnd.ms-excel" : + type == "mht" ? "multipart/related" : + "application/vnd.ms-word"; + return File(stream, contentType); + } + catch (Exception ex) + { + return StatusCode(500, $"File export failed: {ex.Message}"); } - - return Ok(); } - } } \ No newline at end of file diff --git a/obj/Debug/net5.0/rmutr_report.AssemblyInfo.cs b/obj/Debug/net5.0/rmutr_report.AssemblyInfo.cs index 52f7543..0e7a98a 100644 --- a/obj/Debug/net5.0/rmutr_report.AssemblyInfo.cs +++ b/obj/Debug/net5.0/rmutr_report.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("rmutr_report")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+91cff19bff1260866d947163111eb8dd29ad5077")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9a4f0e95eaab3a2fdfc849b0360aca819504bede")] [assembly: System.Reflection.AssemblyProductAttribute("rmutr_report")] [assembly: System.Reflection.AssemblyTitleAttribute("rmutr_report")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net5.0/rmutr_report.AssemblyInfoInputs.cache b/obj/Debug/net5.0/rmutr_report.AssemblyInfoInputs.cache index 56e93e5..0ff9b20 100644 --- a/obj/Debug/net5.0/rmutr_report.AssemblyInfoInputs.cache +++ b/obj/Debug/net5.0/rmutr_report.AssemblyInfoInputs.cache @@ -1 +1 @@ -0acb8f26062a50e497e3d20f8ebbaf2a2401ca261db2b8a593f3d5291aef38be +e563ea69829e73d4ba56ee977869e67c6b68c3d5c86c9a065ecf6a266aae8031 diff --git a/obj/rider.project.model.nuget.info b/obj/rider.project.model.nuget.info index 146e73e..c6af64b 100644 --- a/obj/rider.project.model.nuget.info +++ b/obj/rider.project.model.nuget.info @@ -1 +1 @@ -17376239754643386 \ No newline at end of file +17379510230109028 \ No newline at end of file