public async Task <IActionResult> UpdateAllowedEmployee([FromBody] AllowedEmployeeViewModel allowed) { var message = "Update allowed failed"; try { if (allowed != null) { string contentRootPath = this.hostingEnv.ContentRootPath; // get activity code from json file var allowedEmployees = JsonConvert.DeserializeObject <List <AllowedEmployeeViewModel> > (await System.IO.File.ReadAllTextAsync(contentRootPath + "/Data/allowed_emp.json")); // get activity code from somchai database server if (allowedEmployees.Any()) { if (allowedEmployees.Any(x => x.EmpCode == allowed.EmpCode)) { var index = allowedEmployees.FindIndex(x => x.EmpCode == allowed.EmpCode); if (index >= 0) { allowedEmployees[index].EmpCode = allowed.EmpCode; allowedEmployees[index].SubLevel = allowed.SubLevel; } } else { allowedEmployees.Add(new AllowedEmployeeViewModel() { EmpCode = allowed.EmpCode, SubLevel = allowed.SubLevel, }); } await System.IO.File.WriteAllTextAsync(contentRootPath + "/Data/allowed_emp.json", JsonConvert.SerializeObject(allowedEmployees)); return(new JsonResult(allowed, this.DefaultJsonSettings)); } } } catch (Exception ex) { message = $"Has error {ex.ToString()}"; } return(BadRequest(new { message })); }
public async Task <IActionResult> PutUpdateAllowedEmployee(string key, [FromBody] AllowedEmployeeViewModel allowed) { var message = "Data not been found."; try { if (!string.IsNullOrEmpty(key) && allowed != null) { string contentRootPath = this.hosting.ContentRootPath; // get activity code from json file var allowedEmployees = JsonConvert.DeserializeObject <List <AllowedEmployeeViewModel> > (await System.IO.File.ReadAllTextAsync(contentRootPath + "/Data/allowed_emp.json")); if (allowedEmployees.Any(x => x.EmpCode == allowed.EmpCode)) { var index = allowedEmployees.FindIndex(x => x.EmpCode == key); if (index >= 0) { allowedEmployees[index].EmpCode = key; allowedEmployees[index].NameThai = allowed.NameThai; allowedEmployees[index].SubLevel = allowed.SubLevel; } await System.IO.File.WriteAllTextAsync(contentRootPath + "/Data/allowed_emp.json", JsonConvert.SerializeObject(allowedEmployees)); return(new JsonResult(allowed, this.DefaultJsonSettings)); } } } catch (Exception ex) { message = $"Has error {ex.ToString()}"; } return(BadRequest(new { message })); }