using System.Diagnostics.CodeAnalysis; using Backend.Model; using Backend.Model.Relations; using Backend.Utils; using InnovEnergy.Lib.Utils; using SQLite; namespace Backend.Database; public partial class Db : IDisposable { internal const String DbPath = "./db.sqlite"; private readonly SQLiteConnection _Db; // internal handle to the connection, disposable private TableQuery Sessions => _Db.Table(); [SuppressMessage("ReSharper", "AccessToDisposedClosure")] static Db() { // on startup create/migrate tables using var db = new SQLiteConnection(DbPath); db.RunInTransaction(() => { db.CreateTable(); db.CreateTable(); db.CreateTable(); db.CreateTable(); db.CreateTable(); db.CreateTable(); }); } // private, force access through Connect() private Db() => _Db = new SQLiteConnection(DbPath); public static Db Connect() => new Db(); public void Dispose() => _Db.Dispose(); // the C in CRUD private Result Create(TreeNode treeNode) { try { _Db.Insert(treeNode); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } // the U in CRUD private Result Update(TreeNode treeNode) { try { _Db.InsertOrReplace(treeNode); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } // the D in CRUD private Result Delete(TreeNode treeNode) { try { _Db.Delete(treeNode); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } public IEnumerable GetAllAccessibleInstallations(User user) { var direct = GetDirectlyAccessibleInstallations(user).ToList(); var fromFolders = GetAllAccessibleFolders(user) .SelectMany(GetChildInstallations) .Except(direct); return direct.Concat(fromFolders); } public IEnumerable GetAllAccessibleInstallationIds(User user) { var direct = GetDirectlyAccessibleInstallationIds(user); var fromFolders = GetAllAccessibleFolders(user) .SelectMany(GetChildInstallations) .Select(installation => installation.Id); return direct.Concat(fromFolders); } public IEnumerable GetAllAccessibleFolders(User user) { return GetDirectlyAccessibleFolders(user) .SelectMany(GetDescendantFolders); } public IEnumerable GetAllAccessibleFolderIds(User user) { return GetDirectlyAccessibleFolders(user) .SelectMany(GetDescendantFolders) .Select(folder => folder.Id ); } public IEnumerable GetDirectlyAccessibleInstallations(User user) { return User2Installation .Where(r => r.UserId == user.Id) .Select(r => r.InstallationId) .Select(GetInstallationById) .NotNull(); } public IEnumerable GetDirectlyAccessibleInstallationIds(User user) { return User2Installation .Where(r => r.UserId == user.Id) .Select(r => r.InstallationId) .NotNull(); } public IEnumerable GetDirectlyAccessibleFolders(User user) { return User2Folder .Where(r => r.UserId == user.Id) .Select(r => r.FolderId) .Select(GetFolderById) .NotNull(); } public Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId) { var con = new User2Installation(); con.UserId = userId; con.InstallationId = updatedInstallationId; try { _Db.InsertOrReplace(con); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } public Result AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId) { var con = new User2Folder(); con.UserId = userId; con.FolderId = updatedFolderId; try { _Db.InsertOrReplace(con); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } public User? GetUserByToken(String token) { return Sessions .Where(s => s.Token == token).ToList() .Where(s => s.Valid) .Select(s => s.UserId) .Select(GetUserById) .FirstOrDefault(); } public Result NewSession(Session ses) { try { _Db.InsertOrReplace(ses); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } public Result DeleteSession(Int64 id) { try { Sessions .Where(u => u.UserId == id) .Delete(); } catch (Exception e) { return Result.Error(e); } return Result.Ok; } }