129 lines
4.3 KiB
C#
129 lines
4.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Project.Application.DTOs.Incoming;
|
|
using Project.Application.Interfaces;
|
|
|
|
namespace Project.Web.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
//[Authorize(Roles = "Admin")] // Authorize Admins only to use this controller
|
|
public class UserController : Controller
|
|
{
|
|
// FIELDS FOR CTOR
|
|
private readonly IUserService _userService;
|
|
|
|
// CTOR
|
|
public UserController(IUserService userService)
|
|
{
|
|
_userService = userService;
|
|
}
|
|
|
|
// CREATE
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateUser([FromBody] CreatingUserDto creatingUserDto)
|
|
{
|
|
// Validate incomming model
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
// Try to add user asynchronously
|
|
var result = await _userService.AddUserAsync(creatingUserDto);
|
|
|
|
// If user is successfully created, return a CreatedAtAction response with the created resource
|
|
if (result != null)
|
|
{
|
|
var id = result.Id;
|
|
var createdResource = new { Id = id };
|
|
var actionName = nameof(GetUserById);
|
|
var routeValue = new { id = createdResource.Id };
|
|
return CreatedAtAction(actionName, routeValue, createdResource);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Creation failed");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
// READ ALL
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetUsers()
|
|
{
|
|
var users = await _userService.GetUsersAsync();
|
|
return Ok(users);
|
|
}
|
|
|
|
// READ BY ID
|
|
[HttpGet("id/{id}", Name = "GetUserById")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetUserById(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid Id");
|
|
}
|
|
var user = await _userService.GetByIdAsync(id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(user);
|
|
}
|
|
|
|
// READ BY EMAIL
|
|
[HttpGet("email/{email}", Name = "GetUserByEmail")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetUserByEmail(string email)
|
|
{
|
|
if (string.IsNullOrEmpty(email))
|
|
{
|
|
return BadRequest("Email connot be empty");
|
|
}
|
|
var user = await _userService.GetByEmailAsync(email);
|
|
if(user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(user);
|
|
}
|
|
|
|
// UPDATE
|
|
[HttpPut("id/{id}", Name = "UpdateUser")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> UpdateUser(int id, UpdatingUserDto updatingUserDto)
|
|
{
|
|
var updated = await _userService.UpdateUserAsync(updatingUserDto);
|
|
return Ok(updated);
|
|
}
|
|
|
|
// DELETE
|
|
[HttpDelete("id/{id}", Name = "DeleteUser")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> DeleteProduct([FromBody] int id)
|
|
{
|
|
await _userService.DeleteUserAsync(id);
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|