Remove Result class;

This commit is contained in:
ig 2023-03-09 16:32:11 +01:00
parent 9d753a8471
commit d885fa94c6
5 changed files with 86 additions and 125 deletions

View File

@ -31,7 +31,6 @@ public partial class Db : IDisposable
db.CreateTable<User2Installation>(); db.CreateTable<User2Installation>();
db.CreateTable<Session>(); db.CreateTable<Session>();
}); });
} }
// private, force access through Connect() // private, force access through Connect()
@ -43,48 +42,59 @@ public partial class Db : IDisposable
// the C in CRUD // the C in CRUD
private Result Create(TreeNode treeNode) private Int64 Create(TreeNode treeNode)
{ {
try try
{ {
_Db.Insert(treeNode); _Db.Insert(treeNode);
return SQLite3.LastInsertRowid(_Db.Handle);
} }
catch (Exception e) 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 // the U in CRUD
private Result Update(TreeNode treeNode) private Boolean Update(TreeNode treeNode)
{ {
try try
{ {
_Db.InsertOrReplace(treeNode); _Db.InsertOrReplace(treeNode);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
return Result.Error(e); return false;
} }
return Result.Ok;
} }
// the D in CRUD // the D in CRUD
private Result Delete(TreeNode treeNode) private Boolean Delete(TreeNode treeNode)
{ {
try try
{ {
_Db.Delete(treeNode); _Db.Delete(treeNode);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
return Result.Error(e); return false;
} }
return Result.Ok;
} }
public IEnumerable<Installation> GetAllAccessibleInstallations(User user) public IEnumerable<Installation> GetAllAccessibleInstallations(User user)
@ -129,7 +139,7 @@ public partial class Db : IDisposable
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user; .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 var con = new User2Installation
{ {
@ -139,17 +149,16 @@ public partial class Db : IDisposable
try try
{ {
_Db.InsertOrReplace(con); _Db.Insert(con);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
return Result.Error(e); return false;
}
} }
return Result.Ok; public Boolean AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
}
public Result AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
{ {
var con = new User2Folder var con = new User2Folder
{ {
@ -159,18 +168,16 @@ public partial class Db : IDisposable
try try
{ {
_Db.InsertOrReplace(con); _Db.Insert(con);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
return Result.Error(e); return false;
} }
return Result.Ok;
} }
public User? GetUserByToken(String token) public User? GetUserByToken(String token)
{ {
return Sessions return Sessions
@ -182,32 +189,19 @@ public partial class Db : IDisposable
} }
public Result NewSession(Session ses) public Boolean NewSession(Session ses) => Create(ses);
{
try
{
_Db.InsertOrReplace(ses);
}
catch (Exception e)
{
return Result.Error(e);
}
return Result.Ok; public Boolean DeleteSession(Int64 id)
}
public Result DeleteSession(Int64 id)
{ {
try try
{ {
Sessions.Delete(u => u.UserId == id); Sessions.Delete(u => u.UserId == id);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
return Result.Error(e); return false;
} }
return Result.Ok;
} }
public Object? GetInstallationS3Key(Int64 installationId) public Object? GetInstallationS3Key(Int64 installationId)
@ -226,4 +220,3 @@ public partial class Db : IDisposable
} }
} }
} }

View File

@ -46,31 +46,31 @@ public partial class Db
return parent.Traverse(GetChildUsers); return parent.Traverse(GetChildUsers);
} }
public Result CreateFolder(Folder folder) public Int64 CreateFolder(Folder folder)
{ {
return Create(folder); return Create(folder);
} }
public Result UpdateFolder(Folder folder) public Boolean UpdateFolder(Folder folder)
{ {
// TODO: no circles in path // TODO: no circles in path
return Update(folder); return Update(folder);
} }
public Result ChangeParent(Installation child, Int64 parentId) public Boolean ChangeParent(Installation child, Int64 parentId)
{ {
child.ParentId = parentId; child.ParentId = parentId;
return UpdateInstallation(child); return UpdateInstallation(child);
} }
public Result ChangeParent(Folder child, Int64 parentId) public Boolean ChangeParent(Folder child, Int64 parentId)
{ {
child.ParentId = parentId; child.ParentId = parentId;
return UpdateFolder(child); return UpdateFolder(child);
} }
public Result DeleteFolder(Folder folder) public Boolean DeleteFolder(Folder folder)
{ {
// Delete direct children // Delete direct children
User2Folder .Delete(f => f.FolderId == folder.Id); User2Folder .Delete(f => f.FolderId == folder.Id);

View File

@ -14,18 +14,18 @@ public partial class Db
public Installation? GetInstallationById(Int64 id) => Installations public Installation? GetInstallationById(Int64 id) => Installations
.FirstOrDefault(u => u.Id == id); .FirstOrDefault(u => u.Id == id);
public Result CreateInstallation(Installation installation) public Int64 CreateInstallation(Installation installation)
{ {
return Create(installation); return Create(installation);
} }
public Result UpdateInstallation(Installation installation) public Boolean UpdateInstallation(Installation installation)
{ {
return Update(installation); return Update(installation);
} }
public Result DeleteInstallation(Installation installation) public Boolean DeleteInstallation(Installation installation)
{ {
User2Installation.Delete(i => i.InstallationId == installation.Id); User2Installation.Delete(i => i.InstallationId == installation.Id);

View File

@ -1,16 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Mail; using System.Net.Mail;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Flurl.Http;
using InnovEnergy.App.Backend.Model; using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using SQLite; using SQLite;
using ResponseExtensions = Flurl.Http.ResponseExtensions;
#pragma warning disable CS0472 #pragma warning disable CS0472
#pragma warning disable CS8602 #pragma warning disable CS8602
@ -53,10 +50,10 @@ public partial class Db
public User? GetUserByEmail(String email) => Users.FirstOrDefault(u => u.Email == email); 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) 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 //Salting and Hashing password
var salt = Crypto.GenerateSalt(); var salt = Crypto.GenerateSalt();
@ -195,11 +192,11 @@ public partial class Db
return newKey; return newKey;
} }
public Result UpdateUser(User user) public Boolean UpdateUser(User user)
{ {
var oldUser = GetUserById(user.Id); var oldUser = GetUserById(user.Id);
if (oldUser == null) if (oldUser == null)
return Result.Error("User doesn't exist"); return false; // TODO: "User doesn't exist"
//Checking for unchangeable things //Checking for unchangeable things
// TODO: depends on privileges of caller // TODO: depends on privileges of caller
@ -211,7 +208,7 @@ public partial class Db
return Update(user); return Update(user);
} }
public Result DeleteUser(User user) public Boolean DeleteUser(User user)
{ {
User2Folder.Delete(u => u.UserId == user.Id); User2Folder.Delete(u => u.UserId == user.Id);
User2Installation.Delete(u => u.UserId == user.Id); User2Installation.Delete(u => u.UserId == user.Id);

View File

@ -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);
}
}