laatzen/LaaProductionWeb/LaaProductionWeb.Services/AccountService.cs
2023-04-20 11:13:06 +02:00

52 lines
2.0 KiB
C#

namespace LaaProductionWeb.Services
{
using LaaProductionWeb.Data.Interfaces;
using LaaProductionWeb.Services.Interfaces;
using LaaProductionWeb.Services.Models;
using System.Collections.Generic;
public class AccountService : IAccountService
{
private readonly ISqlClient sqlClient;
public AccountService(ISqlClient sqlClient)
=> this.sqlClient = sqlClient;
public Employee FindEmployee(int userId)
{
var employeIdentity = this.sqlClient.FirstOrDefault($@"
SELECT TOP 1
[MitarbeiterNr]
, [Vorname] + ' ' + [Name]
FROM [Mitarbeiter]
WHERE [MitarbeiterNr] = @{nameof(userId)}",
parameters => parameters.Add(nameof(userId), userId),
reader => new KeyValuePair<int, string>(
key: reader.GetValue<short>(),
value: reader.GetString()));
var permissions = this.sqlClient.ExecuteReader($@"
SELECT [MitarbeiterRechte].[Recht]
FROM [MitarbeiterRechte]
WHERE [MitarbeiterNr] = @{nameof(employeIdentity.Key)}
AND [MitarbeiterRechte].[Recht] LIKE 'WEB_%'",
parameters => parameters.Add(nameof(employeIdentity.Key), employeIdentity.Key),
reader => reader.GetString());
return new Employee(employeIdentity.Value, permissions);
}
public int Login(string username, string password)
=> this.sqlClient.FirstOrDefault($@"
SELECT TOP 1 [MitarbeiterNr]
FROM [Mitarbeiter]
WHERE [Benutzername] LIKE @{nameof(username)}
AND [Kennwort] LIKE @{nameof(password)}",
parameters => parameters
.Add(nameof(username), username)
.Add(nameof(password), password),
reader => reader.GetValue<short>());
}
}