mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 22:23:12 +00:00
Align DeleteUser with AddUser/EditUser: Admin role at controller and service entry, Forbidden for non-Admin, and regression coverage. Co-authored-by: Cursor <cursoragent@cursor.com>
151 lines
5.5 KiB
C#
151 lines
5.5 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Api.SeaHavenIndustries.Helper;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Helpers;
|
|
using SeaHaven.Services.Interfaces;
|
|
using System.Security.Claims;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/User")]
|
|
public class UserController : Controller
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly ILogger<UserController> _logger;
|
|
|
|
public UserController(IUserService userService, ILogger<UserController> logger)
|
|
{
|
|
_userService = userService;
|
|
_logger = logger;
|
|
}
|
|
[Route("")]
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetUsers(CancellationToken cancellationToken)
|
|
{
|
|
var users = await _userService.GetUsersAsync(cancellationToken);
|
|
return Ok(users);
|
|
}
|
|
[HttpPost]
|
|
[Route("AddUser")]
|
|
[Authorize(Roles = "Admin")]
|
|
public async Task<IActionResult> AddUser(User_DTO user, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var dto = new AddUserRequestDTO
|
|
{
|
|
Name = user.Name,
|
|
Email = user.Email,
|
|
Contact = user.Contact,
|
|
Role = user.Role,
|
|
AccountId = user.AccountId
|
|
};
|
|
var outcome = await _userService.AddUserAsync(dto, User, cancellationToken);
|
|
if (!outcome.Success)
|
|
{
|
|
if (string.Equals(outcome.Error, UserMutationErrors.Forbidden, StringComparison.Ordinal))
|
|
return Forbid();
|
|
return BadRequest(new Response { Status = "Error", Message = outcome.Error });
|
|
}
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
|
|
|
}
|
|
}
|
|
[HttpPut]
|
|
[Route("EditUser")]
|
|
[Authorize(Roles = "Admin")]
|
|
public async Task<IActionResult> EditUser(EditUser_DTO user, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var dto = new EditUserRequestDTO
|
|
{
|
|
Id = user.Id,
|
|
Name = user.Name,
|
|
Email = user.Email,
|
|
Role = user.Role,
|
|
AccountId = user.AccountId
|
|
};
|
|
var outcome = await _userService.EditUserAsync(dto, User, cancellationToken);
|
|
if (!outcome.Success)
|
|
{
|
|
if (string.Equals(outcome.Error, UserMutationErrors.Forbidden, StringComparison.Ordinal))
|
|
return Forbid();
|
|
return BadRequest(new Response { Status = "Error", Message = outcome.Error });
|
|
}
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
|
}
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("DeleteUser")]
|
|
[Authorize(Roles = "Admin")]
|
|
public async Task<IActionResult> DeleteUser(EditUser_DTO req, CancellationToken cancellationToken)
|
|
{
|
|
string id = req.Id ?? "";
|
|
try
|
|
{
|
|
var outcome = await _userService.DeleteUserAsync(id, User, cancellationToken);
|
|
if (!outcome.Success)
|
|
{
|
|
if (string.Equals(outcome.Error, UserMutationErrors.Forbidden, StringComparison.Ordinal))
|
|
return Forbid();
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
|
|
}
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("DeleteCurrentUser")]
|
|
public async Task<IActionResult> DeleteCurrentUser(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? "";
|
|
await _userService.DeleteCurrentUserAsync(userid, cancellationToken);
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("UserProfile")]
|
|
public async Task<IActionResult> UserProfile(CancellationToken cancellationToken)
|
|
{
|
|
var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? "";
|
|
|
|
var data = await _userService.GetUserProfileAsync(userid, cancellationToken);
|
|
return Ok(data.Select(d => new
|
|
{
|
|
d.Id,
|
|
Name = d.Name,
|
|
addeddate = d.AddedDate,
|
|
d.Email,
|
|
d.Contact
|
|
}));
|
|
}
|
|
}
|
|
}
|