2020-05-09 22:16:16 +00:00
using System ;
2020-05-10 01:35:42 +00:00
using System.Collections.Generic ;
2020-05-09 22:16:16 +00:00
using System.Linq ;
using System.Threading.Tasks ;
using Dapper ;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs.CDN ;
2020-05-10 01:35:42 +00:00
using Wabbajack.Server.DTOs ;
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Server.DataLayer ;
public partial class SqlService
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
public async Task TouchAuthoredFile ( FileDefinition definition , DateTime ? date = null )
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
await using var conn = await Open ( ) ;
if ( date = = null )
await conn . ExecuteAsync (
"UPDATE AuthoredFiles SET LastTouched = GETUTCDATE() WHERE ServerAssignedUniqueId = @Uid" ,
new { Uid = definition . ServerAssignedUniqueId } ) ;
else
await conn . ExecuteAsync (
"UPDATE AuthoredFiles SET LastTouched = @Date WHERE ServerAssignedUniqueId = @Uid" ,
new { Uid = definition . ServerAssignedUniqueId , Date = date } ) ;
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public async Task < FileDefinition > CreateAuthoredFile ( FileDefinition definition , string login )
{
definition . Author = login ;
var uid = Guid . NewGuid ( ) . ToString ( ) ;
await using var conn = await Open ( ) ;
definition . ServerAssignedUniqueId = uid ;
await conn . ExecuteAsync (
"INSERT INTO dbo.AuthoredFiles (ServerAssignedUniqueId, LastTouched, CDNFileDefinition) VALUES (@Uid, GETUTCDATE(), @CdnFile)" ,
new
{
Uid = uid ,
CdnFile = definition
} ) ;
return definition ;
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public async Task Finalize ( FileDefinition definition )
{
await using var conn = await Open ( ) ;
await conn . ExecuteAsync (
"UPDATE AuthoredFiles SET LastTouched = GETUTCDATE(), Finalized = GETUTCDATE() WHERE ServerAssignedUniqueId = @Uid" ,
new
{
Uid = definition . ServerAssignedUniqueId
} ) ;
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public async Task < FileDefinition > GetCDNFileDefinition ( string serverAssignedUniqueId )
{
await using var conn = await Open ( ) ;
return ( await conn . QueryAsync < FileDefinition > (
"SELECT CDNFileDefinition FROM dbo.AuthoredFiles WHERE ServerAssignedUniqueID = @Uid" ,
new { Uid = serverAssignedUniqueId } ) ) . First ( ) ;
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public async Task DeleteFileDefinition ( FileDefinition definition )
{
await using var conn = await Open ( ) ;
await conn . ExecuteAsync (
"DELETE FROM dbo.AuthoredFiles WHERE ServerAssignedUniqueID = @Uid" ,
new { Uid = definition . ServerAssignedUniqueId } ) ;
}
2021-02-06 16:43:11 +00:00
2021-10-23 16:51:17 +00:00
public async Task < IEnumerable < AuthoredFilesSummary > > AllAuthoredFiles ( )
{
await using var conn = await Open ( ) ;
var results = await conn . QueryAsync < AuthoredFilesSummary > (
"SELECT CONVERT(NVARCHAR(50), ServerAssignedUniqueId) as ServerAssignedUniqueId, Size, OriginalFileName, Author, LastTouched, Finalized, MungedName from dbo.AuthoredFilesSummaries ORDER BY LastTouched DESC" ) ;
return results ;
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}