wabbajack/Wabbajack.Server/DataLayer/AuthoredFiles.cs

79 lines
3.1 KiB
C#
Raw Normal View History

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
namespace Wabbajack.Server.DataLayer
{
public partial class SqlService
{
2021-09-27 12:42:46 +00:00
public async Task TouchAuthoredFile(FileDefinition definition, DateTime? date = null)
2020-05-09 22:16:16 +00:00
{
await using var conn = await Open();
2021-02-06 16:43:11 +00:00
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-09-27 12:42:46 +00:00
public async Task<FileDefinition> CreateAuthoredFile(FileDefinition definition, string login)
2020-05-09 22:16:16 +00:00
{
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;
}
2021-09-27 12:42:46 +00:00
public async Task Finalize(FileDefinition definition)
2020-05-09 22:16:16 +00:00
{
await using var conn = await Open();
await conn.ExecuteAsync("UPDATE AuthoredFiles SET LastTouched = GETUTCDATE(), Finalized = GETUTCDATE() WHERE ServerAssignedUniqueId = @Uid",
new {
Uid = definition.ServerAssignedUniqueId
});
}
2021-09-27 12:42:46 +00:00
public async Task<FileDefinition> GetCDNFileDefinition(string serverAssignedUniqueId)
2020-05-09 22:16:16 +00:00
{
await using var conn = await Open();
2021-09-27 12:42:46 +00:00
return (await conn.QueryAsync<FileDefinition>(
2020-05-09 22:16:16 +00:00
"SELECT CDNFileDefinition FROM dbo.AuthoredFiles WHERE ServerAssignedUniqueID = @Uid",
new {Uid = serverAssignedUniqueId})).First();
}
2021-09-27 12:42:46 +00:00
public async Task DeleteFileDefinition(FileDefinition definition)
2020-05-09 22:16:16 +00:00
{
await using var conn = await Open();
2021-02-06 16:43:11 +00:00
await conn.ExecuteAsync(
2020-05-09 22:16:16 +00:00
"DELETE FROM dbo.AuthoredFiles WHERE ServerAssignedUniqueID = @Uid",
2021-02-06 16:43:11 +00:00
new {Uid = definition.ServerAssignedUniqueId});
return;
2020-05-09 22:16:16 +00:00
}
2020-05-10 01:35:42 +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;
}
2021-02-06 16:43:11 +00:00
2020-05-09 22:16:16 +00:00
}
}