Ability to update settings. Take effect on next reboot.
This commit is contained in:
parent
e60f795410
commit
1050fa4e54
21 changed files with 953 additions and 146 deletions
|
@ -1,14 +1,14 @@
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Helpers.Converters;
|
||||
using API.Interfaces;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Controllers
|
||||
|
@ -18,30 +18,30 @@ namespace API.Controllers
|
|||
{
|
||||
private readonly DataContext _dataContext;
|
||||
private readonly ILogger<SettingsController> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ITaskScheduler _taskScheduler;
|
||||
|
||||
public SettingsController(DataContext dataContext, ILogger<SettingsController> logger, IMapper mapper, ITaskScheduler taskScheduler)
|
||||
public SettingsController(DataContext dataContext, ILogger<SettingsController> logger, IUnitOfWork unitOfWork,
|
||||
ITaskScheduler taskScheduler)
|
||||
{
|
||||
_dataContext = dataContext;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_unitOfWork = unitOfWork;
|
||||
_taskScheduler = taskScheduler;
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ActionResult<ServerSettingDto>> GetSettings()
|
||||
{
|
||||
var settings = await _dataContext.ServerSetting.Select(x => x).ToListAsync();
|
||||
return _mapper.Map<ServerSettingDto>(settings);
|
||||
return Ok(await _unitOfWork.SettingsRepository.GetSettingsDtoAsync());
|
||||
}
|
||||
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpPost("")]
|
||||
public async Task<ActionResult> UpdateSettings(ServerSettingDto updateSettingsDto)
|
||||
public async Task<ActionResult<ServerSettingDto>> UpdateSettings(ServerSettingDto updateSettingsDto)
|
||||
{
|
||||
_logger.LogInformation($"{User.GetUsername()} is updating Server Settings");
|
||||
|
||||
|
||||
if (updateSettingsDto.CacheDirectory.Equals(string.Empty))
|
||||
{
|
||||
return BadRequest("Cache Directory cannot be empty");
|
||||
|
@ -51,13 +51,39 @@ namespace API.Controllers
|
|||
{
|
||||
return BadRequest("Directory does not exist or is not accessible.");
|
||||
}
|
||||
// TODO: Figure out how to handle a change. This means that on clean, we need to clean up old cache
|
||||
// directory and new one, but what if someone is reading?
|
||||
// I can just clean both always, /cache/ is an owned folder, so users shouldn't use it.
|
||||
|
||||
|
||||
//_dataContext.ServerSetting.Update
|
||||
return BadRequest("Not Implemented");
|
||||
|
||||
// We do not allow CacheDirectory changes, so we will ignore.
|
||||
var currentSettings = await _unitOfWork.SettingsRepository.GetSettingsAsync();
|
||||
|
||||
foreach (var setting in currentSettings)
|
||||
{
|
||||
if (setting.Key == ServerSettingKey.TaskBackup && updateSettingsDto.TaskBackup != setting.Value)
|
||||
{
|
||||
setting.Value = updateSettingsDto.TaskBackup;
|
||||
_unitOfWork.SettingsRepository.Update(setting);
|
||||
}
|
||||
|
||||
if (setting.Key == ServerSettingKey.TaskScan && updateSettingsDto.TaskScan != setting.Value)
|
||||
{
|
||||
setting.Value = updateSettingsDto.TaskScan;
|
||||
_unitOfWork.SettingsRepository.Update(setting);
|
||||
}
|
||||
}
|
||||
|
||||
if (_unitOfWork.HasChanges() && await _unitOfWork.Complete())
|
||||
{
|
||||
_logger.LogInformation("Server Settings updated.");
|
||||
return Ok(updateSettingsDto);
|
||||
}
|
||||
|
||||
return BadRequest("There was a critical issue. Please try again.");
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpGet("task-frequencies")]
|
||||
public ActionResult<IEnumerable<string>> GetTaskFrequencies()
|
||||
{
|
||||
return Ok(CronConverter.Options);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue