97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using Dapper;
|
|
using ProostenKrant.Models;
|
|
|
|
namespace ProostenKrant.Services;
|
|
|
|
public class UserService
|
|
{
|
|
private readonly Db _db;
|
|
private readonly IWebHostEnvironment _env;
|
|
private readonly IConfiguration _config;
|
|
private static readonly string[] AfbeeldingExtensies = { ".jpg", ".jpeg", ".png", ".webp" };
|
|
|
|
public UserService(Db db, IWebHostEnvironment env, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_env = env;
|
|
_config = config;
|
|
}
|
|
|
|
public async Task<List<User>> GetAllAsync()
|
|
{
|
|
await using var con = await _db.OpenAsync();
|
|
var users = await con.QueryAsync<User>(
|
|
"SELECT id, username, display_name AS DisplayName, profile_image AS ProfileImage, is_active AS IsActive " +
|
|
"FROM users ORDER BY display_name");
|
|
return users.ToList();
|
|
}
|
|
|
|
public async Task<User?> FindAsync(int id)
|
|
{
|
|
await using var con = await _db.OpenAsync();
|
|
return await con.QuerySingleOrDefaultAsync<User>(
|
|
"SELECT id, username, display_name AS DisplayName, profile_image AS ProfileImage, is_active AS IsActive " +
|
|
"FROM users WHERE id = @id", new { id });
|
|
}
|
|
|
|
public async Task<List<User>> GetByIdsAsync(IEnumerable<int> ids)
|
|
{
|
|
var idList = ids.Distinct().ToList();
|
|
if (idList.Count == 0)
|
|
{
|
|
return new List<User>();
|
|
}
|
|
await using var con = await _db.OpenAsync();
|
|
var users = await con.QueryAsync<User>(
|
|
"SELECT id, username, display_name AS DisplayName, profile_image AS ProfileImage, is_active AS IsActive " +
|
|
"FROM users WHERE id IN @ids ORDER BY display_name", new { ids = idList });
|
|
return users.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zoekt de profielfoto van een familielid. Volgorde: eigen uploadmap,
|
|
/// resources-map (bijv. nieuwe familieleden), dan Unknown.jpg.
|
|
/// </summary>
|
|
public string GetProfileWebPath(User user)
|
|
{
|
|
var kandidaten = new List<(string Web, string Abs)>();
|
|
|
|
if (!string.IsNullOrEmpty(user.ProfileImage))
|
|
{
|
|
var rel = user.ProfileImage.TrimStart('/');
|
|
kandidaten.Add(("/" + rel, AbsPath(rel)));
|
|
}
|
|
|
|
kandidaten.Add(("/uploads/profiles/" + user.DisplayName + ".jpg",
|
|
AbsPath("uploads/profiles/" + user.DisplayName + ".jpg")));
|
|
|
|
var resources = _config["Paths:ResourceImages"] ?? "resources-images";
|
|
var resDir = Path.Combine(_env.WebRootPath ?? ".", resources);
|
|
if (Directory.Exists(resDir))
|
|
{
|
|
foreach (var bestand in Directory.GetFiles(resDir))
|
|
{
|
|
var naam = Path.GetFileNameWithoutExtension(bestand);
|
|
var ext = Path.GetExtension(bestand).ToLowerInvariant();
|
|
if (naam.Equals(user.DisplayName, StringComparison.OrdinalIgnoreCase) &&
|
|
AfbeeldingExtensies.Contains(ext))
|
|
{
|
|
kandidaten.Insert(1, ("/" + resources + "/" + Path.GetFileName(bestand), bestand));
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var (web, abs) in kandidaten)
|
|
{
|
|
if (File.Exists(abs))
|
|
{
|
|
return web;
|
|
}
|
|
}
|
|
return "/uploads/profiles/Unknown.jpg";
|
|
}
|
|
|
|
private string AbsPath(string webPath) =>
|
|
Path.Combine(_env.WebRootPath ?? ".", webPath.Replace('/', Path.DirectorySeparatorChar));
|
|
}
|