using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using HRM.BO; using Microsoft.AspNetCore.Http; using System.Data; using System.Reflection; namespace HRM.UI.Controllers.Mobile { [Route("api/MobileAuthentication")] [ApiController] public class MobileAuthenticationController : ControllerBase { private T GetItem(DataRow dr) { Type temp = typeof(T); T obj = Activator.CreateInstance(); foreach (DataColumn column in dr.Table.Columns) { foreach (PropertyInfo pro in temp.GetProperties()) { if (pro.Name == column.ColumnName) pro.SetValue(obj, dr[column.ColumnName], null); else continue; } } return obj; } public readonly IEmpMobileService _empMobileService; public MobileAuthenticationController( IEmpMobileService empMobileService) { this._empMobileService = empMobileService; } [HttpGet("getAllDevice")] public ActionResult getAllDevice() { List items = new List(); try { items = _empMobileService.Get(); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(items); } [HttpGet("getActiveDevice")] public ActionResult getActiveDevice() { DataSet dset = new DataSet(); try { dset = _empMobileService.getActiveDevice(true); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } var lst = dset.Tables[0].AsEnumerable() .Select(dataRow => new { empNo = dataRow.Field("EMPLOYEENO"), empName = dataRow.Field("NAME"), empMobID = dataRow.Field("EmpMobID"), deviceNo = dataRow.Field("DeviceNo"), creationDate = dataRow.Field("CREATIONDATE").ToString("dd MMM yyyy"), isActive = dataRow.Field("IsActive") == 1 ? "Active" : "InActive", }).ToList(); return Ok(lst); } [HttpGet("getInActiveDevice")] public ActionResult getInActiveDevice() { DataSet dset = new DataSet(); try { dset = _empMobileService.getActiveDevice(false); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } var lst = dset.Tables[0].AsEnumerable() .Select(dataRow => new { empNo = dataRow.Field("EMPLOYEENO"), empName = dataRow.Field("NAME"), empMobID = dataRow.Field("EmpMobID"), deviceNo = dataRow.Field("DeviceNo"), creationDate = dataRow.Field("CREATIONDATE").ToString("dd MMM yyyy"), isActive = dataRow.Field("IsActive") == 1 ? "Active" : "InActive", }).ToList(); return Ok(lst); } [HttpPost] [Route("delete")] public ActionResult delete(EmpMobile pEmpMobile) { try { _empMobileService.Delete(pEmpMobile); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(); } [HttpPost] [Route("deleteDevice")] public ActionResult deleteDevice(dynamic data) { List items = new List(); var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); string ids = (string)item["ids"].ToObject(); try { _empMobileService.Delete(ids); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(); } [HttpPost] [Route("Save")] public ActionResult Save(EmpMobile item) { try { _empMobileService.Save(item); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(); } [HttpPost] [Route("SaveDevice")] public ActionResult SaveDevice(dynamic data) { List items = new List(); var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); string ids = (string)item["ids"].ToObject(); try { _empMobileService.Save(ids); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } return Ok(); } } }