public void Post([FromBody] RoleEntity.Role role) { context.Entry(role).State = EntityState.Added; context.Roles.Add(role); context.SaveChanges(); CreatedAtAction(nameof(role), new { id = role.ID }, role); }
public void Put(long id, [FromBody] RoleEntity.Role role) { if (id != role.ID) { BadRequest(); return; } context.Entry(role).State = EntityState.Modified; context.Update(role); context.SaveChanges(); }
private RoleEntity.Role GetPopulateData(string id, string paramAll) { string[] iParams; iParams = paramAll.Split('~'); string roleName = iParams[0].ToString(); string desc = iParams[1].ToString(); roleInfo = new RoleEntity.Role(); if (!string.IsNullOrEmpty(id) && id != "0") { roleInfo.ID = Convert.ToInt16(id); } roleInfo.RoleName = roleName; roleInfo.Desc = desc; if (!string.IsNullOrEmpty(id) && id != "0") { HttpClient client = new HttpClient(); string baseUrl = Url.Action("", "", null, HttpContext.Request.Scheme); client.BaseAddress = new Uri(baseUrl); var text = client.GetStringAsync("api/RoleApi/GetList").Result; var resultRole = JsonConvert.DeserializeObject <List <RoleEntity.Role> >(text); roleInfo.CreatedDate = resultRole.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().CreatedDate; roleInfo.CreatedBy = resultRole.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().CreatedBy; roleInfo.UpdatedDate = DateTime.Now; roleInfo.UpdatedBy = "System"; } else { roleInfo.CreatedDate = DateTime.Now; roleInfo.CreatedBy = "System"; roleInfo.UpdatedDate = null; roleInfo.UpdatedBy = null; } return(roleInfo); }
public ActionResult AddEditRole(string id, string paramAll) { object result = null; try { string[] iParams; iParams = paramAll.Split('~'); string roleName = iParams[0].ToString(); HttpClient client = new HttpClient(); string baseUrl = Url.Action("", "", null, HttpContext.Request.Scheme); client.BaseAddress = new Uri(baseUrl); var text = client.GetStringAsync("api/RoleApi/GetList").Result; var resultRole = JsonConvert.DeserializeObject <List <RoleEntity.Role> >(text); int countRoleName = resultRole.Where(x => x.RoleName == roleName.Trim()).Count(); // If data empty bool isFieldNull = false; for (int x = 0; x < iParams.Count(); x++) { if (string.IsNullOrEmpty(iParams[x].ToString())) { isFieldNull = true; break; } } if (!string.IsNullOrEmpty(id) && id != "0") { string roleNameEdit = resultRole.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().RoleName; if (isFieldNull) { result = new { error = 1 } } ; else if (roleNameEdit != roleName && countRoleName > 0) { result = new { error = 2 } } ; else { // Edit Role roleInfo = new RoleEntity.Role(); roleInfo = GetPopulateData(id, paramAll); var jsonString = JsonConvert.SerializeObject(roleInfo); var putTask = client.PutAsync("api/RoleApi/UpdateById=" + id, new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")); putTask.Wait(); result = new { error = "Edit" }; } } else { if (isFieldNull) { result = new { error = 1 } } ; else if (countRoleName > 0) { result = new { error = 2 } } ; else { // Add Role roleInfo = new RoleEntity.Role(); roleInfo = GetPopulateData(id, paramAll); var jsonString = JsonConvert.SerializeObject(roleInfo); var putTask = client.PostAsync("api/RoleApi/CreateNew", new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")); putTask.Wait(); result = new { error = "Add" }; } } } catch (Exception ex) { Log.WriteLog(ex.Message, hosting); } return(Json(result)); }