Innovenergy_trunk/csharp/App/Backend/Database/Db.cs

215 lines
5.0 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Diagnostics.CodeAnalysis;
2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Model.Relations;
using InnovEnergy.App.Backend.Utils;
2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Utils;
using SQLite;
2023-03-08 12:20:33 +00:00
namespace InnovEnergy.App.Backend.Database;
2023-02-16 12:57:06 +00:00
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 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<Installation> GetAllAccessibleInstallations(User user)
{
2023-03-08 14:45:53 +00:00
var direct = GetDirectlyAccessibleInstallations(user);
2023-02-16 12:57:06 +00:00
var fromFolders = GetAllAccessibleFolders(user)
2023-03-08 14:45:53 +00:00
.SelectMany(GetChildInstallations);
2023-02-16 12:57:06 +00:00
2023-03-08 14:45:53 +00:00
return direct
.Concat(fromFolders)
.Distinct();
2023-02-16 12:57:06 +00:00
}
2023-02-16 12:57:06 +00:00
public IEnumerable<Folder> GetAllAccessibleFolders(User user)
{
return GetDirectlyAccessibleFolders(user)
2023-03-08 14:45:53 +00:00
.SelectMany(GetDescendantFolders)
.Distinct();
// Distinct because the user might have direct access
// to a child folder of a folder he has already access to
2023-02-16 12:57:06 +00:00
}
2023-02-16 12:57:06 +00:00
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
2023-02-16 12:57:06 +00:00
}
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;
2023-02-16 12:57:06 +00:00
}
public Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)
{
var con = new User2Installation
{
UserId = userId,
InstallationId = updatedInstallationId
};
2023-02-16 12:57:06 +00:00
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
{
UserId = userId,
FolderId = updatedFolderId
};
2023-02-16 12:57:06 +00:00
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.Delete(u => u.UserId == id);
}
catch (Exception e)
{
return Result.Error(e);
}
return Result.Ok;
}
2023-02-16 12:57:06 +00:00
}