Innovenergy_trunk/csharp/App/Backend/Database/Delete.cs

161 lines
4.3 KiB
C#

using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static void Backup()
{
_backupCounter++;
if (_backupCounter > 100)
{
_backupCounter = 0;
BackupDatabase();
}
}
public static Boolean Delete(Folder folder)
{
var deleteSuccess= RunTransaction(DeleteFolderAndAllItsDependencies);
if (deleteSuccess)
{
Backup();
}
return deleteSuccess;
Boolean DeleteFolderAndAllItsDependencies()
{
return folder
.DescendantFoldersAndSelf()
.All(DeleteDescendantFolderAndItsDependencies);
}
Boolean DeleteDescendantFolderAndItsDependencies(Folder f)
{
FolderAccess .Delete(r => r.FolderId == f.Id);
Installations.Delete(r => r.ParentId == f.Id);
var delete = Folders.Delete(r => r.Id == f.Id);
return delete>0;
}
}
public static Boolean Delete(Error errorToDelete)
{
var deleteSuccess = RunTransaction(DeleteError);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteError()
{
return Errors.Delete(error => error.Id == errorToDelete.Id) >0;
}
}
public static Boolean Delete(UserAction actionToDelete)
{
var deleteSuccess = RunTransaction(DeleteAction);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteAction()
{
return UserActions.Delete(action => action.Id == actionToDelete.Id) >0;
}
}
public static Boolean Delete(Warning warningToDelete)
{
var deleteSuccess = RunTransaction(DeleteWarning);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteWarning()
{
return Warnings.Delete(error => error.Id == warningToDelete.Id) >0;
}
}
public static Boolean Delete(CsvTimestamp csvTimestampToDelete)
{
var deleteSuccess = RunTransaction(DeleteCsvTimestampToDelete);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteCsvTimestampToDelete()
{
return CsvTimestamps.Delete(csvTimestamp => csvTimestamp.Id == csvTimestampToDelete.Id) >0;
}
}
public static Boolean Delete(Installation installation)
{
var deleteSuccess = RunTransaction(DeleteInstallationAndItsDependencies);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteInstallationAndItsDependencies()
{
if (installation.Product == 0)
{
InstallationAccess.Delete(i => i.InstallationId == installation.Id);
OrderNumber2Installation.Delete(i => i.InstallationId == installation.Id);
}
return Installations.Delete(i => i.Id == installation.Id) > 0;
}
}
public static Boolean Delete(User user)
{
var deleteSuccess = RunTransaction(DeleteUserAndHisDependencies);
if (deleteSuccess)
Backup();
return deleteSuccess;
Boolean DeleteUserAndHisDependencies()
{
FolderAccess .Delete(u => u.UserId == user.Id);
InstallationAccess.Delete(u => u.UserId == user.Id);
return Users.Delete(u => u.Id == user.Id) > 0;
}
}
#pragma warning disable CS0618
// private!!
private static Boolean Delete(Session session)
{
var delete = Sessions.Delete(s => s.Id == session.Id) > 0;
if (delete)
Backup();
return delete;
}
public static void Delete(OrderNumber2Installation relation)
{
OrderNumber2Installation.Delete(s => s.InstallationId == relation.InstallationId && s.OrderNumber == relation.OrderNumber);
}
}