219 lines
10 KiB
C#
219 lines
10 KiB
C#
namespace LaaProduction.Personalization.Repositories
|
|
{
|
|
using LaaProduction.Personalization.Models;
|
|
using LaaProduction.Personalization.Repositories.Interfaces;
|
|
using LaaProductionSQL.Interfaces;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
internal class SoftwareRepository : ISoftwareRepository
|
|
{
|
|
private readonly ISQLConnection sqlConnection;
|
|
|
|
public SoftwareRepository(ISQLConnection sqlConnection)
|
|
=> this.sqlConnection = sqlConnection;
|
|
|
|
public void AddVersion(short softwareId, string assemblyName, int majorV, int minorV, int 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 short AppId(string software)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [AppID]
|
|
FROM [VersionSoftwareAccess]
|
|
WHERE [SoftwareAccess_Program] = @{nameof(software)}")
|
|
.SetParameter(nameof(software), software)
|
|
.FirstOrDefault(x => x.GetSmallint());
|
|
|
|
public void Delete(int versionId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
DELETE FROM [VersionSoftwareAccess]
|
|
WHERE [SoftwareAccess_ID] = @{nameof(versionId)}")
|
|
.SetParameter(nameof(versionId), versionId)
|
|
.ExecuteNonQuery();
|
|
|
|
public SoftwareDetails Details(int 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.GetSmallint(),
|
|
AssemblyName = x.GetString(),
|
|
VersionNr = $"{x.GetInt()}.{x.GetInt()}.{x.GetInt()}",
|
|
VersionId = x.GetInt(),
|
|
ValidUntil = $"{x.GetDate():yyyy-MM-dd}",
|
|
Description = x.GetString(),
|
|
});
|
|
|
|
public void EditVersion(int 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(short softwareId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [AppId]
|
|
, [Id]
|
|
, [Function]
|
|
FROM [SoftwareFunctions]
|
|
WHERE [AppId] = @{nameof(softwareId)}")
|
|
.SetParameter(nameof(softwareId), softwareId)
|
|
.ExecuteReader(x => new SoftwareFunction
|
|
{
|
|
SoftwareId = x.GetSmallint(),
|
|
FunctionId = x.GetInt(),
|
|
Function = x.GetString()
|
|
});
|
|
|
|
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.GetString());
|
|
|
|
public short Insert(string assembly, int major, int minor, int 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.GetSmallint());
|
|
|
|
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.GetSmallint(),
|
|
AssemblyName = x.GetString(),
|
|
CurrentVersion = $"{x.GetInt()}.{x.GetInt()}.{x.GetInt()}",
|
|
VersionId = x.GetInt(),
|
|
ValidUntil = $"{x.GetDate():yyyy-MM-dd}",
|
|
Description = x.GetString(),
|
|
});
|
|
|
|
public short SoftwareId(string assembly)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [AppId]
|
|
FROM [VersionSoftwareAccess]
|
|
WHERE [SoftwareAccess_Program] = @{nameof(assembly)}")
|
|
.SetParameter(nameof(assembly), assembly)
|
|
.FirstOrDefault(x => x.GetSmallint());
|
|
|
|
public IEnumerable<SoftwareVersion> Versions(short 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.GetSmallint(),
|
|
AssemblyName = x.GetString(),
|
|
VersionId = x.GetInt(),
|
|
VersionNr = $"{x.GetInt()}.{x.GetInt()}.{x.GetInt()}",
|
|
ValidUntil = $"{x.GetDate():yyyy-MM-dd}",
|
|
Description = x.GetString(),
|
|
});
|
|
}
|
|
}
|