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; using System.Net; namespace TodoAPI2.Controllers { //[Authorize] [Produces("application/json")] [Route("api/eva_evaluation_operating_agreement")] public class eva_evaluation_operating_agreementController : BaseController { #region Private Variables private ILogger _logger; private Ieva_evaluation_operating_agreementService _repository; private IConfiguration Configuration { get; set; } #endregion #region Properties #endregion /// /// Default constructure for dependency injection /// /// /// /// public eva_evaluation_operating_agreementController(ILogger logger, Ieva_evaluation_operating_agreementService repository, IConfiguration configuration) { _logger = logger; _repository = repository; Configuration = configuration; } /// /// Get specific item by id /// /// /// /// Return Get specific item by id /// Returns the item /// Error Occurred [HttpGet("{id}")] [ProducesResponseType(typeof(eva_evaluation_operating_agreementWithSelectionViewModel), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult Get(int id) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.GetWithSelection(id); return Ok(result); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult Get.", ex); return StatusCode(500, $"{ex.Message}"); } } /// /// Get Blank Item /// /// /// /// Return a blank item /// Returns the item /// Error Occurred [HttpGet("GetBlankItem")] [ProducesResponseType(typeof(eva_evaluation_operating_agreementWithSelectionViewModel), 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, $"{ex.Message}"); } } /// /// Get list items by create_evaluation_detail_id /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("")] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult GetList(int? create_evaluation_detail_id) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); return Ok(_repository.GetListBycreate_evaluation_detail_id(create_evaluation_detail_id)); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetList.", ex); return StatusCode(500, $"{ex.Message}"); } } /// /// Get list items by search /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("GetListBySearch")] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult GetListBySearch(eva_evaluation_operating_agreementSearchModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); return Ok(_repository.GetListBySearch(model)); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetListBySearch.", ex); return StatusCode(500, $"{ex.Message}"); } } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("eva_evaluation_operating_agreement_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult eva_evaluation_operating_agreement_report(eva_evaluation_operating_agreementReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var httpclient = new WebClient(); string mainurl = MyHelper.GetConfig(Configuration, "JasperReportServer:MainURL"); string reportsite = MyHelper.GetConfig(Configuration, "JasperReportServer:reportsite"); string username = MyHelper.GetConfig(Configuration, "JasperReportServer:username"); string password = MyHelper.GetConfig(Configuration, "JasperReportServer:password"); string url = $"{mainurl}{reportsite}/xxใส่ชื่อรายงานตรงนี้xx.{model.filetype}?{MyHelper.GetParameterForJasperReport(model)}&j_username={username}&j_password={password}"; if (model.filetype == "xlsx") { url += "&ignorePagination=true"; } 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, $"{ex.Message}"); } } /// /// Create new item /// /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpPost("")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult Insert([FromBody] eva_evaluation_operating_agreementInputModel model) { if (ModelState.IsValid) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.Insert(model); var message = new CommonResponseMessage(); message.code = "200"; message.message = $"เพิ่มข้อมูล เรียบร้อย"; message.data = result; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while insert.", ex); return StatusCode(500, $"{ex.Message}"); } } return BadRequest(ModelState); } /// /// Update item /// /// /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpPut("{id}")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult Update(int id, [FromBody] eva_evaluation_operating_agreementInputModel model) { if (ModelState.IsValid) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.Update(id, model); var message = new CommonResponseMessage(); message.code = "200"; message.message = $"แก้ไขข้อมูล เรียบร้อย"; message.data = result; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while update {id.ToString()}.", ex); return StatusCode(500, $"{id.ToString()}. {ex.Message}"); } } return BadRequest(ModelState); } /// /// Delete item /// /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpDelete("{id}")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult Delete(int id) { if (ModelState.IsValid) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); _repository.Delete(id); var message = new CommonResponseMessage(); message.code = "200"; message.message = $"ลบข้อมูล เรียบร้อย"; message.data = null; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while delete {id.ToString()}.", ex); return StatusCode(500, $"{id.ToString()}. {ex.Message}"); } } return BadRequest(ModelState); } /// /// Update multiple item /// /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpPut("UpdateMultiple")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult UpdateMultiple([FromBody] List model) { if (ModelState.IsValid) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); string rowCount = _repository.UpdateMultiple(model, true); var message = new CommonResponseMessage(); message.code = "200"; message.message = "ปรับปรุงข้อมูลเรียบร้อย จำนวน "+rowCount+" รายการ"; message.data = null; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while UpdateMultiple.", ex); return StatusCode(500, $"{ex.Message}"); } } return BadRequest(ModelState); } /// /// Refresh AutoField of all items /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpPut("RefreshAutoField")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult RefreshAutoField() { if (ModelState.IsValid) { try { //if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); _repository.RefreshAutoFieldOfAllData(); var message = new CommonResponseMessage(); message.code = "200"; message.message = $"ปรับปรุง Auto Field ของทุก record เรียบร้อย"; message.data = null; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while RefreshAutoField.", ex); return StatusCode(500, $"มีปัญหาระหว่างการปรับปรุง Auto Field. {ex.Message}"); } } return BadRequest(ModelState); } } }