2024-09-28 14:58:36 -04:00
|
|
|
|
using Api.SeaHavenIndustries.DTOs;
|
|
|
|
|
|
using Api.SeaHavenIndustries.Helper;
|
|
|
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-07-24 17:35:34 -03:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using SeaHaven.Services.DTOs;
|
2026-08-11 09:28:08 -03:00
|
|
|
|
using SeaHaven.Services.Helpers;
|
2026-07-24 17:35:34 -03:00
|
|
|
|
using SeaHaven.Services.Interfaces;
|
2024-09-28 14:58:36 -04:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
[ApiController]
|
2025-02-18 13:13:33 -06:00
|
|
|
|
[Route("api/User")]
|
2024-09-28 14:58:36 -04:00
|
|
|
|
public class UserController : Controller
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly ILogger<UserController> _logger;
|
2024-09-28 14:58:36 -04:00
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public UserController(IUserService userService, ILogger<UserController> logger)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_logger = logger;
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
2025-02-18 13:13:33 -06:00
|
|
|
|
[Route("")]
|
|
|
|
|
|
[HttpGet]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> GetUsers(CancellationToken cancellationToken)
|
2025-02-18 13:13:33 -06:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
var users = await _userService.GetUsersAsync(cancellationToken);
|
|
|
|
|
|
return Ok(users);
|
2025-02-18 13:13:33 -06:00
|
|
|
|
}
|
2024-09-28 14:58:36 -04:00
|
|
|
|
[HttpPost]
|
|
|
|
|
|
[Route("AddUser")]
|
2026-08-11 09:28:08 -03:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> AddUser(User_DTO user, CancellationToken cancellationToken)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
var dto = new AddUserRequestDTO
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
Name = user.Name,
|
|
|
|
|
|
Email = user.Email,
|
|
|
|
|
|
Contact = user.Contact,
|
2026-08-06 10:26:04 -03:00
|
|
|
|
Role = user.Role,
|
|
|
|
|
|
AccountId = user.AccountId
|
2026-07-24 17:35:34 -03:00
|
|
|
|
};
|
2026-08-11 09:28:08 -03:00
|
|
|
|
var outcome = await _userService.AddUserAsync(dto, User, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
|
if (!outcome.Success)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-08-11 09:28:08 -03:00
|
|
|
|
if (string.Equals(outcome.Error, UserMutationErrors.Forbidden, StringComparison.Ordinal))
|
|
|
|
|
|
return Forbid();
|
2026-07-24 17:35:34 -03:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = outcome.Error });
|
2025-02-18 13:13:33 -06:00
|
|
|
|
}
|
2024-09-28 14:58:36 -04:00
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
2025-02-18 13:13:33 -06:00
|
|
|
|
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2025-02-18 13:13:33 -06:00
|
|
|
|
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-18 13:13:33 -06:00
|
|
|
|
[HttpPut]
|
2024-09-28 14:58:36 -04:00
|
|
|
|
[Route("EditUser")]
|
2026-08-11 09:28:08 -03:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> EditUser(EditUser_DTO user, CancellationToken cancellationToken)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
var dto = new EditUserRequestDTO
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
Id = user.Id,
|
|
|
|
|
|
Name = user.Name,
|
|
|
|
|
|
Email = user.Email,
|
2026-08-06 10:26:04 -03:00
|
|
|
|
Role = user.Role,
|
|
|
|
|
|
AccountId = user.AccountId
|
2026-07-24 17:35:34 -03:00
|
|
|
|
};
|
2026-08-11 09:28:08 -03:00
|
|
|
|
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 });
|
|
|
|
|
|
}
|
2024-09-28 14:58:36 -04:00
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-18 13:13:33 -06:00
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
2024-09-28 14:58:36 -04:00
|
|
|
|
[Route("DeleteUser")]
|
2026-08-11 09:44:52 -03:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> DeleteUser(EditUser_DTO req, CancellationToken cancellationToken)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
string id = req.Id ?? "";
|
2024-09-28 14:58:36 -04:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-08-11 09:44:52 -03:00
|
|
|
|
var outcome = await _userService.DeleteUserAsync(id, User, cancellationToken);
|
|
|
|
|
|
if (!outcome.Success)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-08-11 09:44:52 -03:00
|
|
|
|
if (string.Equals(outcome.Error, UserMutationErrors.Forbidden, StringComparison.Ordinal))
|
|
|
|
|
|
return Forbid();
|
2024-09-28 14:58:36 -04:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
|
|
|
|
|
|
}
|
|
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
[Route("DeleteCurrentUser")]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> DeleteCurrentUser(CancellationToken cancellationToken)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? "";
|
2026-07-24 17:35:34 -03:00
|
|
|
|
await _userService.DeleteCurrentUserAsync(userid, cancellationToken);
|
2024-09-28 14:58:36 -04:00
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
[Route("UserProfile")]
|
2026-07-24 17:35:34 -03:00
|
|
|
|
public async Task<IActionResult> UserProfile(CancellationToken cancellationToken)
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? "";
|
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
|
var data = await _userService.GetUserProfileAsync(userid, cancellationToken);
|
|
|
|
|
|
return Ok(data.Select(d => new
|
2024-09-28 14:58:36 -04:00
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
|
d.Id,
|
|
|
|
|
|
Name = d.Name,
|
|
|
|
|
|
addeddate = d.AddedDate,
|
|
|
|
|
|
d.Email,
|
|
|
|
|
|
d.Contact
|
|
|
|
|
|
}));
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|