93 lines
2.5 KiB
C#

namespace ActiveAllocator.API.Controllers;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ActiveAllocator.API.Models.Users;
using ActiveAllocator.API.Services;
using ActiveAllocator.API.Authorization;
using ActiveAllocator.API.Helpers;
using System;
[Authorize]
[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{
private IUserService _userService;
private IMapper _mapper;
private readonly AppSettings _appSettings;
private readonly ILogger<UsersController> _logger;
public UsersController(
IUserService userService,
IMapper mapper,
IOptions<AppSettings> appSettings,
ILogger<UsersController> logger)
{
_userService = userService;
_mapper = mapper;
_appSettings = appSettings.Value;
_logger = logger;
}
[AllowAnonymous]
[HttpPost("authenticate")]
public IActionResult Authenticate(AuthenticateRequest model)
{
var response = _userService.Authenticate(model);
return Ok(response);
}
[AllowAnonymous]
[HttpPost("register")]
public IActionResult Register(RegisterRequest model)
{
_userService.Register(model);
return Ok(new { message = "Registration successful" });
}
[HttpGet]
public IActionResult GetAll()
{
var users = _userService.GetAll();
return Ok(users);
}
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var user = _userService.GetById(id);
return Ok(user);
}
[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody]UserUpdateRequest model)
{
_userService.Update(id, model);
return Ok(new { message = "User updated" });
}
/*
[HttpPut("{id}")]
public IActionResult Update(int id, string Email = null, string FirstName = null, string LastName = null, string Username = null, string Role = null, string Password = null)
{
UserUpdateRequest model = new UserUpdateRequest() {
Email = Email,
FirstName = FirstName,
LastName = LastName,
Username = Username,
Role = Role,
Password = Password
};
_userService.Update(id, model);
return Ok(new { message = "User updated" });
}*/
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
_userService.Delete(id);
return Ok(new { message = "User deleted" });
}
}