namespace LaaProduction.Personalization.Repositories { using LaaProduction.Personalization.Repositories.Interfaces; using LaaProductionSQL.Interfaces; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; internal class FunctionsRepository : IFunctionsRepository { private readonly ISQLConnection sqlConnection; public FunctionsRepository(ISQLConnection sqlConnection) => this.sqlConnection = sqlConnection; public void AddEmployeesFunctions(int functionId, IEnumerable employees) => this.sqlConnection .CreateCommand($@" INSERT INTO [UsersFunctions] ([UserId], [SoftwareFunctionId]) VALUES {string.Join(", ", employees.Select((x, i) => $"({new SqlParameter($"p{i}", x).SqlValue}, @{nameof(functionId)})"))}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public int AddFunction(short softwareId, string function) => this.sqlConnection .CreateCommand($@" INSERT INTO [SoftwareFunctions] ([AppId], [Function]) OUTPUT [inserted].[Id] VALUES (@{nameof(softwareId)}, @{nameof(function)})") .SetParameter(nameof(softwareId), softwareId) .SetParameter(nameof(function), function) .ExecuteScalar(x => x.GetInt()); public IEnumerable All() => this.sqlConnection .CreateCommand($@" SELECT DISTINCT [SF].[Function] FROM [SoftwareFunctions] AS [SF]") .ExecuteReader(x => x.GetString()); public IEnumerable All(short appId) => this.sqlConnection .CreateCommand($@" SELECT [SF].[Function] FROM [SoftwareFunctions] AS [SF] WHERE [SF].[AppId] = @{nameof(appId)}") .SetParameter(nameof(appId), appId) .ExecuteReader(x => x.GetString()); public void Delete(short employeeId) => this.sqlConnection .CreateCommand($@" DELETE FROM [UsersFunctions] WHERE [UserId] = @{nameof(employeeId)}") .SetParameter(nameof(employeeId), employeeId) .ExecuteNonQuery(); public void DeleteEmployeesFunctions(int functionId) => this.sqlConnection .CreateCommand($@" DELETE FROM [UsersFunctions] WHERE [SoftwareFunctionId] = @{nameof(functionId)}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public void DeleteFunction(int functionId) => this.sqlConnection .CreateCommand($@" DELETE FROM [SoftwareFunctions] WHERE [Id] = @{nameof(functionId)}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public IEnumerable EmployeeAll(short employeeId) => this.sqlConnection .CreateCommand($@" SELECT [SF].[Function] FROM [UsersFunctions] AS [UF] JOIN [SoftwareFunctions] AS [SF] ON [SF].[Id] = [UF].[SoftwareFunctionId] WHERE [UF].[UserId] = @{nameof(employeeId)}") .SetParameter(nameof(employeeId), employeeId) .ExecuteReader(x => x.GetString()); public bool Exists(int softwareId, int functionId) => this.sqlConnection .CreateCommand($@" SELECT COUNT(1) FROM [SoftwareFunctions] WHERE [AppId] = @{nameof(softwareId)} AND [Id] = @{nameof(functionId)}") .SetParameter(nameof(softwareId), softwareId) .SetParameter(nameof(functionId), functionId) .FirstOrDefault(x => x.GetInt() > 0); public IEnumerable Find(string token) => this.sqlConnection .CreateCommand($@" SELECT [SF].[Function] FROM [UsersFunctions] AS [UF] JOIN [SoftwareFunctions] AS [SF] ON [SF].[Id] = [UF].[SoftwareFunctionId] JOIN [MitarbeiterTokens] AS [MT] ON [MT].[UserId] = [UF].[UserId] AND [MT].[AppId] = [SF].[AppId] AND [MT].[Token] = @{nameof(token)}") .SetParameter(nameof(token), token) .ExecuteReader(x => x.GetString()); public void Insert(short employeeId, IEnumerable functions) => this.sqlConnection .CreateCommand($@" INSERT INTO [UsersFunctions] ( [UserId] , [SoftwareFunctionId]) SELECT @{nameof(employeeId)} , [Id] FROM [SoftwareFunctions] WHERE [Function] IN ({string.Join(", ", functions.Select(x => $"'{x}'"))})") .SetParameter(nameof(employeeId), employeeId) .ExecuteNonQuery(); public void UpdateFunction(int functionId, string function) => this.sqlConnection .CreateCommand($@" UPDATE [SoftwareFunctions] SET [Function] = @{nameof(function)} WHERE [Id] = @{nameof(functionId)}") .SetParameter(nameof(functionId), functionId) .SetParameter(nameof(function), function) .ExecuteNonQuery(); public IEnumerable> UserFunctionsCount() => this.sqlConnection .CreateCommand($@" SELECT DISTINCT [UF].[UserId] AS [EmployeeId] , COUNT([SF].[Function]) AS [Functions] FROM [UsersFunctions] AS [UF] JOIN [SoftwareFunctions] AS [SF] ON [SF].[Id] = [UF].[SoftwareFunctionId] GROUP BY [UF].[UserId]") .ExecuteReader(x => new KeyValuePair(x.GetSmallint(), x.GetInt())); } }