using HanyadikHetVan.DTO; using HanyadikHetVan.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Net.Mime; using System.Threading.Tasks; namespace HanyadikHetVan.Controllers.V2 { [ApiVersion("2.0")] [Route("api/v{version:apiVersion}/[controller]")] [ApiController] public class PauseController : Controller { private readonly PauseService _pauseService; public PauseController(PauseService pauseService) { _pauseService = pauseService; } [HttpPost] [Authorize] [Consumes(MediaTypeNames.Application.Json)] [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(WeeklyTimeSpanDTO))] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task AddPause([FromBody] PauseDTO pause) { try { var obj = await _pauseService.AddPause(pause); return CreatedAtAction(nameof(GetPauseById),new { pauseId = obj.Id } ,obj); } catch (Exception) { return BadRequest(); } } [HttpDelete("{pauseId}")] [Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeeklyTimeSpanDTO))] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task DeletePause(int pauseId) { var result = await _pauseService.DeletePause(pauseId); if (result) { return Ok(result); } else { return NotFound(); } } [HttpPut] [Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [Consumes(MediaTypeNames.Application.Json)] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task UpdatePause([FromBody] PauseDTO pause) { try { var obj = await _pauseService.UpdatePause(pause); return Ok(obj); } catch (Exception) { return NotFound(); } } [HttpGet] public async Task> GetAllPauses() { return await _pauseService.GetAllPauses(); } [HttpGet("{pauseId}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetPauseById(int pauseId) { var pause = await _pauseService.GetPause(pauseId); if (pause == null) { return NotFound(); } else { return Ok(pause); } } } }