221 lines
5.2 KiB
C#
221 lines
5.2 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using InnovEnergy.App.Backend.Model;
|
|
using InnovEnergy.App.Backend.Model.Relations;
|
|
using InnovEnergy.App.Backend.Utils;
|
|
using InnovEnergy.Lib.Utils;
|
|
using SQLite;
|
|
|
|
namespace InnovEnergy.App.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<Session> Sessions => _Db.Table<Session>();
|
|
|
|
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
|
static Db()
|
|
{
|
|
// on startup create/migrate tables
|
|
|
|
using var db = new SQLiteConnection(DbPath);
|
|
|
|
db.RunInTransaction(() =>
|
|
{
|
|
db.CreateTable<User>();
|
|
db.CreateTable<Installation>();
|
|
db.CreateTable<Folder>();
|
|
db.CreateTable<User2Folder>();
|
|
db.CreateTable<User2Installation>();
|
|
db.CreateTable<Session>();
|
|
});
|
|
}
|
|
|
|
// 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 Int64 Create(TreeNode treeNode)
|
|
{
|
|
try
|
|
{
|
|
_Db.Insert(treeNode);
|
|
return SQLite3.LastInsertRowid(_Db.Handle);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
private Boolean Create(Session session)
|
|
{
|
|
try
|
|
{
|
|
_Db.Insert(session);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// the U in CRUD
|
|
private Boolean Update(TreeNode treeNode)
|
|
{
|
|
try
|
|
{
|
|
_Db.InsertOrReplace(treeNode);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// the D in CRUD
|
|
private Boolean Delete(TreeNode treeNode)
|
|
{
|
|
try
|
|
{
|
|
_Db.Delete(treeNode);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Installation> GetAllAccessibleInstallations(User user)
|
|
{
|
|
var direct = GetDirectlyAccessibleInstallations(user);
|
|
var fromFolders = GetAllAccessibleFolders(user)
|
|
.SelectMany(GetChildInstallations);
|
|
|
|
return direct
|
|
.Concat(fromFolders)
|
|
.Distinct();
|
|
}
|
|
|
|
public IEnumerable<Folder> GetAllAccessibleFolders(User user)
|
|
{
|
|
return GetDirectlyAccessibleFolders(user)
|
|
.SelectMany(GetDescendantFolders)
|
|
.Distinct();
|
|
|
|
// Distinct because the user might have direct access
|
|
// to a child folder of a folder he has already access to
|
|
}
|
|
|
|
|
|
public IEnumerable<Installation> GetDirectlyAccessibleInstallations(User user)
|
|
{
|
|
return User2Installation
|
|
.Where(r => r.UserId == user.Id)
|
|
.Select(r => r.InstallationId)
|
|
.Select(GetInstallationById)
|
|
.NotNull()
|
|
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user
|
|
}
|
|
|
|
public IEnumerable<Folder> GetDirectlyAccessibleFolders(User user)
|
|
{
|
|
return User2Folder
|
|
.Where(r => r.UserId == user.Id)
|
|
.Select(r => r.FolderId)
|
|
.Select(GetFolderById)
|
|
.NotNull()
|
|
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user;
|
|
}
|
|
|
|
public Boolean AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)
|
|
{
|
|
var con = new User2Installation
|
|
{
|
|
UserId = userId,
|
|
InstallationId = updatedInstallationId
|
|
};
|
|
|
|
try
|
|
{
|
|
_Db.Insert(con);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public Boolean AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
|
|
{
|
|
var con = new User2Folder
|
|
{
|
|
UserId = userId,
|
|
FolderId = updatedFolderId
|
|
};
|
|
|
|
try
|
|
{
|
|
_Db.Insert(con);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
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 Boolean NewSession(Session ses) => Create(ses);
|
|
|
|
public Boolean DeleteSession(Int64 id)
|
|
{
|
|
try
|
|
{
|
|
Sessions.Delete(u => u.UserId == id);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public Object? GetInstallationS3Key(Int64 installationId)
|
|
{
|
|
return Installations
|
|
.FirstOrDefault(installation => installation.Id == installationId).S3Key;
|
|
}
|
|
|
|
public void DeleteS3KeysDaily()
|
|
{
|
|
foreach (var installation in Installations.ToList())
|
|
{
|
|
installation.S3Key = null;
|
|
Update(installation);
|
|
}
|
|
}
|
|
} |