﻿using LearnWordsFast.API.Exceptions;
using LearnWordsFast.API.Services;
using LearnWordsFast.API.ViewModels.TrainingController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace LearnWordsFast.API.Controllers
{
    [Route("api/Training")]
    [Authorize]
    public class TrainingController : ApiController
    {
        private readonly ITrainingService _trainingService;
        private readonly ILogger<TrainingController> _log;

        public TrainingController(
            ITrainingService trainingService,
            ILogger<TrainingController> log)
        {
            _trainingService = trainingService;
            _log = log;
        }

        [HttpGet]
        public IActionResult Get()
        {
            _log.LogInformation("Get next training");
            var training = _trainingService.CreateTraining(UserId);
            if (training == null)
            {
                return NotFound();
            }

            return Ok(training);
        }

        [HttpPost]
        public IActionResult Finish([FromBody]TrainingResultViewModel[] results)
        {
            _log.LogInformation("Finish training");
            foreach (var result in results)
            {
                try
                {
                    _trainingService.FinishTraining(UserId, result);
                }
                catch (NotFoundException)
                {
                    return NotFound(result.WordId);
                }
            }
            
            return Ok();
        }
    }
}
﻿using LearnWordsFast.API.Exceptions;
using LearnWordsFast.API.Services;
using LearnWordsFast.API.ViewModels.TrainingController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace LearnWordsFast.API.Controllers
{
    [Route("api/Training")]
    [Authorize("Bearer")]
    public class TrainingController : ApiController
    {
        private readonly ITrainingService _trainingService;
        private readonly ILogger<TrainingController> _log;

        public TrainingController(
            ITrainingService trainingService,
            ILogger<TrainingController> log)
        {
            _trainingService = trainingService;
            _log = log;
        }

        [HttpGet]
        public IActionResult Get()
        {
            _log.LogInformation("Get next training");
            var training = _trainingService.CreateTraining(UserId);
            if (training == null)
            {
                return NotFound();
            }

            return Ok(training);
        }

        [HttpPost]
        public IActionResult Finish([FromBody]TrainingResultViewModel[] results)
        {
            _log.LogInformation("Finish training");
            foreach (var result in results)
            {
                try
                {
                    _trainingService.FinishTraining(UserId, result);
                }
                catch (NotFoundException)
                {
                    return NotFound(result.WordId);
                }
            }
            
            return Ok();
        }
    }
}
Update Authorize attribute to use Bearer policy
Update Authorize attribute to use Bearer policy
