示例#1
0
        public async Task <ActionResult <Exercise> > Create(ExerciseRequest exercise)
        {
            var result = await _exercises.Create(exercise, HttpContext.RequestAborted);

            if (result == null)
            {
                return(Conflict());
            }

            return(result);
        }
示例#2
0
        public IActionResult Create([FromBody] ExerciseRequest request)
        {
            var exercise = AutoMapper.Mapper.Map <ExerciseDetails>(request);

            exercise.UserId = CurrentUserId;
            if (exercise.Targets != null && exercise.SecondaryTargets != null)
            {
                exercise.SecondaryTargets = exercise.SecondaryTargets.Except(exercise.Targets).ToArray();
            }
            trainingRepository.CreateExercise(exercise);

            var response = AutoMapper.Mapper.Map <ExerciseDetailsResponse>(exercise);

            return(Ok(response));
        }
示例#3
0
        public IActionResult Update(Guid id, [FromBody] ExerciseRequest request)
        {
            var exercise = trainingRepository.GetExercise(id, CurrentUserId, DateTimeOffset.MinValue);

            if (exercise.UserId != CurrentUserId)
            {
                return(Unauthorized());
            }

            AutoMapper.Mapper.Map(request, exercise);
            if (exercise.Targets != null && exercise.SecondaryTargets != null)
            {
                exercise.SecondaryTargets = exercise.SecondaryTargets.Except(exercise.Targets).ToArray();
            }
            trainingRepository.UpdateExercise(exercise);

            var response = AutoMapper.Mapper.Map <ExerciseDetailsResponse>(exercise);

            return(Ok(response));
        }
示例#4
0
        public async Task <ActionResult <Exercise> > Update(Guid id, [CustomizeValidator(RuleSet = "Update")] ExerciseRequest exercise)
        {
            var currentUser     = HttpContext.GetUser();
            var currentExercise = await _exercises.Get(id, HttpContext.RequestAborted);

            if (currentExercise == null)
            {
                return(NotFound());
            }

            if (!await _authorization.HasWriteAccess(currentUser, currentExercise, HttpContext.RequestAborted))
            {
                return(Unauthorized());
            }

            var result = await _exercises.Update(id, exercise, HttpContext.RequestAborted);

            if (result == null)
            {
                return(Conflict());
            }

            return(result);
        }
示例#5
0
        public async Task Update_Subject_State_On_Create(Mock <IAuthorizationService> authorizationService, Mock <IStateService> stateService, ExerciseRequest request)
        {
            stateService.Setup(x => x.UpdateSubjectState(It.IsAny <Guid>(), It.IsAny <CancellationToken>()));

            var context     = TestSetup.SetupContext();
            var httpContext = TestSetup.SetupHttpContext();

            var service = new ExerciseService(context, httpContext, authorizationService.Object, stateService.Object);
            await service.Create(request);

            stateService.VerifyAll();
        }
        public async Task Update_Exercise_Or_404(Mock <IExerciseService> service, Mock <IAuthorizationService> authorization, ExerciseRequest validRequest, ExerciseRequest invalidRequest, Exercise valid, Exercise invalid, User user)
        {
            authorization.Setup(x => x.HasWriteAccess(user, It.IsAny <It.IsAnyType>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);

            service.Setup(x => x.Exists(valid.Id, It.IsAny <CancellationToken>())).ReturnsAsync(true);
            service.Setup(x => x.Exists(invalid.Id, It.IsAny <CancellationToken>())).ReturnsAsync(false);
            service.Setup(x => x.Get(valid.Id, It.IsAny <CancellationToken>())).ReturnsAsync(valid);
            service.Setup(x => x.Get(invalid.Id, It.IsAny <CancellationToken>())).ReturnsAsync((Exercise)null);
            service.Setup(x => x.Update(valid.Id, validRequest, It.IsAny <CancellationToken>())).ReturnsAsync(valid);
            service.Setup(x => x.Update(invalid.Id, invalidRequest, It.IsAny <CancellationToken>())).ReturnsAsync((Exercise)null);

            var controller = TestSetup.SetupController <ExercisesController>(service.Object, authorization.Object).SetupSession(user);

            var goodResult = await controller.Update(valid.Id, validRequest);

            var invalidResult = await controller.Update(invalid.Id, invalidRequest);

            goodResult.Value.Should().BeEquivalentTo(valid);
            invalidResult.Value.Should().BeNull();
            invalidResult.Result.Should().BeOfType <NotFoundResult>();
        }
        public async Task Create_Exercise_Or_409(Mock <IExerciseService> service, IAuthorizationService authorization, ExerciseRequest request, Exercise exercise, User user)
        {
            service.Setup(x => x.Create(request, It.IsAny <CancellationToken>())).ReturnsAsync(exercise);
            service.Setup(x => x.Create(null, It.IsAny <CancellationToken>())).ReturnsAsync((Exercise)null);

            var controller = TestSetup.SetupController <ExercisesController>(service.Object, authorization).SetupSession(user);

            var goodResult = await controller.Create(request);

            var invalidResult = await controller.Create(null);

            goodResult.Value.Should().NotBeNull();
            invalidResult.Value.Should().BeNull();
            invalidResult.Result.Should().BeOfType <ConflictResult>();
        }
示例#8
0
 public Task <ExerciseResponse> UpdateExercise(ExerciseRequest signRequest)
 {
     throw new NotImplementedException();
 }