89 lines
2.5 KiB
C#
89 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Flexitime.Objects;
|
|
using Flexitime.Objects.API;
|
|
using FlexitimeAPI.Exceptions;
|
|
using FlexitimeAPI.Helpers;
|
|
using FlexitimeAPI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Microsoft.AspNetCore.Server.IIS;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace FlexitimeAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly ILogger<UsersController> _logger;
|
|
private readonly IUserService _userService;
|
|
|
|
public UsersController(ILogger<UsersController> logger, IUserService userService)
|
|
{
|
|
_logger = logger;
|
|
_userService = userService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
var userList = await _userService.GetAll();
|
|
|
|
if (!userList.Any())
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
return Ok(userList);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet]
|
|
[Route("{id}")]
|
|
public async Task<IActionResult> Get(Guid id)
|
|
{
|
|
var user = await _userService.GetById(id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok(user);
|
|
}
|
|
|
|
[Authorize("s.u.e")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create([FromBody] User newUser)
|
|
{
|
|
try
|
|
{
|
|
var createdUser = await _userService.Add(newUser);
|
|
if (createdUser == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
return CreatedAtAction(nameof(Get), createdUser.Id);
|
|
}
|
|
catch (InvalidUserNameException iuex)
|
|
{
|
|
var modelStateDictionary = new ModelStateDictionary();
|
|
modelStateDictionary.AddModelError(nameof(Flexitime.Objects.User.UserName), iuex.Message);
|
|
|
|
return BadRequest(modelStateDictionary);
|
|
}
|
|
}
|
|
|
|
[Authorize("s.u.e")]
|
|
[HttpPost]
|
|
[Route("usernamevalid")]
|
|
public Task<IActionResult> UsernameValid([FromBody] UsernameValidCheckRequest usernameValidCheck)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
} |