shoc-backend/Api.SeaHavenIndustries/Controllers/CommonController.cs

98 lines
3 KiB
C#
Raw Normal View History

2024-09-28 14:58:36 -04:00
using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SeaHaven.Services.Interfaces;
2024-09-28 14:58:36 -04:00
namespace Api.SeaHavenIndustries.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
2024-09-28 14:58:36 -04:00
public class CommonController : Controller
{
private readonly ICommonService _commonService;
public CommonController(ICommonService commonService)
2024-09-28 14:58:36 -04:00
{
_commonService = commonService;
2024-09-28 14:58:36 -04:00
}
[HttpGet]
[Route("GetUsers")]
public async Task<IActionResult> GetUsers(string? search = "", int page = 1, int pageSize = 10, CancellationToken cancellationToken = default)
2024-09-28 14:58:36 -04:00
{
try
{
var result = await _commonService.GetUsersPagedAsync(search, page, pageSize, cancellationToken);
2024-09-28 14:58:36 -04:00
var filteredUsers = result.Users.Select(u => new UserRoleViewModel
2024-09-28 14:58:36 -04:00
{
RoleName = u.RoleName,
Email = u.Email,
UserName = u.UserName,
Date = u.Date,
Status = u.Status,
Id = u.Id
}).ToList();
2024-09-28 14:58:36 -04:00
var viewModel = new Pagination_DTO
{
Data = filteredUsers,
PageNumber = result.PageNumber,
PageSize = result.PageSize,
TotalCount = result.TotalCount,
TotalPages = result.TotalPages
2024-09-28 14:58:36 -04:00
};
return Ok(viewModel);
}
catch (Exception ex)
{
return BadRequest(new { Status = "Error", Message = ex.Message });
}
}
[HttpGet]
[Route("GetRoles")]
public async Task<IActionResult> GetRoles(CancellationToken cancellationToken)
2024-09-28 14:58:36 -04:00
{
try
{
var data = await _commonService.GetRolesAsync(cancellationToken);
2024-09-28 14:58:36 -04:00
return Ok(data);
}
catch (Exception ex)
{
return BadRequest(new { Status = "Error", Message = ex.Message });
}
}
[HttpGet]
[Route("GetSitesName")]
public async Task<IActionResult> GetSiteName(CancellationToken cancellationToken)
2024-09-28 14:58:36 -04:00
{
var data = await _commonService.GetSiteNamesAsync(cancellationToken);
2024-09-28 14:58:36 -04:00
return Ok(data);
}
[HttpGet]
[Route("GetContacts")]
public async Task<IActionResult> GetContacts(CancellationToken cancellationToken)
2024-09-28 14:58:36 -04:00
{
var data = await _commonService.GetContactsAsync(cancellationToken);
2024-09-28 14:58:36 -04:00
return Ok(data);
}
[HttpGet]
[Route("GetCategories")]
public async Task<IActionResult> GetCategories(CancellationToken cancellationToken)
2024-09-28 14:58:36 -04:00
{
var data = await _commonService.GetCategoriesAsync(cancellationToken);
2024-09-28 14:58:36 -04:00
return Ok(data);
}
2024-09-28 14:58:36 -04:00
}
}