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