public async Task <IActionResult> CreateProject([FromBody] ProjectResource projectResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var project = mapper.Map <ProjectResource, Project> (projectResource); var major = await majorRepository.GetMajor(projectResource.MajorId); project.Major = major; var lecturer = await lecturerRepository.GetLecturer(projectResource.LecturerId); project.Lecturer = lecturer; projectRepository.AddProject(project); projectRepository.UpdateGroups(project, projectResource); projectRepository.UpdateTagProjects(project, projectResource); //Add categories into projects var categories = GetCategoriesFromDescription(projectResource.Description); await projectRepository.UpdateCategories(project, categories); await unitOfWork.Complete(); project = await projectRepository.GetProject(project.ProjectId); var result = mapper.Map <Project, ProjectResource> (project); return(Ok(result)); }
public async Task <IActionResult> GetMajor(int id) { var major = await repository.GetMajor(id); if (major == null) { return(NotFound()); } var majorResource = mapper.Map <Major, MajorResource>(major); return(Ok(majorResource)); }
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 async Task <IActionResult> CreateStudent([FromBody] StudentResource studentResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var student = mapper.Map <StudentResource, Student>(studentResource); var major = await majorRepository.GetMajor(studentResource.MajorId); student.Major = major; var user = new ApplicationUser { FullName = student.Name, Email = student.Email, Avatar = "/assets/images/user.png", Major = student.Major.MajorName, UserName = student.Email }; if (RoleExists("Student")) { //Check Student Existence if (!StudentExists(user.Email) && !StudentIdExists(student.StudentCode)) { var password = student.StudentCode.ToString(); // Password Default await userManager.CreateAsync(user, password); await userManager.AddToRoleAsync(user, "Student"); } } studentRepository.AddStudent(student); await unitOfWork.Complete(); student = await studentRepository.GetStudent(student.Id); await hubContext.Clients.All.InvokeAsync("LoadData"); var result = mapper.Map <Student, StudentResource>(student); return(Ok(result)); }
public async Task <IActionResult> CreateMajor([FromBody] MajorResource majorResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var major = mapper.Map <MajorResource, Major>(majorResource); repository.AddMajor(major); await unitOfWork.Complete(); major = await repository.GetMajor(major.MajorId); var result = mapper.Map <Major, MajorResource>(major); return(Ok(result)); }
public IActionResult Edit(int id) { var major = majorRepository.GetMajor(id); var editMajor = new UpdateMajor(); if (major != null) { editMajor.MajorId = major.MajorId; editMajor.MajorName = major.MajorName; editMajor.Email = major.Email; editMajor.PhoneNumber = major.PhoneNumber; editMajor.FoundedYear = (int)major.FoundedYear; } return(View(editMajor)); }
public MajorViewModel GetMajor(int id) { return(MapToMajorViewModel(_repository.GetMajor(id))); }
public IActionResult Get(int majorId) { var major = _majorRepository.GetMajor(majorId); return(Ok(major)); }
public IEnumerable <Major> Get() { return(_repo.GetMajor()); }
public async Task <IActionResult> CreateGroup([FromBody] GroupResource groupResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //case: lecturer, project and major is not the same // var checkGroup = await groupRepository.CheckGroup(groupResource); // if (!checkGroup) // { // ModelState.AddModelError("Error", "Stop hacking. Please set the Lecturer, project and group in the same Major."); // return BadRequest(ModelState); // } var group = mapper.Map <GroupResource, Group>(groupResource); group.Lecturer = await lecturerRepository.GetLecturerByEmail(groupResource.LecturerEmail); if (groupResource.ProjectId == null) { var otherProject = mapper.Map <ProjectResource, Project>(groupResource.OtherProject); group.Project = otherProject; } else { group.Project = await projectRepository.GetProject(groupResource.ProjectId); } var major = await majorRepository.GetMajor(groupResource.MajorId); group.Major = major; var quarter = await quarterRepository.GetQuarter(groupResource.QuarterId); group.Quarter = quarter; groupRepository.UpdateEnrollments(group, groupResource); groupRepository.UpdateUploadedFiles(group, groupResource); groupRepository.AddGroup(group); await unitOfWork.Complete(); group = await groupRepository.GetGroup(group.GroupId); //foreach (var enrollmentResource in groupResource.Enrollments) //{ // var enrollment = await enrollmentRepository.GetEnrollment(enrollmentResource.EnrollmentId); // //case: enrollment's type and project's type is different and the student has been already in group // if (group != null && group.Project.Type != enrollment.Type) // { // ModelState.AddModelError("Error", "Enrollment's type and Project Type of Group are not the same."); // groupRepository.RemoveGroup(group); // await unitOfWork.Complete(); // return BadRequest(ModelState); // } // else if (group != null && !groupRepository.CheckEnrollment(group, enrollment)) // { // ModelState.AddModelError("Warning", "This group already has this student."); // groupRepository.RemoveGroup(group); // await unitOfWork.Complete(); // return BadRequest(ModelState); // } // else // { // enrollment.Group = group; // group.Enrollments.Add(enrollment); // await unitOfWork.Complete(); // } //} var result = mapper.Map <Group, GroupResource>(group); return(Ok(result)); }