public ActionResult CreateProj(string projname, string projdesc, IEnumerable <string> SelectedUser) { try { if (string.IsNullOrEmpty(projname) || string.IsNullOrEmpty(projdesc) || SelectedUser == null) //if fileds are empty { TempData["EmptyFields"] = "One or more field is empty"; return(RedirectToAction("CreateProj", "Project")); } } catch { TempData["Unknown"] = "Unknown error occurred!"; return(RedirectToAction("CreateProj", "Project")); } //updating the DB- create new project if (ModelState.IsValid) { ReviseDBEntities con = new ReviseDBEntities(); var proj = new project { ProjName = projname, creation_date = DateTime.Today, description = projdesc, status = "Open" }; var usrList = new List <user>(); foreach (var usr in SelectedUser.ToList()) { var newusr = (con.users.Where(u => u.UserName == usr)); usrList.Add(newusr.FirstOrDefault()); } List <projUser> Allusers = new List <projUser>(); foreach (var usr in usrList) //users to assign to the project { var prjusr = new projUser(); prjusr.project = proj; prjusr.user = usr; prjusr.projid = proj.ProjId; prjusr.userid = usr.userid; prjusr.role = 7; prjusr.dep = 5; Allusers.Add(prjusr); } //proj.users = usrList; foreach (var user in Allusers) { con.projUsers.Add(user); } try { con.projects.Add(proj); con.SaveChanges(); } catch (DbUpdateException) { TempData["FailedProj"] = "Project with this name already exist"; return(RedirectToAction("CreateProj", "Project")); } List <category> catlist = new List <category>(); catlist = con.categories.ToList(); //link categories to the project foreach (var cat in catlist) { projCat entity = new projCat(); entity.category = cat; entity.catId = cat.CatId; entity.project = proj; entity.projId = proj.ProjId; con.projCats.Add(entity); con.SaveChanges(); } // the new id of project that create Session["IdProjectToAssign"] = proj.ProjId; Session["projectName"] = proj.ProjName; // now the user Redirect To assign role every member in project. return(RedirectToAction("AssignMembers", new{ id = proj.ProjId })); } if (SelectedUser == null) { TempData["NoUsers"] = "No users selected"; return(RedirectToAction("CreateProj", "Project")); } else { StringBuilder sb = new StringBuilder(); sb.Append("You selected- " + string.Join(",", SelectedUser)); return(Json(new { success = true, message = sb.ToString() })); } }
public ActionResult CreateCategory(string catname, int?totalLimit) { int projid = Convert.ToInt32(Session["projectid"]); //gets the project id //checks if one of the fields are empty try { if (string.IsNullOrEmpty(catname) || totalLimit == null) { TempData["EmptyFields"] = "One or more field is empty"; return(RedirectToAction("CreateCategory", "Category")); } } catch { TempData["Unknown"] = "Unknown error occurred!"; return(RedirectToAction("CreateCategory", "Category")); } //update the DB, creating new category if (ModelState.IsValid) { int total = 0; ReviseDBEntities con = new ReviseDBEntities(); var catTemp = con.categories.SingleOrDefault(c => c.CatName == catname); int catid = 0; if (catTemp != null) { catid = catTemp.CatId; var prjcat = con.projCats.Where(c => c.catId == catid).SingleOrDefault(p => p.projId == projid); if (prjcat != null) { TempData["FailedCat"] = "Category with this name already exist"; return(RedirectToAction("CreateCategory", "Category", new { id = projid })); } else { total = totalLimit ?? 0; var projcat = new projCat //link the category to the specific project { catId = catid, projId = projid, totalLimit = total }; con.projCats.Add(projcat); } } else { var cat = new category { CatName = catname }; foreach (var prj in con.projects) { if (prj.ProjId != projid) { total = 0; } else { total = totalLimit ?? 0; } var prjcat = new projCat { catId = cat.CatId, projId = prj.ProjId, totalLimit = total }; con.projCats.Add(prjcat); } con.categories.Add(cat); } try { con.SaveChanges(); } catch (DbUpdateException) //if the category already exist { TempData["FailedCat"] = "Category with this name already exist"; return(RedirectToAction("CreateCategory", "Category", new { id = projid })); } return(RedirectToAction("CategoryMain", "Category", new { id = projid, name = Session["projectName"].ToString() })); } return(RedirectToAction("CategoryMain", "Category", new { id = projid, name = Session["projectName"].ToString() })); }