namespace LaaProduction.Personalization.Repositories { using LaaPackages.SqlClient; using LaaProduction.Personalization.Repositories.Interfaces; using System; using System.Collections.Generic; using System.Linq; public class FunctionsRepository : IFunctionsRepository { private readonly SqlConnection sqlConnection; public FunctionsRepository(SqlConnection sqlConnection) => this.sqlConnection = sqlConnection; public void AddEmployeesFunctions(Int32 functionId, IEnumerable employees) => this.sqlConnection .CreateCommand($@" INSERT INTO [UsersFunctions] ([UserId], [SoftwareFunctionId]) VALUES {String.Join(", ", employees.Select((x, i) => $"({new System.Data.SqlClient.SqlParameter($"p{i}", x).SqlValue}, @{nameof(functionId)})"))}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public Int32 AddFunction(Int16 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.GetValue(0)); public IEnumerable All() => this.sqlConnection .CreateCommand($@" SELECT DISTINCT [SF].[Function] FROM [SoftwareFunctions] AS [SF]") .ExecuteReader(x => x.GetValue(0)); public IEnumerable All(Int16 appId) => this.sqlConnection .CreateCommand($@" SELECT [SF].[Function] FROM [SoftwareFunctions] AS [SF] WHERE [SF].[AppId] = @{nameof(appId)}") .SetParameter(nameof(appId), appId) .ExecuteReader(x => x.GetValue(0)); public void Delete(Int16 employeeId) => this.sqlConnection .CreateCommand($@" DELETE FROM [UsersFunctions] WHERE [UserId] = @{nameof(employeeId)}") .SetParameter(nameof(employeeId), employeeId) .ExecuteNonQuery(); public void DeleteEmployeesFunctions(Int32 functionId) => this.sqlConnection .CreateCommand($@" DELETE FROM [UsersFunctions] WHERE [SoftwareFunctionId] = @{nameof(functionId)}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public void DeleteFunction(Int32 functionId) => this.sqlConnection .CreateCommand($@" DELETE FROM [SoftwareFunctions] WHERE [Id] = @{nameof(functionId)}") .SetParameter(nameof(functionId), functionId) .ExecuteNonQuery(); public IEnumerable EmployeeAll(Int16 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.GetValue(0)); public Boolean Exists(Int32 softwareId, Int32 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.GetValue(0) > 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.GetValue(0)); public void Insert(Int16 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(Int32 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.GetValue(0), x.GetValue(1))); } }