/// <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) { var query = from courses in db.Course join classes in db.Class on courses.CourseId equals classes.CourseId where courses.Subject == subject && courses.Number == num && classes.Season == season && classes.Year == year select classes.ClassId; AsgnCategory asgnCategory = new AsgnCategory(); asgnCategory.CatId = db.AsgnCategory.Count() + 1; asgnCategory.Name = category; asgnCategory.Weight = catweight; asgnCategory.ClaId = query.First(); // The query will never be empty. db.AsgnCategory.Add(asgnCategory); try { db.SaveChanges(); } catch (Exception e) { return(Json(new { success = false })); } return(Json(new { success = true })); }
/// <summary> /// Creates a new assignment category for the specified class. /// A class can not have two categories with the same name. /// 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) { // Note: The uniqueness constraint for names within the same class is // provided by SQL. When an attempt is made to add a category with the // same name for the same class, an exception is thrown and this // method returns a JSON object where 'success = false'. // Combine 'Course' and 'Class' tables for retrieval of // the appropriate 'subject' and 'Class' instance. var query = from courses in db.Course join classes in db.Class on courses.CourseId equals classes.CourseId // Match to the appropriate 'Class' instance of the // given course. where courses.Subject == subject && courses.Number == num && classes.Season == season && classes.Year == year // The class ID to which this new assignment category // will belong. select classes.ClassId; // TODO: See the same questions about a similar code block within the // 'CreateAssignment' method. AsgnCategory asgnCategory = new AsgnCategory(); asgnCategory.CatId = db.AsgnCategory.Count() + 1; asgnCategory.Name = category; asgnCategory.Weight = catweight; asgnCategory.ClaId = query.First(); // The query will never be empty. db.AsgnCategory.Add(asgnCategory); try { db.SaveChanges(); } catch (Exception e) { // Most likely, this assignment category name for this class // already exists. return(Json(new { success = false })); } return(Json(new { success = true })); }