diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index 3cd7cf127..1995fbd99 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -16,7 +16,7 @@ public class Controller : ControllerBase [HttpPost(nameof(Login))] public ActionResult Login(String username, String password) { - var user = Db.GetUserByEmail(username); + var user = Db.GetUserByName(username); if (user is null || !user.VerifyPassword(password)) return Unauthorized(); diff --git a/csharp/App/Backend/DataTypes/TreeNode.cs b/csharp/App/Backend/DataTypes/TreeNode.cs index de24da0b8..796ab9cc2 100644 --- a/csharp/App/Backend/DataTypes/TreeNode.cs +++ b/csharp/App/Backend/DataTypes/TreeNode.cs @@ -5,9 +5,9 @@ namespace InnovEnergy.App.Backend.DataTypes; public abstract partial class TreeNode { [PrimaryKey, AutoIncrement] - public Int64 Id { get; set; } - public String Name { get; set; } = ""; - public String Information { get; set; } = ""; // unstructured random info + public Int64 Id { get; set; } + public virtual String Name { get; set; } = ""; // overridden by User (unique) + public String Information { get; set; } = ""; // unstructured random info [Indexed] // parent/child relation public Int64 ParentId { get; set; } diff --git a/csharp/App/Backend/DataTypes/User.cs b/csharp/App/Backend/DataTypes/User.cs index 70ea9e034..449285fe3 100644 --- a/csharp/App/Backend/DataTypes/User.cs +++ b/csharp/App/Backend/DataTypes/User.cs @@ -4,11 +4,13 @@ namespace InnovEnergy.App.Backend.DataTypes; public class User : TreeNode { - [Indexed] public String Email { get; set; } = null!; public Boolean HasWriteAccess { get; set; } = false; public String Language { get; set; } = null!; public String Password { get; set; } = null!; - + + [Unique] + public override String Name { get; set; } = null!; + // TODO: must reset pwd } \ No newline at end of file diff --git a/csharp/App/Backend/Database/Create.cs b/csharp/App/Backend/Database/Create.cs index d1b6d8318..40b40cf2c 100644 --- a/csharp/App/Backend/Database/Create.cs +++ b/csharp/App/Backend/Database/Create.cs @@ -1,5 +1,4 @@ using InnovEnergy.App.Backend.DataTypes; -using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; @@ -21,11 +20,6 @@ public static partial class Db public static Boolean Create(User user) { - if (GetUserByEmail(user.Email) is not null) - return false; - - user.Password = user.SaltAndHashPassword(user.Password); - return Connection.Insert(user) > 0; } diff --git a/csharp/App/Backend/Database/Read.cs b/csharp/App/Backend/Database/Read.cs index 53c0586de..5a4eae7bb 100644 --- a/csharp/App/Backend/Database/Read.cs +++ b/csharp/App/Backend/Database/Read.cs @@ -7,13 +7,13 @@ namespace InnovEnergy.App.Backend.Database; public static partial class Db { - public static Folder? GetFolderById(Int64 id) + public static Folder? GetFolderById(Int64? id) { return Folders .FirstOrDefault(f => f.Id == id); } - public static Installation? GetInstallationById(Int64 id) + public static Installation? GetInstallationById(Int64? id) { return Installations .FirstOrDefault(i => i.Id == id); @@ -25,22 +25,10 @@ public static partial class Db .FirstOrDefault(u => u.Id == id); } - // private!! - private static Session? GetSessionById(Int64 id) - { - #pragma warning disable CS0618 - - return Sessions - .FirstOrDefault(u => u.Id == id); - - #pragma warning restore CS0618 - } - - - public static User? GetUserByEmail(String email) + public static User? GetUserByName(String userName) { return Users - .FirstOrDefault(u => u.Email == email); + .FirstOrDefault(u => u.Name == userName); } public static Session? GetSession(String token) @@ -62,25 +50,4 @@ public static partial class Db return session; } - - public static User? GetUserBySessionToken(String token) - { - var session = Sessions - .FirstOrDefault(s => s.Token == token); - - // cannot user session.Expired in the DB query above. - // It does not exist in the db (IgnoreAttribute) - - if (session is null) - return null; - - if (!session.Valid) - { - Delete(session); - return null; - } - - return GetUserById(session.UserId); - } - } \ No newline at end of file diff --git a/csharp/App/Backend/Database/Update.cs b/csharp/App/Backend/Database/Update.cs index 72aef3015..923d81345 100644 --- a/csharp/App/Backend/Database/Update.cs +++ b/csharp/App/Backend/Database/Update.cs @@ -1,7 +1,4 @@ using InnovEnergy.App.Backend.DataTypes; -using InnovEnergy.App.Backend.DataTypes.Methods; -using InnovEnergy.App.Backend.Relations; - namespace InnovEnergy.App.Backend.Database; @@ -10,54 +7,24 @@ public static partial class Db { public static Boolean Update(Folder folder) { - if (folder.IsRelativeRoot()) // TODO: triple check - { - var original = GetFolderById(folder.Id); - if (original is null) - return false; - - folder.ParentId = original.ParentId; - } - - return Connection.InsertOrReplace(folder) > 0; + return Connection.Update(folder) > 0; } public static Boolean Update(Installation installation) { - if (installation.IsRelativeRoot()) // TODO: triple check - { - var original = GetInstallationById(installation.Id); - if (original is null) - return false; - - installation.ParentId = original.ParentId; - } - - return Connection.InsertOrReplace(installation) > 0; + return Connection.Update(installation) > 0; } - public static Boolean Update(User user) { var originalUser = GetUserById(user.Id); return originalUser is not null - && user.Id == originalUser.Id // these columns must not be modified! - && user.ParentId == originalUser.ParentId - && user.Email == originalUser.Email - && Connection.InsertOrReplace(user) > 0; + && user.ParentId == originalUser.ParentId // these columns must not be modified! + && user.Name == originalUser.Name + && Connection.Update(user) > 0; } - public static Boolean Update(this Session session) - { - #pragma warning disable CS0618 - var originalSession = GetSessionById(session.Id); - #pragma warning restore CS0618 - - return originalSession is not null - && session.Token == originalSession.Token // these columns must not be modified! - && session.UserId == originalSession.UserId - && Connection.InsertOrReplace(session) > 0; - } + } \ No newline at end of file diff --git a/csharp/App/Backend/db.sqlite b/csharp/App/Backend/db.sqlite index 8cddf6109..5ed0b4ad0 100644 Binary files a/csharp/App/Backend/db.sqlite and b/csharp/App/Backend/db.sqlite differ