| 1 | using ChapterX.Application.User.Commands;
|
|---|
| 2 | using ChapterX.Application.User.Queries;
|
|---|
| 3 | using MediatR;
|
|---|
| 4 | using Microsoft.AspNetCore.Authorization;
|
|---|
| 5 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 6 | using Microsoft.Extensions.Logging;
|
|---|
| 7 |
|
|---|
| 8 | namespace ChapterX.API.Controllers
|
|---|
| 9 | {
|
|---|
| 10 | [Route("api/[controller]")]
|
|---|
| 11 | [ApiController]
|
|---|
| 12 | public class UsersController : ControllerBase
|
|---|
| 13 | {
|
|---|
| 14 | private readonly IMediator _mediator;
|
|---|
| 15 | private readonly ILogger<UsersController> _logger;
|
|---|
| 16 |
|
|---|
| 17 | public UsersController(IMediator mediator, ILogger<UsersController> logger)
|
|---|
| 18 | {
|
|---|
| 19 | _mediator = mediator;
|
|---|
| 20 | _logger = logger;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | [HttpGet]
|
|---|
| 24 | [AllowAnonymous]
|
|---|
| 25 | public async Task<ActionResult> GetAll()
|
|---|
| 26 | {
|
|---|
| 27 | _logger.LogInformation("Fetching all users");
|
|---|
| 28 | var response = await _mediator.Send(new GetAllRequest());
|
|---|
| 29 | var result = response.Users.Select(u => new
|
|---|
| 30 | {
|
|---|
| 31 | id = u.Id,
|
|---|
| 32 | username = u.Username,
|
|---|
| 33 | name = u.Name,
|
|---|
| 34 | surname = u.Surname,
|
|---|
| 35 | email = u.Email,
|
|---|
| 36 | role = u.Admin != null ? "admin" : u.Writer != null ? "writer" : "regular",
|
|---|
| 37 | });
|
|---|
| 38 | return Ok(result);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | [HttpGet("{id:int}")]
|
|---|
| 42 | [AllowAnonymous]
|
|---|
| 43 | public async Task<ActionResult> GetById(int id)
|
|---|
| 44 | {
|
|---|
| 45 | _logger.LogInformation("Fetching user with ID: {UserId}", id);
|
|---|
| 46 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 47 | return Ok(response);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | [HttpPost]
|
|---|
| 51 | [Authorize]
|
|---|
| 52 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 53 | {
|
|---|
| 54 | _logger.LogInformation("Adding a new user with username: {Username}", request.Username);
|
|---|
| 55 | var response = await _mediator.Send(request);
|
|---|
| 56 | return Ok(response);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | [HttpPut("{id:int}")]
|
|---|
| 60 | [Authorize]
|
|---|
| 61 | public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
|
|---|
| 62 | {
|
|---|
| 63 | _logger.LogInformation("Updating user with ID: {UserId}", id);
|
|---|
| 64 | if (id != request.Id)
|
|---|
| 65 | {
|
|---|
| 66 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | var response = await _mediator.Send(request);
|
|---|
| 70 | return Ok(response);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | [HttpDelete("{id:int}")]
|
|---|
| 74 | [Authorize]
|
|---|
| 75 | public async Task<ActionResult> Delete(int id)
|
|---|
| 76 | {
|
|---|
| 77 | _logger.LogInformation("Deleting user with ID: {UserId}", id);
|
|---|
| 78 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 79 | return Ok(response);
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|