using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; using TTSW.Controllers; using TTSW.EF; using TTSW.Utils; using TTSW.Constant; using TTSW.Common; using TodoAPI2.Models; using System.Data; using Microsoft.Extensions.Configuration; using System.IO; namespace TodoAPI2.Controllers { //[Authorize] [Produces("application/json")] [Route("api/rep_study_history")] public class rep_study_historyController : BaseController { #region Private Variables private ILogger _logger; private Irep_study_historyService _repository; private IConfiguration Configuration { get; set; } #endregion #region Properties #endregion /// /// Default constructure for dependency injection /// /// /// /// public rep_study_historyController(ILogger logger, Irep_study_historyService repository, IConfiguration configuration) { _logger = logger; _repository = repository; Configuration = configuration; } /// /// Get Blank Item /// /// /// /// Return a blank item /// Returns the item /// Error Occurred [HttpGet("GetBlankItem")] [ProducesResponseType(typeof(rep_study_historyWithSelectionViewModel), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult GetBlankItem() { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.GetBlankItem(); return Ok(result); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetBlankItem.", ex); return StatusCode(500, $"Exception in IActionResult GetBlankItem. {ex.Message}"); } } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("rep_study_history_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult rep_study_history_report(rep_study_historyReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var httpclient = MyHelper.getHttpClient(Configuration); string mainurl = Environment.GetEnvironmentVariable("JasperReportServer_MainURL"); string reportsite = Environment.GetEnvironmentVariable("JasperReportServer_reportsite"); string username = Environment.GetEnvironmentVariable("JasperReportServer_username"); string password = Environment.GetEnvironmentVariable("JasperReportServer_password"); string url = $"{mainurl}{reportsite}/rep_study_history.{model.filetype}?{MyHelper.GetParameterForJasperReport(model)}&j_username={username}&j_password={password}"; var data = httpclient.DownloadData(url); var stream = new MemoryStream(data); return File(stream, model.contentType); } catch (Exception ex) { _logger.LogCritical($"Exception while GetReport.", ex); return StatusCode(500, $"Exception while GetReport. {ex.Message}"); } } } }