public async Task CreateAsync(CreateWorkoutInputModel input, string userId) { string folderName = "workout_images"; var inputPicture = input.Image; var pictureUrl = await this.cloudinaryService.UploadPhotoAsync(inputPicture, inputPicture.FileName, folderName); var position = this.positionsService.GetPositionByName(input.PositionName); var coach = this.coachesService.GetCoachByUserId(userId); var workout = new Workout { Name = input.Name, Description = input.Description, VideoUrl = input.VideoUrl, PositionId = position.Id, Picture = new Picture { Url = pictureUrl }, CoachId = coach.Id, }; coach.CoachWorkouts.Add(workout); await this.workoutsRepository.AddAsync(workout); await this.workoutsRepository.SaveChangesAsync(); }
public async Task <IActionResult> Create(CreateWorkoutInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); await this.workoutsService.CreateAsync(input, user.Id); return(this.RedirectToAction("All")); }
public async Task <IActionResult> AddWorkout(CreateWorkoutInputModel input) { var nameAlreadyExists = this.workoutsService.WorkoutNameAlreadyExists(input.Name); if (!this.ModelState.IsValid || nameAlreadyExists) { if (nameAlreadyExists) { this.TempData["error"] = string.Format("Workout with name \"{0}\" already exists! Please, choose a different name!", input.Name); } return(this.View(input)); } var loggedInUser = await this.GetLoggedInUserAsync(); await this.workoutsService.CreateWorkoutAsync(input.Name, input.Difficulty, input.WorkoutType, input.Description, loggedInUser); return(this.RedirectToAction(nameof(this.CurrentWorkout))); }
public IActionResult Create() { var viewModel = new CreateWorkoutInputModel(); return(this.View(viewModel)); }
public IActionResult AddWorkout() { var model = new CreateWorkoutInputModel(); return(this.View(model)); }
public async Task AddWorkoutShouldCreateWorkoutAndAddItToCoach() { var workouts = new List <Workout> { new Workout { Id = "workoutId123", Name = "Workout One", Description = "Some kind of workout description", Position = new Position { Id = "PositionOne", Name = PositionName.ShootingGuard, Description = "Shooting guard position", Playstyle = "You play like a shooting guard", }, VideoUrl = "test youtube link", PositionId = "PositionOne", Picture = new Picture { Id = "pic", Url = "test url" }, PictureId = "pic", ImageUrl = "testimg", AddedByCoach = 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", }, }, }; this.positionsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <Position> { new Position { Id = "PositionOne", Name = PositionName.ShootingGuard, Description = "Shooting guard position", Playstyle = "You play like a shooting guard", }, }.AsQueryable()); var positionsService = new PositionsService(this.positionsRepository.Object); this.coachRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <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", }, }.AsQueryable()); var coachesService = new CoachesService(this.coachRepository.Object, this.moqCloudinaryService.Object); this.workoutsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => workouts.AsQueryable()); var service = new WorkoutsService(this.moqCloudinaryService.Object, this.workoutsRepository.Object, positionsService, coachesService); // 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.workoutsRepository.Setup(r => r.AddAsync(It.IsAny <Workout>())).Callback((Workout workout) => workouts.Add(workout)); var inputModel = new CreateWorkoutInputModel { Name = "Workout for beginners", Description = "This workout is for beginners and it will help them", PositionName = PositionName.ShootingGuard, VideoUrl = "testvideourl", Image = file, }; await service.CreateAsync(inputModel, "coachuser"); Assert.Contains(workouts, x => x.Name == "Workout for beginners"); Assert.Equal(2, workouts.Count); this.workoutsRepository.Verify(x => x.AllAsNoTracking(), Times.Never); }