Files
hrm_eva/ApiControllers/eva_adjust_postponement_detail_quotaControllers.cs

361 lines
14 KiB
C#

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<eva_adjust_postponement_detail_quotaController> _logger;
private Ieva_adjust_postponement_detail_quotaService _repository;
private IConfiguration Configuration { get; set; }
#endregion
#region Properties
#endregion
/// <summary>
/// Default constructure for dependency injection
/// </summary>
/// <param name="repository"></param>
/// <param name="configuration"></param>
/// <param name="logger"></param>
public eva_adjust_postponement_detail_quotaController(ILogger<eva_adjust_postponement_detail_quotaController> logger, Ieva_adjust_postponement_detail_quotaService repository, IConfiguration configuration)
{
_logger = logger;
_repository = repository;
Configuration = configuration;
}
/// <summary>
/// Get specific item by id
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>Return Get specific item by id</returns>
/// <response code="200">Returns the item</response>
/// <response code="500">Error Occurred</response>
[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}");
}
}
/// <summary>
/// Get Blank Item
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>Return a blank item</returns>
/// <response code="200">Returns the item</response>
/// <response code="500">Error Occurred</response>
[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}");
}
}
/// <summary>
/// Get list items by adjust_postponement_quota_id
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>Return list of items by specifced keyword</returns>
/// <response code="200">Returns the item</response>
/// <response code="500">Error Occurred</response>
[HttpGet("")]
[ProducesResponseType(typeof(List<eva_adjust_postponement_detail_quotaViewModel>), 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}");
}
}
/// <summary>
/// Get list items by search
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>Return list of items by specifced keyword</returns>
/// <response code="200">Returns the item</response>
/// <response code="500">Error Occurred</response>
[HttpGet("GetListBySearch")]
[ProducesResponseType(typeof(List<eva_adjust_postponement_detail_quotaViewModel>), 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}");
}
}
/// <summary>
/// Create new item
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="model"></param>
/// <returns>Response Result Message</returns>
/// <response code="200">Response Result Message</response>
/// <response code="400">If the model is invalid</response>
/// <response code="500">Error Occurred</response>
[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);
}
/// <summary>
/// Update item
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="id"></param>
/// <param name="model"></param>
/// <returns>Response Result Message</returns>
/// <response code="200">Response Result Message</response>
/// <response code="400">If the model is invalid</response>
/// <response code="500">Error Occurred</response>
[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);
}
/// <summary>
/// Delete item
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="id"></param>
/// <returns>Response Result Message</returns>
/// <response code="200">Response Result Message</response>
/// <response code="400">If the model is invalid</response>
/// <response code="500">Error Occurred</response>
[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, $"ไม่สามารถลบข้อมูลได้ เพราะข้อมูลถูกใช้ในส่วนงานอื่น");
}
}
return BadRequest(ModelState);
}
/// <summary>
/// Update multiple item
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="model"></param>
/// <returns>Response Result Message</returns>
/// <response code="200">Response Result Message</response>
/// <response code="400">If the model is invalid</response>
/// <response code="500">Error Occurred</response>
[HttpPut("UpdateMultiple")]
[ProducesResponseType(typeof(CommonResponseMessage), 200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
//[ValidateAntiForgeryToken]
public IActionResult UpdateMultiple([FromBody] List<eva_adjust_postponement_detail_quotaInputModel> 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);
}
/// <summary>
/// Add multiple item
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="adjust_postponement_quota_id"></param>
/// <param name="model"></param>
/// <param name="fiscal_year"></param>
/// <param name="theRound"></param>
/// <returns>Response Result Message</returns>
/// <response code="200">Response Result Message</response>
/// <response code="400">If the model is invalid</response>
/// <response code="500">Error Occurred</response>
[HttpPost("AddMultiple")]
[ProducesResponseType(typeof(CommonResponseMessage), 200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
//[ValidateAntiForgeryToken]
public IActionResult AddMultiple(int? adjust_postponement_quota_id, [FromBody] List<int> 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);
}
}
}