Files
laatzen/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs
T

83 lines
3.3 KiB
C#

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