Need to fix date format

This commit is contained in:
2024-11-18 21:08:13 -07:00
commit 2391462500
32 changed files with 1502 additions and 0 deletions

View File

@ -0,0 +1,123 @@
using ImmichToSlideshow.Models.Immich;
using ImmichToSlideshow.Models;
using Npgsql;
using System.Collections.ObjectModel;
using System.Data;
using System.Text;
using System.Text.Json;
namespace ImmichToSlideshow.Services;
public class AssetService
{
private readonly AppSettings _AppSettings;
public AssetService(AppSettings appSettings) =>
_AppSettings = appSettings;
private static readonly List<Asset> _AssetsRepository = [];
private static string GetCommandText()
{ // cSpell:disable
List<string> results = new();
// results.Add(" SELECT COALESCE(SUM(checksum_failures), 0) ");
// results.Add(" FROM pg_stat_database ");
// results.Add(" SELECT json_agg(t) ");
// results.Add(" FROM information_schema.tables t ");
// results.Add(" WHERE table_schema='public' ");
// results.Add(" AND table_type='BASE TABLE' ");
// results.Add(" SELECT json_agg(c) ");
// results.Add(" FROM information_schema.columns c ");
// results.Add(" WHERE table_name ='assets' ");
// results.Add(" WHERE table_name ='libraries' ");
// results.Add(" WHERE table_name ='asset_files' ");
results.Add(" SELECT json_agg(j) ");
results.Add(" FROM ( ");
results.Add(" SELECT a.* ");
results.Add(" , f.\"path\" ");
results.Add(" FROM assets a ");
// results.Add(" FROM asset_files f ");
results.Add(" INNER ");
results.Add(" JOIN asset_files f ");
results.Add(" ON a.\"id\" = f.\"assetId\" ");
results.Add(" AND f.\"type\" = 'preview' ");
results.Add(" WHERE a.\"status\" = 'active' ");
// results.Add(" WHERE f.\"assetId\" = '4c1933ce-f5b3-4348-bcc3-978f99823d70' ");
results.Add(" AND a.\"isExternal\" = true ");
results.Add(" AND a.\"isOffline\" = false ");
results.Add(" AND a.\"isVisible\" = true ");
// results.Add(" AND a.\"id\" = '4c1933ce-f5b3-4348-bcc3-978f99823d70' ");
// results.Add(" AND a.\"originalFileName\"");
// results.Add(" LIKE '%still%' ");
// results.Add(" AND a.\"originalFileName\" = '979270910999.jpg' ");
results.Add(" ) j ");
return string.Join(Environment.NewLine, results);
} // cSpell:enable
private static int? ExecuteNonQuery(string connectionString, string commandText)
{
int? result;
if (string.IsNullOrEmpty(connectionString))
result = null;
else
{
using NpgsqlConnection npgsqlConnection = new(connectionString);
npgsqlConnection.Open();
using NpgsqlCommand npgsqlCommand = new(commandText, npgsqlConnection);
result = npgsqlCommand.ExecuteNonQuery();
}
return result;
}
private static StringBuilder GetForJsonPath(string connectionString, string commandText)
{
StringBuilder stringBuilder = new();
using NpgsqlConnection npgsqlConnection = new(connectionString);
npgsqlConnection.Open();
using NpgsqlCommand npgsqlCommand = new(commandText, npgsqlConnection);
NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
while (npgsqlDataReader.Read())
_ = stringBuilder.Append(npgsqlDataReader.GetString(0));
return stringBuilder;
}
public ReadOnlyCollection<Asset>? Get()
{
string commandText = GetCommandText();
if (commandText.Length == 1)
{
int? result = ExecuteNonQuery(_AppSettings.ConnectionString, commandText);
if (result is null)
{ }
}
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText);
if (commandText.Length == 1)
File.WriteAllText(".vscode/jsonl/.jsonl", stringBuilder.ToString());
string json = stringBuilder.ToString();
Asset[]? assets = JsonSerializer.Deserialize(json, AssetCollectionSourceGenerationContext.Default.AssetArray);
return assets?.AsReadOnly();
}
// 1. fetch user
// 1. fetch asset
// 1. check wether the user reached the
// 1. update the user
// 1. save the asset
public void Create(Asset asset)
{
// Guid userId,
if (asset is null)
throw new ArgumentNullException(nameof(asset));
// User user = _UsersRepository.Find(x => x.Id == userId)
// ?? throw new InvalidOperationException();
// user.AddAsset(asset);
_AssetsRepository.Add(asset);
}
public Asset? Get(Guid assetId) =>
_AssetsRepository.Find(l => l.Id == assetId.ToString());
}