public async Task <IActionResult> UpdateLecturer(int id, [FromBody] LecturerResource lecturerResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var lecturer = await lecturerRepository.GetLecturer(id); if (lecturer == null) { return(NotFound()); } mapper.Map <LecturerResource, Lecturer>(lecturerResource, lecturer); lecturerRepository.UpdateBoardEnrollments(lecturer, lecturerResource); lecturerRepository.UpdateGroups(lecturer, lecturerResource); lecturerRepository.UpdateProjects(lecturer, lecturerResource); var major = await majorRepository.GetMajor(lecturerResource.MajorId); lecturer.Major = major; await unitOfWork.Complete(); var result = mapper.Map <Lecturer, LecturerResource>(lecturer); return(Ok(result)); }
public async Task <IActionResult> CreateLecturer([FromBody] LecturerResource lecturerResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var lecturer = mapper.Map <LecturerResource, Lecturer>(lecturerResource); var major = await majorRepository.GetMajor(lecturerResource.MajorId); lecturer.Major = major; var user = new ApplicationUser { FullName = lecturer.Name, Major = lecturer.Major.MajorName, Avatar = "/assets/images/user.png", Email = lecturer.Email, UserName = lecturer.Email }; if (RoleExists("Lecturer")) { //Check Student Existence if (!LecturerExists(user.Email)) { var password = "******"; // Password Default await userManager.CreateAsync(user, password); await userManager.AddToRoleAsync(user, "Lecturer"); } else { ModelState.AddModelError("", "Email is registered"); } } else { ModelState.AddModelError("", "'Lecturer' role does not exist"); } lecturerRepository.AddLecturer(lecturer); await unitOfWork.Complete(); lecturer = await lecturerRepository.GetLecturer(lecturer.LecturerId); var result = mapper.Map <Lecturer, LecturerResource>(lecturer); return(Ok(result)); }
public void UpdateProjects(Lecturer lecturer, LecturerResource lecturerResource) { if (lecturerResource.Projects != null && lecturerResource.Projects.Count >= 0) { //remove old projects lecturer.Projects.Clear(); //add new projects var newProjects = context.Projects.Where(e => lecturerResource.Projects.Any(id => id == e.ProjectId)).ToList(); foreach (var a in newProjects) { lecturer.Projects.Add(a); } } }
public void UpdateGroups(Lecturer lecturer, LecturerResource lecturerResource) { if (lecturerResource.Groups != null && lecturerResource.Groups.Count >= 0) { //remove old groups lecturer.Groups.Clear(); //add new groups var newBoards = context.Groups.Where(e => lecturerResource.Groups.Any(id => id == e.GroupId)).ToList(); foreach (var a in newBoards) { lecturer.Groups.Add(a); } } }
public void UpdateBoardEnrollments(Lecturer lecturer, LecturerResource lecturerResource) { if (lecturerResource.BoardEnrollments != null && lecturerResource.BoardEnrollments.Count >= 0) { //remove old boardEnrollments lecturer.BoardEnrollments.Clear(); //add new enrollments var newBoardEnrollments = context.BoardEnrollments.Where(e => lecturerResource.BoardEnrollments.Any(id => id == e.BoardEnrollmentId)).ToList(); foreach (var a in newBoardEnrollments) { lecturer.BoardEnrollments.Add(a); } } }