public async Task <IActionResult> Create([FromBody] CreateExerciseRequest request)
        {
            var newExercise     = _mapper.Map <Exercise>(request);
            var createdExercise = await _exerciseService.CreateExerciseAsync(newExercise);

            if (createdExercise.Id == 0)
            {
                return(BadRequest(new ErrorResponse("Wystąpił błąd podczas dodawania")));
            }

            return(CreatedAtAction(nameof(GetById),
                                   new { exerciseId = createdExercise.Id },
                                   _mapper.Map <ExerciseResponse>(createdExercise)));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateExercise(CreateExerciseRequestModel model)
        {
            try
            {
                model.FileContentType = model.Image.ContentType;

                string accessToken = await HttpContext.GetTokenAsync("access_token");

                ViewBag.Result = await _exerciseService.CreateExerciseAsync(model, accessToken);
            }
            catch (Exception)
            {
                JObject error = new();

                error.Add("succeeded", false);
                error.Add("title", "Internal Error");
            }

            return(View());
        }
Beispiel #3
0
        public async Task <IActionResult> CreateExerciseAsync(Exercise exerciseData, string returnToForm)
        {
            // Check input
            if (!ModelState.IsValid)
            {
                AddStatusMessage(_localizer["CreateExerciseAsync:InvalidInput"], StatusMessageTypes.Error);
                return(await ShowCreateExerciseFormAsync(exerciseData.LabId, exerciseData));
            }

            try
            {
                // Create exercise
                var exercise = new Exercise
                {
                    LabId               = exerciseData.LabId,
                    ExerciseNumber      = exerciseData.ExerciseNumber,
                    Name                = exerciseData.Name,
                    IsMandatory         = exerciseData.IsMandatory,
                    IsPreStartAvailable = exerciseData.IsPreStartAvailable,
                    BasePoints          = exerciseData.BasePoints,
                    PenaltyPoints       = exerciseData.PenaltyPoints
                };
                await _exerciseService.CreateExerciseAsync(exercise, HttpContext.RequestAborted);

                AddStatusMessage(_localizer["CreateExerciseAsync:Success"], StatusMessageTypes.Success);

                if (!string.IsNullOrEmpty(returnToForm))
                {
                    return(await ShowCreateExerciseFormAsync(exerciseData.LabId, exerciseData));
                }
                return(await RenderExerciseListAsync(exerciseData.LabId));
            }
            catch (InvalidOperationException ex)
            {
                _logger.LogError(ex, "Create exercise");
                AddStatusMessage(_localizer["CreateExerciseAsync:UnknownError"], StatusMessageTypes.Error);
                return(await ShowCreateExerciseFormAsync(exerciseData.LabId, exerciseData));
            }
        }