mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 15:23:12 +00:00
59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class UserDataService : IUserDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public UserDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<ApplicationUser?> GetByIdAsync(string id)
|
|
{
|
|
return await _context.Users.FindAsync(id);
|
|
}
|
|
|
|
public async Task<IEnumerable<ApplicationUser>> GetAllAsync()
|
|
{
|
|
return await _context.Users.ToListAsync();
|
|
}
|
|
|
|
public async Task<ApplicationUser?> GetByEmailAsync(string email)
|
|
{
|
|
return await _context.Users
|
|
.FirstOrDefaultAsync(u => u.Email == email);
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(string id)
|
|
{
|
|
return await _context.Users.AnyAsync(u => u.Id == id);
|
|
}
|
|
|
|
public async Task<IReadOnlyDictionary<string, string>> GetDisplayNamesByIdsAsync(IEnumerable<string> ids)
|
|
{
|
|
var idList = ids
|
|
.Where(id => !string.IsNullOrWhiteSpace(id))
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToList();
|
|
|
|
if (idList.Count == 0)
|
|
return new Dictionary<string, string>(StringComparer.Ordinal);
|
|
|
|
var users = await _context.Users
|
|
.AsNoTracking()
|
|
.Where(u => idList.Contains(u.Id))
|
|
.Select(u => new { u.Id, u.FirstName, u.LastName })
|
|
.ToListAsync();
|
|
|
|
return users.ToDictionary(
|
|
u => u.Id,
|
|
u => $"{u.FirstName ?? ""} {u.LastName ?? ""}".Trim(),
|
|
StringComparer.Ordinal);
|
|
}
|
|
}
|
|
}
|