2020-07-15 22:29:43 +00:00
using System.Collections.Generic ;
using System.Linq ;
2020-05-13 03:39:03 +00:00
using System.Threading.Tasks ;
using Dapper ;
using Wabbajack.Lib ;
using Wabbajack.Lib.ModListRegistry ;
using Wabbajack.Common ;
2020-07-15 22:29:43 +00:00
using Wabbajack.Lib.Downloaders ;
2020-05-13 03:39:03 +00:00
namespace Wabbajack.Server.DataLayer
{
public partial class SqlService
{
2020-05-13 12:09:20 +00:00
public async Task IngestModList ( Hash hash , ModlistMetadata metadata , ModList modlist , bool brokenDownload )
2020-05-13 03:39:03 +00:00
{
await using var conn = await Open ( ) ;
await using var tran = await conn . BeginTransactionAsync ( ) ;
await conn . ExecuteAsync ( @"DELETE FROM dbo.ModLists Where MachineUrl = @MachineUrl" ,
new { MachineUrl = metadata . Links . MachineURL } , tran ) ;
2020-07-15 22:29:43 +00:00
var archives = modlist . Archives ;
var directives = modlist . Directives ;
modlist . Archives = new List < Archive > ( ) ;
modlist . Directives = new List < Directive > ( ) ;
2020-05-13 03:39:03 +00:00
await conn . ExecuteAsync (
2020-05-13 12:09:20 +00:00
@"INSERT INTO dbo.ModLists (MachineUrl, Hash, Metadata, ModList, BrokenDownload) VALUES (@MachineUrl, @Hash, @Metadata, @ModList, @BrokenDownload)" ,
2020-05-13 03:39:03 +00:00
new
{
MachineUrl = metadata . Links . MachineURL ,
Hash = hash ,
MetaData = metadata . ToJson ( ) ,
2020-05-13 12:09:20 +00:00
ModList = modlist . ToJson ( ) ,
BrokenDownload = brokenDownload
2020-05-13 03:39:03 +00:00
} , tran ) ;
2020-07-15 22:29:43 +00:00
var entries = archives . Select ( a = >
2020-05-13 03:39:03 +00:00
new
{
MachineUrl = metadata . Links . MachineURL ,
2020-07-16 12:28:05 +00:00
Name = a . Name ,
2020-05-13 03:39:03 +00:00
Hash = a . Hash ,
Size = a . Size ,
State = a . State . ToJson ( ) ,
PrimaryKeyString = a . State . PrimaryKeyString
} ) . ToArray ( ) ;
await conn . ExecuteAsync ( @"DELETE FROM dbo.ModListArchives WHERE MachineURL = @machineURL" ,
new { MachineUrl = metadata . Links . MachineURL } , tran ) ;
foreach ( var entry in entries )
{
await conn . ExecuteAsync (
2020-07-16 12:28:05 +00:00
"INSERT INTO dbo.ModListArchives (MachineURL, Name, Hash, Size, PrimaryKeyString, State) VALUES (@MachineURL, @Name, @Hash, @Size, @PrimaryKeyString, @State)" ,
2020-05-13 03:39:03 +00:00
entry , tran ) ;
}
await tran . CommitAsync ( ) ;
}
public async Task < bool > HaveIndexedModlist ( string machineUrl , Hash hash )
{
await using var conn = await Open ( ) ;
var result = await conn . QueryFirstOrDefaultAsync < string > (
"SELECT MachineURL from dbo.Modlists WHERE MachineURL = @MachineUrl AND Hash = @Hash" ,
new { MachineUrl = machineUrl , Hash = hash } ) ;
return result ! = null ;
}
2020-06-05 20:53:44 +00:00
public async Task < bool > HashIsInAModlist ( Hash hash )
{
await using var conn = await Open ( ) ;
var result = await conn . QueryFirstOrDefaultAsync < bool > ( "SELECT Hash FROM dbo.ModListArchives Where Hash = @Hash" ,
new { Hash = hash } ) ;
return result ;
}
2020-07-15 22:29:43 +00:00
public async Task < List < Archive > > ModListArchives ( string machineURL )
{
await using var conn = await Open ( ) ;
2020-07-16 12:28:05 +00:00
var archives = await conn . QueryAsync < ( string , Hash , long , AbstractDownloadState ) > ( "SELECT Name, Hash, Size, State FROM dbo.ModListArchives WHERE MachineUrl = @MachineUrl" ,
2020-07-25 18:09:02 +00:00
new { MachineUrl = machineURL } ) ;
return archives . Select ( t = > new Archive ( t . Item4 )
{
Name = string . IsNullOrWhiteSpace ( t . Item1 ) ? t . Item4 . PrimaryKeyString : t . Item1 ,
Size = t . Item3 ,
Hash = t . Item2
} ) . ToList ( ) ;
}
public async Task < List < Archive > > ModListArchives ( )
{
await using var conn = await Open ( ) ;
var archives = await conn . QueryAsync < ( string , Hash , long , AbstractDownloadState ) > ( "SELECT Name, Hash, Size, State FROM dbo.ModListArchives" ) ;
2020-07-16 12:28:05 +00:00
return archives . Select ( t = > new Archive ( t . Item4 )
{
Name = string . IsNullOrWhiteSpace ( t . Item1 ) ? t . Item4 . PrimaryKeyString : t . Item1 ,
Size = t . Item3 ,
Hash = t . Item2
} ) . ToList ( ) ;
2020-07-15 22:29:43 +00:00
}
2020-05-13 03:39:03 +00:00
}
}