136 lines
4.2 KiB
C#
136 lines
4.2 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Api.SeaHavenIndustries.Helper.ViewModel;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
|
|
public class CommonController : Controller
|
|
{
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly ApplicationDbContext _db;
|
|
public CommonController(UserManager<ApplicationUser> userManager, ApplicationDbContext db)
|
|
{
|
|
_userManager = userManager;
|
|
_db = db;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetUsers")]
|
|
public async Task<IActionResult> GetUsers(string? search = "", int page = 1, int pageSize = 10)
|
|
{
|
|
try
|
|
{
|
|
var allUsersQuery = _db.Users
|
|
.Where(w => w.FirstName.ToLower().Contains(search.ToLower()))
|
|
.OrderByDescending(d => d.Id);
|
|
|
|
var totalCount = allUsersQuery.Count();
|
|
|
|
var paginatedUsers = allUsersQuery
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
// Calculate the number of pages
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var filteredUsers = new List<UserRoleViewModel>();
|
|
|
|
foreach (var user in paginatedUsers)
|
|
{
|
|
var roles = await _userManager.GetRolesAsync(user);
|
|
if (!roles.Contains("Admin"))
|
|
{
|
|
filteredUsers.Add(new UserRoleViewModel
|
|
{
|
|
RoleName = roles.Count()>0?roles.First():"",
|
|
Email = user.Email,
|
|
UserName = user.FirstName,
|
|
Date = user.CreatedDate?.Date.ToString(),
|
|
Status = user.UniqueName,
|
|
Id = user.Id
|
|
});
|
|
}
|
|
}
|
|
// Create a view model containing the paged items and pagination metadata
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = filteredUsers,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = totalPages
|
|
};
|
|
return Ok(viewModel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetRoles")]
|
|
public async Task<IActionResult> GetRoles()
|
|
{
|
|
try
|
|
{
|
|
var data = await _db.Roles.Select(s => new {Id=s.Name,s.Name}).ToListAsync();
|
|
|
|
return Ok(data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
[Route("GetSitesName")]
|
|
public async Task<IActionResult> GetSiteName()
|
|
{
|
|
var data =await _db.Locations.Select(n=> new
|
|
{
|
|
n.Id,
|
|
n.Name,
|
|
n.Title
|
|
}).ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetContacts")]
|
|
public async Task<IActionResult> GetContacts()
|
|
{
|
|
var data =await _db.Contacts.Select(n=> new
|
|
{
|
|
n.Id,
|
|
n.Name,
|
|
email=n.Email,
|
|
phone=n.PhoneNumber
|
|
}).ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetCategories")]
|
|
public async Task<IActionResult> GetCategories()
|
|
{
|
|
var data = await _db.Categories.Select(s=> new
|
|
{
|
|
s.Id,
|
|
s.Name
|
|
}).ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
}
|
|
}
|