2020-05-20 15:14:44 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2020
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-11-22 08:30:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using ASC.Common;
|
|
|
|
using ASC.Core.Common.EF;
|
2020-05-20 15:14:44 +00:00
|
|
|
using ASC.Core.Tenants;
|
|
|
|
using ASC.Data.Backup.EF.Context;
|
2021-11-22 08:30:38 +00:00
|
|
|
using ASC.Data.Backup.EF.Model;
|
2022-02-04 13:04:52 +00:00
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
2020-05-20 15:14:44 +00:00
|
|
|
namespace ASC.Data.Backup.Storage
|
2021-11-22 08:30:38 +00:00
|
|
|
{
|
2020-10-19 15:53:15 +00:00
|
|
|
[Scope]
|
2020-06-01 10:33:39 +00:00
|
|
|
public class BackupRepository : IBackupRepository
|
2021-11-22 08:30:38 +00:00
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
private readonly Lazy<BackupsContext> _backupContext;
|
|
|
|
public BackupRepository(DbContextManager<BackupsContext> dbContactManager)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-01-21 13:45:50 +00:00
|
|
|
_backupContext = new Lazy<BackupsContext>(() => dbContactManager.Value);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveBackupRecord(BackupRecord backup)
|
|
|
|
{
|
2022-01-21 13:45:50 +00:00
|
|
|
_backupContext.Value.AddOrUpdate(r => r.Backups, backup);
|
2022-02-04 13:04:52 +00:00
|
|
|
_backupContext.Value.SaveChanges();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public BackupRecord GetBackupRecord(Guid id)
|
2021-11-22 08:30:38 +00:00
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Backups.Find(id);
|
2021-11-22 08:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public BackupRecord GetBackupRecord(string hash, int tenant)
|
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Backups.AsNoTracking().SingleOrDefault(b => b.Hash == hash && b.TenantId == tenant);
|
2021-11-22 08:30:38 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
public List<BackupRecord> GetExpiredBackupRecords()
|
2021-11-22 08:30:38 +00:00
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Backups.AsNoTracking().AsQueryable().Where(b => b.ExpiresOn != DateTime.MinValue && b.ExpiresOn <= DateTime.UtcNow).ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<BackupRecord> GetScheduledBackupRecords()
|
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Backups.AsQueryable().AsNoTracking().Where(b => b.IsScheduled == true).ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<BackupRecord> GetBackupRecordsByTenantId(int tenantId)
|
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Backups.AsQueryable().AsNoTracking().Where(b => b.TenantId == tenantId).ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteBackupRecord(Guid id)
|
|
|
|
{
|
2022-01-21 13:45:50 +00:00
|
|
|
var backup = _backupContext.Value.Backups.Find(id);
|
2022-02-04 13:04:52 +00:00
|
|
|
|
2020-05-20 15:14:44 +00:00
|
|
|
if (backup != null)
|
|
|
|
{
|
2022-01-21 13:45:50 +00:00
|
|
|
_backupContext.Value.Backups.Remove(backup);
|
|
|
|
_backupContext.Value.SaveChanges();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:48:27 +00:00
|
|
|
public void SaveBackupSchedule(BackupSchedule schedule)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-01-21 13:45:50 +00:00
|
|
|
_backupContext.Value.AddOrUpdate(r => r.Schedules, schedule);
|
|
|
|
_backupContext.Value.SaveChanges();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteBackupSchedule(int tenantId)
|
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
var shedule = _backupContext.Value.Schedules.AsQueryable().Where(s => s.TenantId == tenantId).ToList();
|
2022-01-21 13:45:50 +00:00
|
|
|
|
|
|
|
_backupContext.Value.Schedules.RemoveRange(shedule);
|
|
|
|
_backupContext.Value.SaveChanges();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 14:48:27 +00:00
|
|
|
public List<BackupSchedule> GetBackupSchedules()
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
var query = _backupContext.Value.Schedules.AsQueryable().Join(_backupContext.Value.Tenants,
|
2021-11-22 08:30:38 +00:00
|
|
|
s => s.TenantId,
|
2020-05-29 14:48:27 +00:00
|
|
|
t => t.Id,
|
2021-11-22 08:30:38 +00:00
|
|
|
(s, t) => new { schedule = s, tenant = t })
|
|
|
|
.Where(q => q.tenant.Status == TenantStatus.Active)
|
|
|
|
.Select(q => q.schedule);
|
2020-05-29 14:48:27 +00:00
|
|
|
|
|
|
|
return query.ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 14:48:27 +00:00
|
|
|
public BackupSchedule GetBackupSchedule(int tenantId)
|
2021-11-22 08:30:38 +00:00
|
|
|
{
|
2022-02-04 13:04:52 +00:00
|
|
|
return _backupContext.Value.Schedules.AsNoTracking().SingleOrDefault(s => s.TenantId == tenantId);
|
2021-11-22 08:30:38 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|