hanyadikhetvan-dotnethf/HanyadikHetVan/Controllers/V2/PauseController.cs

102 lines
3.4 KiB
C#
Raw Permalink Normal View History

2021-05-19 04:33:53 +02:00
using HanyadikHetVan.DTO;
using HanyadikHetVan.Services;
2021-05-19 23:14:57 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2021-05-19 04:33:53 +02:00
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;
2021-05-20 01:36:54 +02:00
using System.Security.Claims;
2021-05-19 04:33:53 +02:00
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)]
2021-05-20 01:36:54 +02:00
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(PauseDTO))]
2021-05-19 04:33:53 +02:00
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> AddPause([FromBody] PauseDTO pause)
{
try
{
2021-05-20 01:36:54 +02:00
var obj = await _pauseService.AddPause(this.User.FindFirst(ClaimTypes.NameIdentifier).Value, pause);
2021-05-19 04:33:53 +02:00
return CreatedAtAction(nameof(GetPauseById),new { pauseId = obj.Id } ,obj);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpDelete("{pauseId}")]
2021-05-19 23:14:57 +02:00
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
2021-05-20 01:36:54 +02:00
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))]
2021-05-19 04:33:53 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> DeletePause(int pauseId)
{
var result = await _pauseService.DeletePause(pauseId);
if (result)
{
return Ok(result);
}
else
{
return NotFound();
}
}
[HttpPut]
2021-05-19 23:14:57 +02:00
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
2021-05-19 04:33:53 +02:00
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> UpdatePause([FromBody] PauseDTO pause)
{
try
{
var obj = await _pauseService.UpdatePause(pause);
return Ok(obj);
}
catch (Exception)
{
return NotFound();
}
}
[HttpGet]
public async Task<List<PauseDTO>> GetAllPauses()
{
return await _pauseService.GetAllPauses();
}
[HttpGet("{pauseId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetPauseById(int pauseId)
{
var pause = await _pauseService.GetPause(pauseId);
if (pause == null)
{
return NotFound();
}
else
{
return Ok(pause);
}
}
}
}