2023-03-15 13:38:06 +00:00
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
|
|
using InnovEnergy.App.Backend.Relations;
|
|
|
|
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.Database;
|
|
|
|
|
|
|
|
|
|
|
|
public static partial class Db
|
|
|
|
{
|
|
|
|
public static Folder? GetFolderById(Int64 id)
|
|
|
|
{
|
|
|
|
return Folders
|
|
|
|
.FirstOrDefault(f => f.Id == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Installation? GetInstallationById(Int64 id)
|
|
|
|
{
|
|
|
|
return Installations
|
|
|
|
.FirstOrDefault(i => i.Id == id);
|
|
|
|
}
|
|
|
|
|
2023-03-16 12:33:51 +00:00
|
|
|
public static User? GetUserById(Int64? id)
|
2023-03-15 13:38:06 +00:00
|
|
|
{
|
|
|
|
return Users
|
|
|
|
.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)
|
|
|
|
{
|
|
|
|
return Users
|
|
|
|
.FirstOrDefault(u => u.Email == email);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Session? GetSession(String token)
|
|
|
|
{
|
|
|
|
var session = Sessions
|
|
|
|
.FirstOrDefault(s => s.Token == token);
|
|
|
|
|
|
|
|
// cannot use session.Valid 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 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|