hanyadikhetvan-dotnethf/HanyadikHetVan/Controllers/V2/WeeklyTimeSpanController.cs
Torma Kristóf beab15a7ef
All checks were successful
continuous-integration/drone/push Build is passing
everything works now
2021-05-19 23:14:57 +02:00

136 lines
5.1 KiB
C#

using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.DTO;
using HanyadikHetVan.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Security.Claims;
using System.Threading.Tasks;
namespace HanyadikHetVan.Controllers.V2
{
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class WeeklyTimeSpanController: Controller
{
private readonly WeeklyTimeSpanService _weeklytimespanService;
private readonly UserManager<User> _userManager;
public WeeklyTimeSpanController(WeeklyTimeSpanService weeklytimespanService, UserManager<User> userManager)
{
_weeklytimespanService = weeklytimespanService;
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
}
[HttpPost]
[Authorize]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(WeeklyTimeSpanDTO))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> AddWeeklyTimeSpan([FromBody] WeeklyTimeSpanDTO weeklytimespan)
{
try
{
var obj = await _weeklytimespanService.AddWeeklyTimeSpan(this.User.FindFirst(ClaimTypes.NameIdentifier).Value,weeklytimespan);
return CreatedAtAction(nameof(GetWeeklyTimeSpanById), new { weeklytimespanId = obj.Id }, obj);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpDelete("{weeklyTimeSpanId}")]
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> DeleteWeeklyTimeSpan(int weeklyTimeSpanId)
{
var result = await _weeklytimespanService.DeleteWeeklyTimeSpan(weeklyTimeSpanId);
if (result)
{
return Ok(result);
}
else
{
return NotFound();
}
}
[HttpPut]
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeeklyTimeSpanDTO))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> UpdateWeeklyTimeSpan([FromBody] WeeklyTimeSpanDTO weeklytimespan)
{
try
{
var wts = await _weeklytimespanService.UpdateWeeklyTimeSpan(weeklytimespan);
return Ok(wts);
}
catch (Exception)
{
return NotFound();
}
}
[HttpGet("date/{startTime}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeeklyTimeSpanDTO))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetAllWeeklyTimeSpanByStartdate(DateTime startTime)
{
var wts = await _weeklytimespanService.GetWeeklyTimeSpanByStartdate(startTime);
if (wts == null)
{
return NotFound();
}
else
{
return Ok(wts);
}
}
[HttpGet]
public async Task<List<WeeklyTimeSpanDTO>> GetAllWeeklyTimespans()
{
return await _weeklytimespanService.GetAllWeeklyTimeSpans();
}
[HttpGet("{weeklytimespanId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeeklyTimeSpanDTO))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetWeeklyTimeSpanById(int weeklytimespanId)
{
var wts = await _weeklytimespanService.GetWeeklyTimeSpan(weeklytimespanId);
if (wts == null)
{
return NotFound();
}
else
{
return Ok(wts);
}
}
[HttpGet("pauses/{weeklytimespanId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<PauseDTO>))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetAllPausesOfTimeSpan(int weeklytimespanId)
{
var wts = await _weeklytimespanService.GetPausesOfWeeklyTimeSpan(weeklytimespanId);
if (wts == null)
{
return NotFound();
}
else
{
return Ok(wts);
}
}
}
}