/// <summary> /// Creates a new assignment category for the specified class. /// If a category of the given class with the given name already exists, return success = false. /// </summary> /// <param name="subject">The course subject abbreviation</param> /// <param name="num">The course number</param> /// <param name="season">The season part of the semester for the class the assignment belongs to</param> /// <param name="year">The year part of the semester for the class the assignment belongs to</param> /// <param name="category">The new category name</param> /// <param name="catweight">The new category weight</param> /// <returns>A JSON object containing {success = true/false} </returns> public IActionResult CreateAssignmentCategory(string subject, int num, string season, int year, string category, int catweight) { // Grab the class id where the Courses matches the parameters (join Classes) to use for the CId in the AssignmentCatagory var query = from cours in db.Courses join clas in db.Classes on cours.CatalogId equals clas.CatalogId where cours.Subject == subject && cours.Num == num && clas.Semester == season && clas.Year == year select clas.CId; //create the new AssignmentCatagory AssignmentCatagories asgCat = new AssignmentCatagories(); asgCat.Name = category; asgCat.Weight = (uint)catweight; asgCat.CId = query.First(); try { db.AssignmentCatagories.Add(asgCat); db.SaveChanges(); return(Json(new { success = true })); } catch (Exception e) { Console.WriteLine("Unable to add the Assignment Catagory.\n" + e.Message); return(Json(new { success = false })); } }
/// <summary> /// Creates a new assignment category for the specified class. /// </summary> /// <param name="subject">The course subject abbreviation</param> /// <param name="num">The course number</param> /// <param name="season">The season part of the semester for the class the assignment belongs to</param> /// <param name="year">The year part of the semester for the class the assignment belongs to</param> /// <param name="category">The new category name</param> /// <param name="catweight">The new category weight</param> /// <returns>A JSON object containing {success = true/false}, /// false if an assignment category with the same name already exists in the same class.</returns> public IActionResult CreateAssignmentCategory(string subject, int num, string season, int year, string category, int catweight) { using (Team94LMSContext db = new Team94LMSContext()) { try { var classID = from t1 in db.Courses where t1.Department == subject && t1.Number == num join t2 in db.Classes on t1.CourseId equals t2.Course where t2.Semester == season && t2.Year == (uint)year select t2.ClassId; AssignmentCatagories newCat = new AssignmentCatagories(); newCat.Class = classID.Single(); newCat.Name = category; newCat.Weight = (uint)catweight; db.AssignmentCatagories.Add(newCat); db.SaveChanges(); return(Json(new { success = true })); } catch (DbUpdateException e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.InnerException.Message); return(Json(new { success = false })); } } }