laatzen/LaaProductionWeb/LaaProduction.Personalization/Repositories/FunctionsRepository.cs
2023-06-16 11:29:11 +02:00

65 lines
2.5 KiB
C#

namespace LaaProduction.Personalization.Repositories
{
using LaaProduction.Personalization.Repositories.Interfaces;
using LaaProductionSQL.Interfaces;
using System.Collections.Generic;
using System.Linq;
internal class FunctionsRepository : IFunctionsRepository
{
private readonly ISQLConnection sqlConnection;
public FunctionsRepository(ISQLConnection sqlConnection)
=> this.sqlConnection = sqlConnection;
public IEnumerable<string> All()
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT [SF].[Function]
FROM [SoftwareFunctions] AS [SF]")
.ExecuteReader(x => x.GetString());
public IEnumerable<string> 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 IEnumerable<string> 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 void Insert(short employeeId, IEnumerable<string> 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();
}
}