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

136 lines
5.1 KiB
C#
Raw Permalink Normal View History

2021-05-19 17:56:56 +02:00
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.DTO;
2021-05-19 04:33:53 +02:00
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;
2021-05-19 17:56:56 +02:00
using Microsoft.AspNetCore.Identity;
2021-05-19 04:33:53 +02:00
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Mime;
2021-05-19 17:56:56 +02:00
using System.Security.Claims;
2021-05-19 04:33:53 +02:00
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;
2021-05-19 17:56:56 +02:00
private readonly UserManager<User> _userManager;
2021-05-19 04:33:53 +02:00
2021-05-19 17:56:56 +02:00
public WeeklyTimeSpanController(WeeklyTimeSpanService weeklytimespanService, UserManager<User> userManager)
2021-05-19 04:33:53 +02:00
{
_weeklytimespanService = weeklytimespanService;
2021-05-19 17:56:56 +02:00
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
2021-05-19 04:33:53 +02:00
}
[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
{
2021-05-19 17:56:56 +02:00
var obj = await _weeklytimespanService.AddWeeklyTimeSpan(this.User.FindFirst(ClaimTypes.NameIdentifier).Value,weeklytimespan);
2021-05-19 04:33:53 +02:00
return CreatedAtAction(nameof(GetWeeklyTimeSpanById), new { weeklytimespanId = obj.Id }, obj);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpDelete("{weeklyTimeSpanId}")]
2021-05-19 23:14:57 +02:00
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
2021-05-19 04:33:53 +02:00
[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]
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(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);
}
}
}
}