public async Task <IActionResult> AddCourse([FromBody] CourseAddViewModel courseAdd) { var addCourse = _mapper.Map <CourseAddDTO>(courseAdd); await _courseService.AddCourse(addCourse); return(Ok("Added")); }
public async Task <ActionResult> Edit(CourseAddViewModel model) { if (ModelState.IsValid) { var file = model.Image; var course = await db.Courses.FindAsync(model.Id); course.Category = await db.CourseCategories.FindAsync(model.CategoryId); course.Desc = model.Desc; course.Name = model.Name; course.Price = model.Price; int id = course.Id; var path = AddCourseFile(file, "thumbnail", null, id); if (path != null) { course.ImagePath = path; } await db.SaveChangesAsync(); return(RedirectToAction("View", "Course", new { id = id })); } model.CourseCategoryList = db.CourseCategories.ToList(); return(View(model)); }
public async Task <IActionResult> Create(CourseAddViewModel viewModel) { if (ModelState.IsValid) { var teacher = _userManager.Users.FirstOrDefault(u => u.Id == viewModel.TeacherId); Course course = _mapper.Map <Course>(viewModel); List <User> participants = new List <User>(); if (!(teacher is null)) { participants.Add(teacher); } course.Users = participants; await _context.Courses.AddAsync(course); _context.SaveChanges(); TempData["AlertMsg"] = "Course added"; return(RedirectToAction(nameof(Details), new { course.Id })); } else { var teachers = await _userManager.GetUsersInRoleAsync("Teacher"); viewModel.Teachers = teachers.Select(t => new Tuple <string, string>(t.Id, t.UserName)).ToList(); } return(View(viewModel)); }
public async Task <IActionResult> Edit(CourseAddViewModel viewModel) { if (ModelState.IsValid) { var course = _context.Courses.Include(c => c.Users).FirstOrDefault(c => c.Id == viewModel.Id); var teacher = _userManager.Users.FirstOrDefault(u => u.Id == viewModel.TeacherId); course.EndDate = viewModel.EndDate; course.StartDate = viewModel.StartDate; course.Name = viewModel.Name; course.Description = viewModel.Description; var participants = course.Users; //List<User> participants = new List<User>(); if (!(teacher is null)) { var teachers = _userManager.GetUsersInRoleAsync("Teacher"); teachers.Wait(); participants = participants.Except(teachers.Result).Append(teacher).ToList(); } course.Users = participants; var modulesOutSideStartEndDate = await GetModulesOutSideCourseStartEndDates(course); if (modulesOutSideStartEndDate.Count() > 0) { var errorCount = 0; foreach (var module in modulesOutSideStartEndDate) { ModelState.AddModelError($"module_start_end_error_{errorCount++}", $"Module: {module.Name} {module.StartDate.ToString(Common.DateFormat)} - {module.EndDate.ToString(Common.DateFormat)} is outside course Start/End dates"); } return(View(viewModel)); } _context.SaveChanges(); TempData["AlertMsg"] = "Saved changes"; return(RedirectToAction(nameof(Details), new { course.Id })); } else { var teachers = await _userManager.GetUsersInRoleAsync("Teacher"); viewModel.Teachers = teachers.Select(t => new Tuple <string, string>(t.Id, t.UserName)).ToList(); } return(View(viewModel)); }
public ActionResult <CourseAddViewModel> DeleteCourse(CourseAddViewModel courseAddViewModel) { CourseAddViewModel courseDelete = new CourseAddViewModel(); try { courseDelete = _courseManagerService.DeleteCourse(courseAddViewModel); } catch (Exception es) { courseDelete._failure = true; courseDelete._message = es.Message; } return(courseDelete); }
public async Task <IActionResult> Create() { var teachers = await _userManager.GetUsersInRoleAsync("Teacher"); var students = await _userManager.GetUsersInRoleAsync("Student"); var startDate = DateTime.Now; CourseAddViewModel viewModel = new CourseAddViewModel { Teachers = teachers.Select(t => new Tuple <string, string>(t.Id, t.UserName)).ToList(), StartDate = startDate, EndDate = startDate.AddMonths(1) }; return(View(viewModel)); }
public IActionResult Edit(int id) { var course = _context.Courses.Include(c => c.Users).FirstOrDefault(c => c.Id == id); var teachers = _userManager.GetUsersInRoleAsync("Teacher"); teachers.Wait(); if (course is null) { return(NotFound()); } var viewModel = new CourseAddViewModel() { Id = course.Id, Name = course.Name, Description = course.Description, StartDate = course.StartDate, EndDate = course.EndDate, }; var teacher = _userManager.GetUsersInRoleAsync("Teacher"); teacher.Wait(); var theTeacher = course.Users.Intersect(teacher.Result); if (theTeacher.Count() < 1) { theTeacher = new List <User>() { new User() { Email = "not assigned" } }; } viewModel.Teachers = teachers.Result.Select(t => new Tuple <string, string>(t.Id, t.UserName)).ToList(); viewModel.TeacherId = theTeacher.FirstOrDefault().Id; return(View(viewModel)); }
/// <summary> /// Delete Course /// </summary> /// <param name="courseAddViewModel"></param> /// <returns></returns> public CourseAddViewModel DeleteCourse(CourseAddViewModel courseAddViewModel) { CourseAddViewModel courseDelete = new CourseAddViewModel(); try { if (TokenManager.CheckToken(courseAddViewModel._tenantName + courseAddViewModel._userName, courseAddViewModel._token)) { courseDelete = this.courseManagerRepository.DeleteCourse(courseAddViewModel); } else { courseDelete._failure = true; courseDelete._message = TOKENINVALID; } } catch (Exception es) { courseDelete._failure = true; courseDelete._message = es.Message; } return(courseDelete); }