Remove Result class;
This commit is contained in:
parent
9d753a8471
commit
d885fa94c6
|
@ -31,7 +31,6 @@ public partial class Db : IDisposable
|
|||
db.CreateTable<User2Installation>();
|
||||
db.CreateTable<Session>();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// private, force access through Connect()
|
||||
|
@ -43,66 +42,77 @@ public partial class Db : IDisposable
|
|||
|
||||
|
||||
// the C in CRUD
|
||||
private Result Create(TreeNode treeNode)
|
||||
private Int64 Create(TreeNode treeNode)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Db.Insert(treeNode);
|
||||
return SQLite3.LastInsertRowid(_Db.Handle);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
|
||||
private Boolean Create(Session session)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Db.Insert(session);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// the U in CRUD
|
||||
private Result Update(TreeNode treeNode)
|
||||
private Boolean Update(TreeNode treeNode)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Db.InsertOrReplace(treeNode);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
// the D in CRUD
|
||||
private Result Delete(TreeNode treeNode)
|
||||
private Boolean Delete(TreeNode treeNode)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Db.Delete(treeNode);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
public IEnumerable<Installation> GetAllAccessibleInstallations(User user)
|
||||
{
|
||||
var direct = GetDirectlyAccessibleInstallations(user);
|
||||
var direct = GetDirectlyAccessibleInstallations(user);
|
||||
var fromFolders = GetAllAccessibleFolders(user)
|
||||
.SelectMany(GetChildInstallations);
|
||||
.SelectMany(GetChildInstallations);
|
||||
|
||||
return direct
|
||||
.Concat(fromFolders)
|
||||
.Distinct();
|
||||
.Concat(fromFolders)
|
||||
.Distinct();
|
||||
}
|
||||
|
||||
public IEnumerable<Folder> GetAllAccessibleFolders(User user)
|
||||
{
|
||||
return GetDirectlyAccessibleFolders(user)
|
||||
.SelectMany(GetDescendantFolders)
|
||||
.Distinct();
|
||||
.SelectMany(GetDescendantFolders)
|
||||
.Distinct();
|
||||
|
||||
// Distinct because the user might have direct access
|
||||
// to a child folder of a folder he has already access to
|
||||
|
@ -112,24 +122,24 @@ public partial class Db : IDisposable
|
|||
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
|
||||
.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;
|
||||
.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 Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)
|
||||
public Boolean AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)
|
||||
{
|
||||
var con = new User2Installation
|
||||
{
|
||||
|
@ -139,17 +149,16 @@ public partial class Db : IDisposable
|
|||
|
||||
try
|
||||
{
|
||||
_Db.InsertOrReplace(con);
|
||||
_Db.Insert(con);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
public Result AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
|
||||
public Boolean AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
|
||||
{
|
||||
var con = new User2Folder
|
||||
{
|
||||
|
@ -159,18 +168,16 @@ public partial class Db : IDisposable
|
|||
|
||||
try
|
||||
{
|
||||
_Db.InsertOrReplace(con);
|
||||
_Db.Insert(con);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public User? GetUserByToken(String token)
|
||||
{
|
||||
return Sessions
|
||||
|
@ -182,39 +189,26 @@ public partial class Db : IDisposable
|
|||
}
|
||||
|
||||
|
||||
public Result NewSession(Session ses)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Db.InsertOrReplace(ses);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
}
|
||||
public Boolean NewSession(Session ses) => Create(ses);
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
public Result DeleteSession(Int64 id)
|
||||
public Boolean DeleteSession(Int64 id)
|
||||
{
|
||||
try
|
||||
{
|
||||
Sessions.Delete(u => u.UserId == id);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
}
|
||||
|
||||
public Object? GetInstallationS3Key(Int64 installationId)
|
||||
{
|
||||
return Installations
|
||||
.Where(installation => installation.Id == installationId)
|
||||
.Select(installation => installation.S3Key);
|
||||
.Where(installation => installation.Id == installationId)
|
||||
.Select(installation => installation.S3Key);
|
||||
}
|
||||
|
||||
public void DeleteS3KeysDaily()
|
||||
|
@ -226,4 +220,3 @@ public partial class Db : IDisposable
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,31 +46,31 @@ public partial class Db
|
|||
return parent.Traverse(GetChildUsers);
|
||||
}
|
||||
|
||||
public Result CreateFolder(Folder folder)
|
||||
public Int64 CreateFolder(Folder folder)
|
||||
{
|
||||
return Create(folder);
|
||||
}
|
||||
|
||||
public Result UpdateFolder(Folder folder)
|
||||
public Boolean UpdateFolder(Folder folder)
|
||||
{
|
||||
// TODO: no circles in path
|
||||
|
||||
return Update(folder);
|
||||
}
|
||||
|
||||
public Result ChangeParent(Installation child, Int64 parentId)
|
||||
public Boolean ChangeParent(Installation child, Int64 parentId)
|
||||
{
|
||||
child.ParentId = parentId;
|
||||
return UpdateInstallation(child);
|
||||
}
|
||||
|
||||
public Result ChangeParent(Folder child, Int64 parentId)
|
||||
public Boolean ChangeParent(Folder child, Int64 parentId)
|
||||
{
|
||||
child.ParentId = parentId;
|
||||
return UpdateFolder(child);
|
||||
}
|
||||
|
||||
public Result DeleteFolder(Folder folder)
|
||||
public Boolean DeleteFolder(Folder folder)
|
||||
{
|
||||
// Delete direct children
|
||||
User2Folder .Delete(f => f.FolderId == folder.Id);
|
||||
|
|
|
@ -14,18 +14,18 @@ public partial class Db
|
|||
public Installation? GetInstallationById(Int64 id) => Installations
|
||||
.FirstOrDefault(u => u.Id == id);
|
||||
|
||||
public Result CreateInstallation(Installation installation)
|
||||
public Int64 CreateInstallation(Installation installation)
|
||||
{
|
||||
return Create(installation);
|
||||
}
|
||||
|
||||
public Result UpdateInstallation(Installation installation)
|
||||
public Boolean UpdateInstallation(Installation installation)
|
||||
{
|
||||
return Update(installation);
|
||||
}
|
||||
|
||||
|
||||
public Result DeleteInstallation(Installation installation)
|
||||
public Boolean DeleteInstallation(Installation installation)
|
||||
{
|
||||
User2Installation.Delete(i => i.InstallationId == installation.Id);
|
||||
|
||||
|
|
|
@ -1,16 +1,13 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Mail;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
using Flurl.Http;
|
||||
using InnovEnergy.App.Backend.Model;
|
||||
using InnovEnergy.App.Backend.Utils;
|
||||
using InnovEnergy.Lib.Utils;
|
||||
using SQLite;
|
||||
using ResponseExtensions = Flurl.Http.ResponseExtensions;
|
||||
|
||||
#pragma warning disable CS0472
|
||||
#pragma warning disable CS8602
|
||||
|
@ -53,10 +50,10 @@ public partial class Db
|
|||
|
||||
public User? GetUserByEmail(String email) => Users.FirstOrDefault(u => u.Email == email);
|
||||
|
||||
public Result CreateUser(User user)
|
||||
public Int64 CreateUser(User user)
|
||||
{
|
||||
if (GetUserByEmail(user.Email) is not null)
|
||||
return Result.Error("User with that email already exists");
|
||||
return -1; // TODO: User with that email already exists
|
||||
|
||||
//Salting and Hashing password
|
||||
var salt = Crypto.GenerateSalt();
|
||||
|
@ -195,23 +192,23 @@ public partial class Db
|
|||
return newKey;
|
||||
}
|
||||
|
||||
public Result UpdateUser(User user)
|
||||
public Boolean UpdateUser(User user)
|
||||
{
|
||||
var oldUser = GetUserById(user.Id);
|
||||
if (oldUser == null)
|
||||
return Result.Error("User doesn't exist");
|
||||
return false; // TODO: "User doesn't exist"
|
||||
|
||||
//Checking for unchangeable things
|
||||
// TODO: depends on privileges of caller
|
||||
|
||||
user.Id = oldUser.Id;
|
||||
user.Id = oldUser.Id;
|
||||
user.ParentId = oldUser.ParentId;
|
||||
user.Email = oldUser.Email;
|
||||
user.Email = oldUser.Email;
|
||||
|
||||
return Update(user);
|
||||
}
|
||||
|
||||
public Result DeleteUser(User user)
|
||||
public Boolean DeleteUser(User user)
|
||||
{
|
||||
User2Folder.Delete(u => u.UserId == user.Id);
|
||||
User2Installation.Delete(u => u.UserId == user.Id);
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
namespace InnovEnergy.App.Backend.Utils;
|
||||
|
||||
public class Result
|
||||
{
|
||||
private const String OkMsg = "Ok";
|
||||
|
||||
private readonly String _Error;
|
||||
|
||||
public static Result Ok = new Result(OkMsg);
|
||||
|
||||
public Boolean IsError => _Error != OkMsg;
|
||||
public Boolean IsSuccess => _Error == OkMsg;
|
||||
|
||||
private Result(String error) => _Error = error;
|
||||
|
||||
public static Result Error(String error) => new Result(error);
|
||||
|
||||
public static Result Error(Exception exception)
|
||||
{
|
||||
|
||||
#if DEBUG
|
||||
var msg = exception.ToString(); // includes stacktrace
|
||||
#else
|
||||
var msg = exception.Message; // excludes stacktrace
|
||||
#endif
|
||||
|
||||
return new Result(msg);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue