using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using HRM.BO; using HRM.DA; using HRM.UI.DTOs.Training; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using static iTextSharp.text.pdf.AcroFields; namespace HRM.UI.Controllers { [ApiController] [Route("api/Training")] [Authorize] public class TrainingController : ControllerBase { private readonly ITrainingTypeService _trainingTypeService; private readonly ITrainingService _trainingService; private readonly IInstitutionService _institueService; private readonly INatureOfTrainingService _natureOfTrainingService; private readonly ITrainingScheduleService _trainingScheduleService; public TrainingController(ITrainingTypeService trainingType, ITrainingService training, IInstitutionService institutionService, INatureOfTrainingService natureOfTrainingService, ITrainingScheduleService trainingScheduleService) { _trainingTypeService = trainingType; this._trainingService = training; this._institueService = institutionService; this._natureOfTrainingService = natureOfTrainingService; _trainingScheduleService = trainingScheduleService; } [HttpGet("GetAllTrainingType/{status}/{code}/{name}")] public ActionResult GetAllTrainingType(EnumStatus status, string code, string name) { code = GlobalFunctions.GetApiDefaultData(code); name = GlobalFunctions.GetApiDefaultData(name); List items = new List(); CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); try { items = this._trainingTypeService.Get(status); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } [HttpGet("GetTrainingybyTypeID/{traininTypeID}")] public ActionResult GetTrainingybyTypeID(int traininTypeID) { List items = new List(); try { items = this._trainingService.GetbyTypeID(traininTypeID); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } [HttpGet("GetAllTraining/{status}/{code}/{name}")] public ActionResult GetAllTraining(EnumStatus status, string code, string name) { code = GlobalFunctions.GetApiDefaultData(code); name = GlobalFunctions.GetApiDefaultData(name); List items = new List(); CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); try { items = this._trainingService.Get(); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } [HttpGet("GetAllInstitues/{status}/{code}/{name}")] public ActionResult GetAllInstitues(EnumStatus status, string code, string name) { code = GlobalFunctions.GetApiDefaultData(code); name = GlobalFunctions.GetApiDefaultData(name); List items = new List(); CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); try { items = this._institueService.Get(); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } [HttpGet("GetAllNatureOfTraining/{status}/{code}/{name}")] public ActionResult GetAllNatureOfTraining(EnumStatus status, string code, string name) { code = GlobalFunctions.GetApiDefaultData(code); name = GlobalFunctions.GetApiDefaultData(name); List items = new List(); CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); try { items = this._natureOfTrainingService.Get(); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } [HttpPost] [Route("savetrainingschedule")] public ActionResult savetrainingschedule(TrainingSchedule ob) { try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); ob.CreatedBy = currentUser.UserID; ob.CreatedDate = DateTime.Today; if(ob.EnrolledStartDate== DateTime.MinValue) { ob.EnrolledStartDate = null; } if (ob.EnrolledEndDate == DateTime.MinValue) { ob.EnrolledEndDate = null; } _trainingScheduleService.Save(ob); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(); } [HttpPost("getTrainingSchedule")] public ActionResult GetTrainingSchedule(dynamic data) { List list = new List(); var items = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); DateTime? fromDate =null; DateTime? toDate = null; if (items["fromDate"] != null) fromDate = (DateTime)items["fromDate"].ToObject(); if (items["toDate"] != null) toDate = (DateTime)items["toDate"].ToObject(); int trainingId = items["trainingId"].ToObject(); int trainingTypeId = items["trainingTypeId"].ToObject(); try { list = _trainingScheduleService.GetTrainingSchedule(trainingId, trainingTypeId, fromDate, toDate); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(list); } [HttpGet("getTrainingScheduleById/{id}")] public ActionResult getTrainingScheduleById(int id) { TrainingSchedule item = null; CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); try { List departments = new DepartmentService().Get(EnumStatus.Regardless, (int)payrollTypeId); item = this._trainingScheduleService.Get(id); if(item != null && item.EnrolledTrainingEmployees != null && item.EnrolledTrainingEmployees.Count > 0) { foreach(var x in item.EnrolledTrainingEmployees) { x.Employee = new Employee(); x.Employee = new EmployeeService().Get(x.EmployeeID); var department = departments.FirstOrDefault(d => d.ID == x.Employee.DepartmentID); if (department != null) x.Employee.DepartmentName = department.Name; } } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(item); } [HttpGet] [Route("deleteTrainingByID/{id}")] public ActionResult deleteTrainingByID(int id) { try { this._trainingScheduleService.Delete(id); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(); } } }