public HttpResponseMessage AddUserRoleTbl(UserDataModel userDataModal) { try { UserTbl userTbl = new UserTbl(); userTbl.UserName = userDataModal.UserName; userTbl.UserPwd = userDataModal.UserPwd; userTbl.LastName = userDataModal.LastName; userTbl.FirstName = userDataModal.FirstName; userTbl.UserEmail = userDataModal.UserEmail; userTbl.UserPhone = userDataModal.UserPhone; int[] roleId = userDataModal.RoleId; db.UserTbls.Add(userTbl); db.SaveChanges(); int latestUserId = userTbl.UserId; foreach (int items in roleId) { UserRoleTbl userRoleTbl = new UserRoleTbl(); userRoleTbl.UserId = latestUserId; userRoleTbl.RoleId = items; db.UserRoleTbls.Add(userRoleTbl); } db.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, userTbl); return(message); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage UpdateUser(int id, UserDataModel userDataModel) { try { UserTbl userTbl = new UserTbl(); var data = db.UserTbls.FirstOrDefault(x => x.UserId == id); if (data == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "UserTbl with userid = " + id.ToString() + " not found to update")); } else { data.LastName = userDataModel.LastName; data.FirstName = userDataModel.FirstName; data.UserEmail = userDataModel.UserEmail; data.UserPhone = userDataModel.UserPhone; db.SaveChanges(); List <UserRoleTbl> myUserRoleTbl = db.UserRoleTbls.Where(x => x.UserId == id).ToList(); if (myUserRoleTbl.Count() > 0) { for (int j = 0; j < myUserRoleTbl.Count(); j++) { db.UserRoleTbls.Remove(myUserRoleTbl[j]); } db.SaveChanges(); } int[] roleId = userDataModel.RoleId; int id1 = id; foreach (int items in roleId) { UserRoleTbl userRoleTbl = new UserRoleTbl(); userRoleTbl.UserId = id1; userRoleTbl.RoleId = items; db.UserRoleTbls.Add(userRoleTbl); } db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, data)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage DeleteUser(UserDataModel userDataModel) { try { UserRoleTbl userRoleTbl = new UserRoleTbl(); int id = userDataModel.UserId; var data = db.UserRoleTbls.FirstOrDefault(x => x.UserId == id); if (data == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "UserRoleTbl with userid = " + id.ToString() + " not found to delete")); } else { List <UserRoleTbl> myUserRoleTbl = db.UserRoleTbls.Where(x => x.UserId == id).ToList(); if (myUserRoleTbl.Count() > 0) { for (int j = 0; j < myUserRoleTbl.Count(); j++) { db.UserRoleTbls.Remove(myUserRoleTbl[j]); } db.SaveChanges(); } UserTbl myUserTbl = db.UserTbls.Where(x => x.UserId == id).FirstOrDefault(); myUserTbl.DeletedDate = DateTime.Now; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "userid = " + id.ToString() + " is deleted successfully...!!")); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage AddUserRoleTbl(UserDataModel userDataModal) { int val = 0; try { UserTbl userTbl = new UserTbl(); userTbl.UserName = userDataModal.UserName; userTbl.UserPwd = userDataModal.UserPwd; userTbl.LastName = userDataModal.LastName; userTbl.FirstName = userDataModal.FirstName; userTbl.UserEmail = userDataModal.UserEmail; userTbl.UserPhone = userDataModal.UserPhone; int[] roleId = new int[userDataModal.RoleId.Length]; //string roleId = userDataModal.RoleId; //string role = ""; db.UserTbls.Add(userTbl); db.SaveChanges(); int latestUserId = userTbl.UserId; //for(int i=0; i<roleId.Length; ++i) //{ // val = roleId[i]; // UserRoleTbl userRoleTbl = new UserRoleTbl(); // userRoleTbl.UserId = latestUserId; // userRoleTbl.RoleId = val; // db.UserRoleTbls.Add(userRoleTbl); //} foreach (int items in roleId) { UserRoleTbl userRoleTbl = new UserRoleTbl(); userRoleTbl.UserId = latestUserId; userRoleTbl.RoleId = items; db.UserRoleTbls.Add(userRoleTbl); } //while (roleId.Length > 0) //{ // role = roleId.Contains(",") ? // roleId.Substring(0, roleId.IndexOf(",")) // : roleId; // val = Convert.ToInt32(role); // if (!roleId.Contains(",")) // roleId = ""; // else // roleId = roleId.Substring(roleId.IndexOf(",") + 1); // UserRoleTbl userRoleTbl = new UserRoleTbl(); // userRoleTbl.UserId = latestUserId; // userRoleTbl.RoleId = val; // db.UserRoleTbls.Add(userRoleTbl); //} db.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, userTbl); return(message); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }