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/eva_adjust_postponement_detail_quota")] public class eva_adjust_postponement_detail_quotaController : BaseController { #region Private Variables private ILogger _logger; private Ieva_adjust_postponement_detail_quotaService _repository; private IConfiguration Configuration { get; set; } #endregion #region Properties #endregion /// /// Default constructure for dependency injection /// /// /// /// public eva_adjust_postponement_detail_quotaController(ILogger logger, Ieva_adjust_postponement_detail_quotaService 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_adjust_postponement_detail_quotaWithSelectionViewModel), 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, $"Exception in IActionResult Get. {ex.Message}"); } } /// /// Get Blank Item /// /// /// /// Return a blank item /// Returns the item /// Error Occurred [HttpGet("GetBlankItem")] [ProducesResponseType(typeof(eva_adjust_postponement_detail_quotaWithSelectionViewModel), 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}"); } } /// /// Get list items by adjust_postponement_quota_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? adjust_postponement_quota_id) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); return Ok(_repository.GetListByadjust_postponement_quota_id(adjust_postponement_quota_id)); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetList.", ex); return StatusCode(500, $"Exception in IActionResult GetList. {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_adjust_postponement_detail_quotaSearchModel 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, $"Exception in IActionResult GetListBySearch. {ex.Message}"); } } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("eva_adjust_postponement_detail_quota_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult eva_adjust_postponement_detail_quota_report(eva_adjust_postponement_detail_quotaReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var httpclient = MyHelper.getHttpClient(Configuration); string mainurl = Configuration["JasperReportServer:MainURL"]; string url = $"{mainurl}/ro519eva/eva_adjust_postponement_detail_quota_report.{model.filetype}?{MyHelper.GetParameterForJasperReport(model)}"; 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}"); } } /// /// 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_adjust_postponement_detail_quotaInputModel 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, $"Exception while insert. {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_adjust_postponement_detail_quotaInputModel 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, $"Exception while update {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, $"Exception while delete {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); 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, $"Exception while UpdateMultiple. {ex.Message}"); } } return BadRequest(ModelState); } /// /// Add multiple item /// /// /// /// /// /// /// /// Response Result Message /// Response Result Message /// If the model is invalid /// Error Occurred [HttpPost("AddMultiple")] [ProducesResponseType(typeof(CommonResponseMessage), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult AddMultiple(int? adjust_postponement_quota_id, [FromBody] List model, int? fiscal_year, int? theRound) { if (ModelState.IsValid) { try { string rowCount = _repository.AddMultiple(adjust_postponement_quota_id, model, fiscal_year, theRound); var message = new CommonResponseMessage(); message.code = "200"; message.message = "เพิ่มรายชื่อบุคลากร เรียบร้อย จำนวน " + rowCount + " รายชื่อ"; message.data = null; return Ok(message); } catch (Exception ex) { _logger.LogCritical($"Exception while AddMultiple.", ex); return StatusCode(500, $"Exception while AddMultiple. {ex.Message}"); } } return BadRequest(ModelState); } } }