laatzen/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs
Stoyan Zlatev e32e553bfc clr types
2024-12-18 15:25:14 +01:00

82 lines
3.3 KiB
C#

namespace LaaProduction.Personalization.Repositories
{
using LaaPackages.SqlClient;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
public class RolesRepository : IRolesRepository
{
private readonly SqlConnection sqlConnection;
public RolesRepository(SqlConnection sqlConnection)
=> this.sqlConnection = sqlConnection;
public IEnumerable<Role> All()
=> this.sqlConnection
.CreateCommand(@"
SELECT [Recht]
, [MitarbeiterNr]
FROM [MitarbeiterRechte]")
.ExecuteReader(x => new Role
{
Name = x.GetValue<string>(0),
EmployeeId = (short)x.GetValue<int>(1)
});
public IEnumerable<string> All(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT [Recht]
FROM [MitarbeiterRechte]
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteReader(x => x.GetValue<string>(0));
public void Delete(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [MitarbeiterRechte]
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void Insert(short employeeId, IEnumerable<string> roles)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [MitarbeiterRechte] ([MitarbeiterNr], [Recht])
VALUES {string.Join(", ", roles.Select((x, i) => $"(@{nameof(employeeId)}, '{new System.Data.SqlClient.SqlParameter(i.ToString(), x).SqlValue}')"))}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void Insert(string role, IEnumerable<short> employees)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [MitarbeiterRechte] ([MitarbeiterNr], [Recht])
VALUES {string.Join(", ", employees.Select((x, i) => $"('{new System.Data.SqlClient.SqlParameter(i.ToString(), x).SqlValue}', @{nameof(role)})"))}")
.SetParameter(nameof(role), role)
.ExecuteNonQuery();
public void Delete(string role)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [MitarbeiterRechte]
WHERE [Recht] = @{nameof(role)}")
.SetParameter(nameof(role), role)
.ExecuteNonQuery();
public IEnumerable<KeyValuePair<int, int>> UsersRolesCount()
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT
[MitarbeiterNr]
, COUNT([Recht])
FROM [MitarbeiterRechte]
GROUP BY [MitarbeiterNr]")
.ExecuteReader(x => new KeyValuePair<int, int>(x.GetValue<int>(0), x.GetValue<int>(1)));
}
}