public async Task <int> AddCourseAsync(CreateCourseInputModel inputModel, string thumbnailUrl, string authorId) { var thumbnail = new CourseThumbnail() { Url = thumbnailUrl }; var newCourse = new Course() { Title = inputModel.Title, Price = inputModel.Price, Thumbnail = thumbnail, CategoryId = inputModel.CategoryId, LanguageId = inputModel.LanguageId, SubCategoryId = inputModel.SubcategoryId, Description = inputModel.Description, AuthorId = authorId, }; this.userRepository.All().Include(x => x.Lecturer).FirstOrDefault(x => x.Id == authorId).Lecturer.Courses.Add(newCourse); await this.courseRepository.AddAsync(newCourse); await this.courseRepository.SaveChangesAsync(); return(newCourse.Id); }
public ActionResult Create(CreateCourseInputModel model) { if (!this.ModelState.IsValid) { var username = this.User.Identity.Name; var schoolId = this.schoolService.GetSchoolByManagerName <SchoolViewModel>(username).Id; this.ViewBag.SchoolId = schoolId; this.ViewBag.Trainers = this.trainerService .TrainersBySchoolId <AvailableTrainerViewModel>( schoolId); this.ViewBag.Cars = this.carService.GetCarsBySchoolId <CarViewModel>(schoolId); return(this.View(model)); } try { var course = this.courseService.Create(model).GetAwaiter().GetResult(); return(this.RedirectToAction("Details", "Courses", new { Area = "", id = course.Id })); } catch (Exception e) { return(this.View("_Error", e.Message)); } }
public void Create_ValidInputModel_ReturnsCreatedCourse() { var course = new Course() { Id = 1, Category = Category.A, Description = "TestDesc", MinimumLessonsCount = 20, Price = 100, TrainerId = 1, CarId = 1, SchoolId = 1 }; this.repository.Setup(r => r.AddAsync(course)).Returns(Task.FromResult(course)); var model = new CreateCourseInputModel() { Category = Category.A, Description = "TestDesc", MinimumLessonsCount = 20, Price = 100, TrainerId = 1, CarId = 1, SchoolId = 1 }; var result = this.courseService.Create(model).GetAwaiter().GetResult(); Assert.That(result.TrainerId, Is.EqualTo(1)); Assert.That(result.Price, Is.EqualTo(100)); Assert.That(result, Is.TypeOf <Course>()); }
public async Task CreateCourseAsync(CreateCourseInputModel input, string userId) { string folderName = "course_images"; var inputPicture = input.Image; var pictureUrl = await this.cloudinaryService.UploadPhotoAsync(inputPicture, inputPicture.FileName, folderName); var coach = this.coachesService.GetCoachByUserId(userId); var course = new Course { Name = input.Name, PositionName = input.PositionName, StarDate = input.StartDate, EndDate = input.EndDate, Description = input.Description, Picture = new Picture { Url = pictureUrl }, CoachId = coach.Id, }; coach.Courses.Add(course); await this.coursesRepository.AddAsync(course); await this.coursesRepository.SaveChangesAsync(); }
public async Task CreateAsync(CreateCourseInputModel input) { Course course = new Course { Name = input.Name, Description = input.Description, Price = input.Price, SubjectId = input.SubjectId, StartDate = input.StartDate, EndDate = input.EndDate, }; string fileName = course.Name + Guid.NewGuid().ToString(); string remoteUrl = await this.UploadImageAsync(input.Image, fileName); File file = new File { Extension = System.IO.Path.GetExtension(input.Image.FileName), RemoteUrl = remoteUrl, CourseId = course.Id, UserId = input.UserId, }; await this.filesRepository.AddAsync(file); await this.filesRepository.SaveChangesAsync(); course.FileId = file.Id; await this.coursesRepository.AddAsync(course); await this.coursesRepository.SaveChangesAsync(); foreach (var tagId in input.Tags) { CourseTag courseTag = new CourseTag { CourseId = course.Id, TagId = tagId, }; await this.courseTagsRepository.AddAsync(courseTag); } await this.courseTagsRepository.SaveChangesAsync(); foreach (var lecturerId in input.Lecturers) { CourseLecturer courseLecturer = new CourseLecturer { CourseId = course.Id, LecturerId = lecturerId, }; await this.courseLecturersRepository.AddAsync(courseLecturer); } await this.courseLecturersRepository.SaveChangesAsync(); }
public async Task <IActionResult> Create() { var viewModel = new CreateCourseInputModel { CategoriesItems = await this.categoriesService.AllAsync <CategoriesItemsViewModel>(), }; return(this.View(viewModel)); }
public async Task <Course> Create(CreateCourseInputModel model) { var course = Mapper.Map <Course>(model); await this.courseRepository.AddAsync(course); await this.courseRepository.SaveChangesAsync(); return(course); }
public IActionResult Create() { var viewModel = new CreateCourseInputModel(); viewModel.CategoriesItems = this.categoriesService.GetAllAsSelectListItems(); viewModel.SubcategoriesItems = this.subcategoriesService.GetAllAsSelectListItems(); viewModel.LanguagesItems = this.languagesService.GetAllAsSelectListItems(); return(this.View(viewModel)); }
public IActionResult Create() { CreateCourseInputModel input = new CreateCourseInputModel { SubjectsItems = this.subjectsService.GetAllAsSelectListItems(), TagItems = this.tagsService.GetAllAsSelectListItems(), LecturerItems = this.lecturersService.GetAllAsSelectListItems(), }; return(this.View(input)); }
public async Task <IActionResult> Create(CreateCourseInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); await this.coursesService.CreateCourseAsync(input, user.Id); return(this.RedirectToAction("All")); }
public async Task <IActionResult> Create(CreateCourseInputModel model) { if (!this.ModelState.IsValid) { model.CategoriesItems = await this.categoriesService.AllAsync <CategoriesItemsViewModel>(); return(this.View(model)); } var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; await this.coursesService.CreateAsync(model.Title, model.Description, userId, model.CategoryId); return(this.RedirectToAction("MyResources", "Users")); }
public async Task <IActionResult> Create(CreateCourseInputModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } var resultUrl = await CloudinaryExtentsion.UploadAsync(this.cloudinary, inputModel.Thumbnail); var url = resultUrl; var userId = this.userManager.GetUserId(this.User); var newCourseId = await this.coursesService.AddCourseAsync(inputModel, url, userId); return(this.Redirect($"/Courses/GetCourse/{newCourseId}")); }
public async Task <IActionResult> Create(CreateCourseInputModel input) { if (!this.ModelState.IsValid) { input.SubjectsItems = this.subjectsService.GetAllAsSelectListItems(); input.TagItems = this.tagsService.GetAllAsSelectListItems(); input.LecturerItems = this.lecturersService.GetAllAsSelectListItems(); return(this.View(input)); } ApplicationUser user = await this.userManager.GetUserAsync(this.User); input.UserId = user.Id; await this.coursesService.CreateAsync(input); this.TempData["Message"] = "Course is created successfully!"; return(this.Redirect("/")); }
public async Task CreateCourseShouldCreateCourseSuccessfullyAndAddItToCoach() { var courses = new List <Course> { new Course { Name = "test", PositionName = PositionName.SmallForward, StarDate = DateTime.Now, EndDate = DateTime.Now, Description = "Test description", Picture = new Picture { Url = "testurl" }, Coach = new Coach { Id = "c1", Name = "Coach1", Description = "desc1", Experience = 2, Phone = "321312312", Email = "*****@*****.**", User = new ApplicationUser { Id = "coachuser" }, UserId = "coachuser", Picture = new Picture { Id = "cpic", Url = "test xurl" }, PictureId = "cpic", }, CoachId = "c1", }, new Course { Name = "test2", PositionName = PositionName.Center, StarDate = DateTime.Now, EndDate = DateTime.Now, Description = "Test description2", Picture = new Picture { Url = "testurl2" }, Coach = new Coach { Id = "c2", Name = "Coach2", Description = "desc2", Experience = 5, Phone = "3213123312", Email = "*****@*****.**", User = new ApplicationUser { Id = "coachuser2" }, UserId = "coachuser2", Picture = new Picture { Id = "cpic2", Url = "test xurl2" }, PictureId = "cpic2", }, CoachId = "c2", }, }; this.courseRepository.Setup(r => r.AllAsNoTracking()).Returns(() => courses.AsQueryable()); this.coachRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <Coach> { new Coach { Id = "c2", Name = "Coach2", Description = "desc2", Experience = 5, Phone = "3213123312", Email = "*****@*****.**", User = new ApplicationUser { Id = "coachuser2" }, UserId = "coachuser2", Picture = new Picture { Id = "cpic2", Url = "test xurl2" }, PictureId = "cpic2", }, }.AsQueryable()); // Arrange var fileMock = new Mock <IFormFile>(); // Setup mock file using a memory stream var content = "Hello World from a Fake File"; var fileName = "test.pdf"; var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(content); writer.Flush(); ms.Position = 0; fileMock.Setup(_ => _.OpenReadStream()).Returns(ms); fileMock.Setup(_ => _.FileName).Returns(fileName); fileMock.Setup(_ => _.Length).Returns(ms.Length); var file = fileMock.Object; this.courseRepository.Setup(r => r.AddAsync(It.IsAny <Course>())).Callback((Course course) => courses.Add(course)); var model = new CreateCourseInputModel { Name = "testcourse", Description = "test description for course", PositionName = PositionName.ShootingGuard, StartDate = DateTime.Now, EndDate = DateTime.Now, Image = file, }; await this.coursesService.CreateCourseAsync(model, "coachuser2"); Assert.Contains(courses, x => x.Name == "testcourse"); Assert.Equal(3, courses.Count); this.courseRepository.Verify(x => x.AllAsNoTracking(), Times.Never); }
public IActionResult Create() { var viewModel = new CreateCourseInputModel(); return(this.View(viewModel)); }