Files
laatzen/LaaProductionWeb/LaaProduction.Personalization/Repositories/EmployeesRepository.cs
T
2023-06-22 14:04:12 +02:00

184 lines
7.8 KiB
C#

namespace LaaProduction.Personalization.Repositories
{
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using LaaProductionSQL.Interfaces;
using System.Collections.Generic;
using System.Linq;
internal class EmployeesRepository : IEmployeesRepository
{
private readonly ISQLConnection sqlConnection;
public EmployeesRepository(ISQLConnection sqlConnection)
=> this.sqlConnection = sqlConnection;
public IEnumerable<Employee> All()
=> this.sqlConnection
.CreateCommand($@"
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
, [M].[PasswordHash] AS [Password]
, [MR].[Recht] AS [Role]
, [SF].[Function] AS [Function]
FROM [Mitarbeiter] AS [M]
LEFT JOIN [MitarbeiterRechte] AS [MR]
ON [MR].[MitarbeiterNr] = [M].[MitarbeiterNr]
LEFT JOIN [UsersFunctions] AS [UF]
ON [UF].[UserId] = [M].[MitarbeiterNr]
LEFT JOIN [SoftwareFunctions] AS [SF]
ON [SF].[Id] = [UF].[SoftwareFunctionId]
ORDER BY [FullName], [Username]")
.ExecuteReader(x => new
{
EmployeeId = x.GetSmallint(),
FullName = x.GetString(),
Username = x.GetString(),
HasPassword = !string.IsNullOrWhiteSpace(x.GetString()),
Role = x.GetString(),
Function = x.GetString()
})
.GroupBy(x => new Employee
{
EmployeeId = x.EmployeeId,
FullName = x.FullName,
Username = x.Username,
HasPassword = x.HasPassword,
}, new EmployeeComparer())
.Select(x =>
{
x.Key.Roles = x.Select(r => r.Role).Distinct();
x.Key.RolesCount = x.Key.Roles.Count();
x.Key.Functions = x.Select(f => f.Function).Distinct();
x.Key.FunctionsCount = x.Key.Functions.Count();
return x.Key;
});
public void CreateToken(short appId, short userId)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [MitarbeiterTokens] (
[AppId]
, [UserId])
VALUES (@{nameof(appId)}
, @{nameof(userId)})")
.SetParameter(nameof(appId), appId)
.SetParameter(nameof(userId), userId)
.ExecuteNonQuery();
public short EmployeeId(string username, string password)
=> this.sqlConnection
.CreateCommand($@"
SELECT [MitarbeiterNr]
FROM [Mitarbeiter]
WHERE [Benutzername] = @{nameof(username)}
AND [PasswordHash] = @{nameof(password)}")
.SetParameter(nameof(username), username)
.SetParameter(nameof(password), password)
.FirstOrDefault(x => x.GetSmallint());
public Employee Find(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
, [M].[PasswordHash] AS [Password]
FROM [Mitarbeiter] AS [M]
WHERE [M].[MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.FirstOrDefault(x => new Employee
{
EmployeeId = x.GetSmallint(),
FullName = x.GetString(),
Username = x.GetString(),
HasPassword = !string.IsNullOrWhiteSpace(x.GetString())
});
public Employee GetEmployee(string token)
=> this.sqlConnection
.CreateCommand($@"
SELECT [M].[MitarbeiterNr]
, [M].[Vorname] + ' ' + [M].[Name]
, [M].[Benutzername]
FROM [MitarbeiterTokens] AS [MT]
JOIN [Mitarbeiter] AS [M]
ON [M].[MitarbeiterNr] = [MT].[UserId]
WHERE [MT].[Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.FirstOrDefault(x => new Employee
{
EmployeeId = x.GetSmallint(),
FullName = x.GetString(),
Username = x.GetString()
});
public string GetToken(short appId, short userId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [Token]
FROM [MitarbeiterTokens]
WHERE [AppId] = @{nameof(appId)}
AND [UserId] = @{nameof(userId)}")
.SetParameter(nameof(appId), appId)
.SetParameter(nameof(userId), userId)
.FirstOrDefault(x => x.GetString());
public EmployeeToken GetTokenData(string token)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppId]
, [UserId]
FROM [MitarbeiterTokens]
WHERE [Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.FirstOrDefault(x => new EmployeeToken
{
AppId = x.GetSmallint(),
EmployeeId = x.GetSmallint()
});
public IEnumerable<string> GetUserRoles(string token)
=> this.sqlConnection
.CreateCommand($@"
SELECT [MR].[Recht]
FROM [MitarbeiterTokens] AS [MT]
JOIN [MitarbeiterRechte] AS [MR]
ON [MR].[MitarbeiterNr] = [MT].[UserId]
WHERE [MT].[Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.ExecuteReader(x => x.GetString());
public void ResetPassword(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
UPDATE [Mitarbeiter]
SET [PasswordHash] = NULL
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void ResetTokens(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [MitarbeiterTokens]
WHERE [UserId] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void SetPassword(short employeeId, string password)
=> this.sqlConnection
.CreateCommand($@"
UPDATE [Mitarbeiter]
SET [PasswordHash] = @{nameof(password)}
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.SetParameter(nameof(password), password)
.ExecuteNonQuery();
}
}