Oilshop / Controllers /BackupController.cs
almakhmri253-stack
Add automated daily/weekly database backup system with admin UI
9ba9d1b
Raw History Blame Contribute Delete
1.03 kB
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OilShopManagement.Services;
namespace OilShopManagement.Controllers;
[Authorize(Roles = "Admin")]
public class BackupController : Controller
{
private readonly DatabaseBackupService _backup;
public BackupController(DatabaseBackupService backup) => _backup = backup;
public IActionResult Index()
{
var files = DatabaseBackupService.ListBackups();
return View(files);
}
[HttpPost]
public async Task<IActionResult> Create()
{
await _backup.TriggerAsync();
TempData["Success"] = "تم إنشاء نسخة احتياطية بنجاح";
return RedirectToAction(nameof(Index));
}
public IActionResult Download(string fileName)
{
var path = DatabaseBackupService.GetBackupPath(fileName);
if (path == null) return NotFound();
var bytes = System.IO.File.ReadAllBytes(path);
return File(bytes, "application/json", fileName);
}
}