83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using InnovEnergy.App.Backend.DataTypes;
|
|
using InnovEnergy.App.Backend.Relations;
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.Database;
|
|
|
|
|
|
public static partial class Db
|
|
{
|
|
private static Boolean Insert(Object obj)
|
|
{
|
|
var success = Connection.Insert(obj) > 0;
|
|
if(success) BackupDatabase();
|
|
return success;
|
|
}
|
|
|
|
public static Boolean Create(Installation installation)
|
|
{
|
|
// SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion
|
|
return Insert(installation);
|
|
}
|
|
|
|
public static Boolean Create(Error error)
|
|
{
|
|
return Insert(error);
|
|
}
|
|
|
|
public static Boolean Create(Folder folder)
|
|
{
|
|
return Insert(folder);
|
|
}
|
|
|
|
public static Boolean Create(User user)
|
|
{
|
|
return Insert(user);
|
|
}
|
|
|
|
public static Boolean Create(Session session)
|
|
{
|
|
return Insert(session);
|
|
}
|
|
|
|
public static Boolean Create(InstallationAccess installationAccess)
|
|
{
|
|
return Insert(installationAccess);
|
|
}
|
|
|
|
public static Boolean Create(FolderAccess folderAccess)
|
|
{
|
|
return Insert(folderAccess);
|
|
}
|
|
|
|
public static Boolean Create(OrderNumber2Installation o2i)
|
|
{
|
|
return Insert(o2i);
|
|
}
|
|
|
|
public static void HandleError(Error newError,int installationId)
|
|
{
|
|
//Find the total number of errors for this installation
|
|
var totalErrors = Errors.Count(error => error.InstallationId == installationId);
|
|
|
|
//If there are 100 errors, remove the one with the oldest timestamp
|
|
if (totalErrors == 100)
|
|
{
|
|
var oldestError =
|
|
Errors.Where(error => error.InstallationId == installationId)
|
|
.OrderBy(error => error.CreatedAt)
|
|
.FirstOrDefault();
|
|
|
|
//Remove the old error
|
|
Delete(oldestError);
|
|
|
|
//Add the new error
|
|
Create(newError);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("---------------Added the new Error to the database-----------------");
|
|
Create(newError);
|
|
}
|
|
}
|
|
} |