/// <summary> /// You should implement this function, such that all tests will pass. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { // TODO: implement this logic! var course = (from c in _courseInstances.All() where c.ID == courseInstanceID select c).SingleOrDefault(); if (course == null) { throw new AppObjectNotFoundException(); } var person = (from p in _persons.All() where p.SSN == model.SSN select new PersonDTO { Name = p.Name, SSN = p.SSN }).SingleOrDefault(); if (person == null) { throw new AppObjectNotFoundException(); } if (model.Type == TeacherType.MainTeacher) { var mainTeacher = (from t in _teacherRegistrations.All() where t.CourseInstanceID == courseInstanceID where t.Type == TeacherType.MainTeacher select t).SingleOrDefault(); if (mainTeacher != null) { throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER"); } } var teacherRegistration = (from t in _teacherRegistrations.All() where t.CourseInstanceID == courseInstanceID where t.SSN == model.SSN select t).SingleOrDefault(); if (teacherRegistration != null) { throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE"); } teacherRegistration = new TeacherRegistration() { CourseInstanceID = courseInstanceID, SSN = model.SSN, Type = TeacherType.MainTeacher }; _teacherRegistrations.Add(teacherRegistration); _uow.Save(); return(person); }
/// <summary> /// You should implement this function, such that all tests will pass. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { int total = (from tr in _teacherRegistrations.All() where tr.SSN == model.SSN && tr.CourseInstanceID == courseInstanceID select tr).Count(); // If total is not 0 then this teacher is a teacher in this course already if (total == 0) { TeacherRegistration newRegistration = new TeacherRegistration { SSN = model.SSN, CourseInstanceID = courseInstanceID, Type = model.Type }; _teacherRegistrations.Add(newRegistration); return((from p in _persons.All() where model.SSN == p.SSN select new PersonDTO { SSN = p.SSN, Name = p.Name }).First()); } return(null); }
public async Task <IActionResult> Teacher(TeacherRegistration registration) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = registration.Email, Email = registration.Email, PhoneNumber = registration.PhoneNumber, }; var result = await _userManager.CreateAsync(user, registration.Password); if (result.Succeeded) { await AssignRoleToTeacherAsync(user, registration); await SaveTeacherToDataBaseAsync(user, registration); TempData["TeacherSaved"] = Constant.Saved; return(RedirectToAction("Index", "Teachers")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(View(registration)); }
public Result Handle(TeacherRegistrationCommand command) { var registration = new TeacherRegistration(command.Token, false, command.Email); _context.Registrations.Add(registration); _context.SaveChanges(); return(Result.Ok(value: registration.Id)); }
public IActionResult Edit(TeacherRegistration entity) { MongoDBContext dbContext = new MongoDBContext(); //you can use the UpdateOne to get higher performance if you need. dbContext.TeacherRegistrations.ReplaceOne(m => m.Id == entity.Id, entity); return(View(entity)); }
public void SetTokenStatus_Sets_TokenStatus(bool status) { var schoolClass = new TeacherRegistration("token", !status, "*****@*****.**"); // Act schoolClass.SetTokenStatus(status); // Assert Assert.That(schoolClass.Used, Is.EqualTo(status)); }
public IActionResult Add(TeacherRegistration entity) { MongoDBContext dbContext = new MongoDBContext(); entity.Id = Guid.NewGuid(); dbContext.TeacherRegistrations.InsertOne(entity); return(Redirect("/")); }
/// <summary> /// You should implement this function, such that all tests will pass. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { var course = (from c in _courseInstances.All() where c.ID == courseInstanceID select c).SingleOrDefault(); if (course == null) { throw new AppObjectNotFoundException(); } var newTeacher = (from t in _persons.All() where t.SSN == model.SSN select new PersonDTO { SSN = t.SSN, Name = t.Name, }).SingleOrDefault(); if (newTeacher == null) { throw new AppObjectNotFoundException(); } var isTeacherInCourse = (from t in _teacherRegistrations.All() where t.SSN == newTeacher.SSN && t.CourseInstanceID == courseInstanceID select t).SingleOrDefault(); if (isTeacherInCourse != null) { throw new AppValidationException("The teacher is already registered to this course"); } var isThereAMainTeacher = (from t in _teacherRegistrations.All() where t.CourseInstanceID == courseInstanceID && t.Type == TeacherType.MainTeacher select t).SingleOrDefault(); if (isThereAMainTeacher != null && model.Type == TeacherType.MainTeacher) { throw new AppValidationException("There already is a main theacher in this course"); } var teacherEntity = new TeacherRegistration { SSN = model.SSN, CourseInstanceID = courseInstanceID, Type = model.Type }; _teacherRegistrations.Add(teacherEntity); _uow.Save(); return(newTeacher); }
public async Task SaveTeacherToDataBaseAsync(ApplicationUser user, TeacherRegistration registration) { var teacher = new Teacher { FirstName = registration.FirstName, LastName = registration.LastName, Bio = registration.Bio, Address = registration.Address, UserId = user.Id, PhoneNumber = registration.PhoneNumber }; await _context.Teachers.AddAsync(teacher); await _context.SaveChangesAsync(); }
public ActionResult AddEditTeacher(TeacherDto model) { var item = ab.TeacherRegistrations.FirstOrDefault(x => x.Tid == model.Tid); if (item == null) { item = new TeacherRegistration(); ab.TeacherRegistrations.Add(item); } item.TeacherName = model.TeacherName; item.TeacherCnic = model.TeacherCnic; item.Address = model.Address; item.Email = model.Email; if (model.Password != null) { item.Password = model.Password; } ab.SaveChanges(); var temp = item.Tid; if (model.SubjectIds.Count > 0) { var ListAssign = ab.TeacherAssigns.Where(x => x.TR_FK == model.Tid).ToList(); if (ListAssign.Count > 0) { ab.TeacherAssigns.RemoveRange(ListAssign); ab.SaveChanges(); } foreach (var item1 in model.SubjectIds) { TeacherAssign teacherAssign = new TeacherAssign(); teacherAssign.TR_FK = temp; teacherAssign.SS_FK = item1; ab.TeacherAssigns.Add(teacherAssign); ab.SaveChanges(); } ab.SaveChanges(); } return(RedirectToAction("TeacherRecord")); }
/// <summary> /// You should implement this function, such that all tests will pass. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { var course = _courseInstances.All().SingleOrDefault(c => c.ID == courseInstanceID); if (course == null) { throw new AppObjectNotFoundException(); } var teacher = _persons.All().SingleOrDefault(t => t.SSN == model.SSN); if (teacher == null) { throw new AppObjectNotFoundException(); } var isCurrentTeacher = _teacherRegistrations.All().SingleOrDefault(tr => tr.CourseInstanceID == courseInstanceID && tr.SSN == model.SSN); if (isCurrentTeacher != null) { throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE"); } var currentMainTeacherOfCourse = _teacherRegistrations.All().SingleOrDefault(tr => tr.CourseInstanceID == courseInstanceID && tr.Type == TeacherType.MainTeacher); if (currentMainTeacherOfCourse != null) { throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER"); } TeacherRegistration teacherRegistration = new TeacherRegistration { SSN = model.SSN, Type = model.Type, CourseInstanceID = courseInstanceID }; _teacherRegistrations.Add(teacherRegistration); _uow.Save(); return(new PersonDTO { Name = teacher.Name, SSN = teacher.SSN }); }
/// <summary> /// You should implement this function, such that all tests will pass. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { PersonDTO p = GetPersonBySSn(model.SSN); if (p == null) { throw new AppObjectNotFoundException(); } // TODO: implement this logic! CourseInstanceDTO course = GetCourseInstanceByCourseInstanceId(courseInstanceID); if (course == null) { System.Console.WriteLine("THROWING"); throw new AppObjectNotFoundException(); } if (course.MainTeacher != "") { throw new AppValidationException(""); } if (course.MainTeacher == "") { TeacherRegistration tr = new TeacherRegistration { Type = TeacherType.MainTeacher, SSN = model.SSN, CourseInstanceID = courseInstanceID }; _teacherRegistrations.Add(tr); //var person = GetPersonBySSn(model.SSN); return(GetPersonBySSn(tr.SSN)); } return(null); }
public async Task AssignRoleToTeacherAsync(ApplicationUser user, TeacherRegistration registration) { if (!await _roleManager.RoleExistsAsync(Constant.AdminUser)) { await _roleManager.CreateAsync(new IdentityRole(Constant.AdminUser)); } if (!await _roleManager.RoleExistsAsync(Constant.RegularUser)) { await _roleManager.CreateAsync(new IdentityRole(Constant.RegularUser)); } if (registration.IsAdmin) { await _userManager.AddToRoleAsync(user, Constant.AdminUser); } else { await _userManager.AddToRoleAsync(user, Constant.RegularUser); } }
/// <summary> /// You should implement this function, such that all tests will pass. /// --- /// Implemented so that at first we make sure that both course and person exist. If not an AppObjectNotFoundException /// is thrown. If both exist, more checks are made to check if we're updating a teacher or adding a new one. /// In the case that we're adding a main teacher and there already exists a main teacher, an AppValidationException /// is thrown. Also if a teacher is being added that already exists, an AppValidationException is thrown. /// </summary> /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param> /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param> /// <returns>Should return basic information about the person.</returns> /// <exception cref="AppObjectNotFoundException" /> /// <exception cref="AppValidationException" /> public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model) { var person = GetPersonBySSNIfExistsNullIfNot(model.SSN); if (!CheckIfCourseExists(courseInstanceID) || person == null) { throw new AppObjectNotFoundException(); } var teachers = (from tr in _teacherRegistrations.All() where tr.CourseInstanceID == courseInstanceID select tr).ToList(); string mainTeacherSSN = null; TeacherRegistration teacher = null; foreach (var t in teachers) { if (model.Type == TeacherType.MainTeacher && t.Type == TeacherType.MainTeacher) { mainTeacherSSN = t.SSN; } if (model.SSN == t.SSN) { teacher = t; } } if (model.Type == TeacherType.MainTeacher) // Skrá á main teacher { if (teacher != null) // Þessi aðili er skráður sem kennari í þessum kúrs { if (mainTeacherSSN != null) // Skráður er main teacher { if (model.SSN != mainTeacherSSN) // Main teacher er ekki sá sami { throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER"); } } else // Ekki er skráður main teacher { teacher.Type = model.Type; // Teacher type updated _uow.Save(); } return(person); } if (mainTeacherSSN != null) { throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER"); } } else // Skrá á (ekki main) kennara { if (teacher != null) // Þessi aðili er skráður sem kennari í þessum kúrs { if (mainTeacherSSN == null || model.SSN != mainTeacherSSN) { throw new AppValidationException("PERSON_ALREADY_REGISTERED_AS_TEACHER_IN_COURSE"); } teacher.Type = model.Type; // Teacher type updated _uow.Save(); return(person); } } _teacherRegistrations.Add(new TeacherRegistration { SSN = model.SSN, CourseInstanceID = courseInstanceID, Type = model.Type }); _uow.Save(); return(person); }