laatzen/LaaProductionWeb/LaaProduction.Personalization/Repositories/SoftwareRepository.cs
2024-09-18 15:36:12 +02:00

219 lines
10 KiB
C#

namespace LaaProduction.Personalization.Repositories
{
using LaaPackages.SqlClient;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using System;
using System.Collections.Generic;
public class SoftwareRepository : ISoftwareRepository
{
private readonly SqlConnection sqlConnection;
public SoftwareRepository(SqlConnection sqlConnection)
=> this.sqlConnection = sqlConnection;
public void AddVersion(Int16 softwareId, String assemblyName, Int32 majorV, Int32 minorV, Int32 buildV, DateTime validUntil, String description)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [VersionSoftwareAccess] (
[AppID]
, [SoftwareAccess_Program]
, [SoftwareAccess_MajorVersion]
, [SoftwareAccess_MinorVersion]
, [SoftwareAccess_BuildVersion]
, [SoftwareAccess_VaildTo]
, [SoftwareAccess_Desc])
VALUES (@{nameof(softwareId)}
, @{nameof(assemblyName)}
, @{nameof(majorV)}
, @{nameof(minorV)}
, @{nameof(buildV)}
, @{nameof(validUntil)}
, @{nameof(description)})")
.SetParameter(nameof(softwareId), softwareId)
.SetParameter(nameof(assemblyName), assemblyName)
.SetParameter(nameof(majorV), majorV)
.SetParameter(nameof(minorV), minorV)
.SetParameter(nameof(buildV), buildV)
.SetParameter(nameof(validUntil), validUntil)
.SetParameter(nameof(description), description)
.ExecuteNonQuery();
public Int16 AppId(String software)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppID]
FROM [VersionSoftwareAccess]
WHERE [SoftwareAccess_Program] = @{nameof(software)}")
.SetParameter(nameof(software), software)
.FirstOrDefault(x => x.GetValue<Int16>(0));
public void Delete(Int32 versionId)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [VersionSoftwareAccess]
WHERE [SoftwareAccess_ID] = @{nameof(versionId)}")
.SetParameter(nameof(versionId), versionId)
.ExecuteNonQuery();
public SoftwareDetails Details(Int32 versionId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppID]
, [SoftwareAccess_Program]
, [SoftwareAccess_MajorVersion]
, [SoftwareAccess_MinorVersion]
, [SoftwareAccess_BuildVersion]
, [SoftwareAccess_ID]
, [SoftwareAccess_VaildTo]
, [SoftwareAccess_Desc]
FROM [VersionSoftwareAccess]
WHERE [SoftwareAccess_ID] = @{nameof(versionId)}")
.SetParameter(nameof(versionId), versionId)
.FirstOrDefault(x => new SoftwareDetails
{
SoftwareId = x.GetValue<Int16>(0),
AssemblyName = x.GetValue<String>(1),
VersionNr = $"{x.GetValue<Int32>(2)}.{x.GetValue<Int32>(3)}.{x.GetValue<Int32>(4)}",
VersionId = x.GetValue<Int32>(5),
ValidUntil = $"{x.GetValue<DateTime>(6):yyyy-MM-dd}",
Description = x.GetValue<String>(7),
});
public void EditVersion(Int32 versionId, DateTime? validUntil, String description)
=> this.sqlConnection
.CreateCommand($@"
UPDATE [VersionSoftwareAccess]
SET [SoftwareAccess_VaildTo] = @{nameof(validUntil)}
, [SoftwareAccess_Desc] = @{nameof(description)}
WHERE [SoftwareAccess_ID] = @{nameof(versionId)}")
.SetParameter(nameof(versionId), versionId)
.SetParameter(nameof(validUntil), validUntil)
.SetParameter(nameof(description), description)
.ExecuteNonQuery();
public IEnumerable<SoftwareFunction> Functions(Int16 softwareId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppId]
, [Id]
, [Function]
FROM [SoftwareFunctions]
WHERE [AppId] = @{nameof(softwareId)}")
.SetParameter(nameof(softwareId), softwareId)
.ExecuteReader(x => new SoftwareFunction
{
SoftwareId = x.GetValue<Int16>(0),
FunctionId = x.GetValue<Int32>(1),
Function = x.GetValue<String>(2)
});
public IEnumerable<String> GetSoftwareFunctions(String token)
=> this.sqlConnection
.CreateCommand($@"
SELECT [SF].[Function]
FROM [MitarbeiterTokens] AS [MT]
JOIN [SoftwareFunctions] AS [SF]
ON [SF].[AppId] = [MT].[AppId]
WHERE [MT].[Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.ExecuteReader(x => x.GetValue<String>(0));
public Int16 Insert(String assembly, Int32 major, Int32 minor, Int32 build)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [VersionSoftwareAccess] (
[SoftwareAccess_Program]
, [SoftwareAccess_MajorVersion]
, [SoftwareAccess_MinorVersion]
, [SoftwareAccess_BuildVersion]
, [SoftwareAccess_VaildTo]
, [SoftwareAccess_Desc]
, [AppID])
OUTPUT [inserted].[AppId]
VALUES (@{nameof(assembly)}
, @{nameof(major)}
, @{nameof(minor)}
, @{nameof(build)}
, GETDATE()
, 'Automatically created on first use.'
, (SELECT MAX([AppId]) + 1 FROM [VersionSoftwareAccess]))")
.SetParameter(nameof(assembly), assembly)
.SetParameter(nameof(major), major)
.SetParameter(nameof(minor), minor)
.SetParameter(nameof(build), build)
.ExecuteScalar(x => x.GetValue<Int16>(0));
public IEnumerable<SoftwareItem> List()
=> this.sqlConnection
.CreateCommand($@"
SELECT [Software].[SoftwareId]
, [Software].[AssemblyName]
, [Software].[MajorV]
, [Software].[MinorV]
, [Software].[Build]
, [Software].[VersionId]
, [Software].[ValidUntil]
, [Software].[Description]
FROM (SELECT [AppID] AS [SoftwareId]
, [SoftwareAccess_Program] AS [AssemblyName]
, [SoftwareAccess_MajorVersion] AS [MajorV]
, [SoftwareAccess_MinorVersion] AS [MinorV]
, [SoftwareAccess_BuildVersion] AS [Build]
, [SoftwareAccess_ID] AS [VersionId]
, [SoftwareAccess_VaildTo] AS [ValidUntil]
, [SoftwareAccess_Desc] AS [Description]
, ROW_NUMBER()
OVER (PARTITION BY [AppID]
ORDER BY [SoftwareAccess_VaildTo] DESC) AS [RowNumber]
FROM [VersionSoftwareAccess])
AS [Software]
WHERE [Software].[RowNumber] <= 1
ORDER BY [Software].[AssemblyName]")
.ExecuteReader(x => new SoftwareItem
{
SoftwareId = x.GetValue<Int16>(0),
AssemblyName = x.GetValue<String>(1),
CurrentVersion = $"{x.GetValue<Int32>(2)}.{x.GetValue<Int32>(3)}.{x.GetValue<Int32>(4)}",
VersionId = x.GetValue<Int32>(5),
ValidUntil = $"{x.GetValue<DateTime>(6):yyyy-MM-dd}",
Description = x.GetValue<String>(7),
});
public Int16 SoftwareId(String assembly)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppId]
FROM [VersionSoftwareAccess]
WHERE [SoftwareAccess_Program] = @{nameof(assembly)}")
.SetParameter(nameof(assembly), assembly)
.FirstOrDefault(x => x.GetValue<Int16>(0));
public IEnumerable<SoftwareVersion> Versions(Int16 softwareId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppId]
, [SoftwareAccess_Program]
, [SoftwareAccess_ID]
, [SoftwareAccess_MajorVersion]
, [SoftwareAccess_MinorVersion]
, [SoftwareAccess_BuildVersion]
, [SoftwareAccess_VaildTo]
, [SoftwareAccess_Desc]
FROM [VersionSoftwareAccess]
WHERE [AppId] = @{nameof(softwareId)}")
.SetParameter(nameof(softwareId), softwareId)
.ExecuteReader(x => new SoftwareVersion
{
SoftwareId = x.GetValue<Int16>(0),
AssemblyName = x.GetValue<String>(1),
VersionId = x.GetValue<Int32>(2),
VersionNr = $"{x.GetValue<Int32>(3)}.{x.GetValue<Int32>(4)}.{x.GetValue<Int32>(5)}",
ValidUntil = $"{x.GetValue<DateTime>(6):yyyy-MM-dd}",
Description = x.GetValue<String>(7),
});
}
}