diff --git a/RSecurityBackend/DbContext/RSecurityDbContext.cs b/RSecurityBackend/DbContext/RSecurityDbContext.cs index 0a529f83..3a06fd2b 100644 --- a/RSecurityBackend/DbContext/RSecurityDbContext.cs +++ b/RSecurityBackend/DbContext/RSecurityDbContext.cs @@ -62,9 +62,12 @@ namespace RSecurityBackend.DbContext builder.Entity() .HasIndex(u => u.Secret) .IsUnique(); + + builder.Entity() + .HasIndex(o => o.Name) + .IsUnique(); } - #region Auth /// /// Permissions /// @@ -91,29 +94,24 @@ namespace RSecurityBackend.DbContext /// public DbSet CaptchaImages { get; set; } - - #endregion - - #region Audit - /// /// Audit Logs Events /// public DbSet AuditLogs { get; set; } - #endregion - #region /// /// long running jobs /// public DbSet LongRunningJobs { get; set; } - #endregion - #region Notifications /// /// Notifications /// public DbSet Notifications { get; set; } - #endregion + + /// + /// Options + /// + public DbSet Options { get; set; } } } diff --git a/RSecurityBackend/Models/Generic/Db/RGenericOption.cs b/RSecurityBackend/Models/Generic/Db/RGenericOption.cs new file mode 100644 index 00000000..482d38c8 --- /dev/null +++ b/RSecurityBackend/Models/Generic/Db/RGenericOption.cs @@ -0,0 +1,23 @@ +namespace RSecurityBackend.Models.Generic.Db +{ + /// + /// generic option + /// + public class RGenericOption + { + /// + /// id + /// + public int Id { get; set; } + + /// + /// name + /// + public string Name { get; set; } + + /// + ///value + /// + public string Value { get; set; } + } +} diff --git a/RSecurityBackend/RSecurityBackend.xml b/RSecurityBackend/RSecurityBackend.xml index 668d77e5..d3375544 100644 --- a/RSecurityBackend/RSecurityBackend.xml +++ b/RSecurityBackend/RSecurityBackend.xml @@ -643,6 +643,11 @@ Notifications + + + Options + + Audit Event @@ -1744,6 +1749,26 @@ Amount + + + generic option + + + + + id + + + + + name + + + + + value + + Long Running Job Status @@ -3270,6 +3295,37 @@ + + + generic options service + + + + + modify an option or add a new one if an option with the requested name does not exist + + + + + + + + get option value + + + + + + + Database Contetxt + + + + + constructor + + + Internal messaging system implementation @@ -3472,6 +3528,26 @@ + + + generic options service + + + + + modify an option or add a new one if an option with the requested name does not exist + + + + + + + + get option value + + + + Internal messaging system interface diff --git a/RSecurityBackend/Services/IRGenericOptionsService.cs b/RSecurityBackend/Services/IRGenericOptionsService.cs new file mode 100644 index 00000000..1b26fbe8 --- /dev/null +++ b/RSecurityBackend/Services/IRGenericOptionsService.cs @@ -0,0 +1,27 @@ +using RSecurityBackend.Models.Generic; +using RSecurityBackend.Models.Generic.Db; +using System.Threading.Tasks; + +namespace RSecurityBackend.Services +{ + /// + /// generic options service + /// + public interface IRGenericOptionsService + { + /// + /// modify an option or add a new one if an option with the requested name does not exist + /// + /// + /// + /// + Task> SetAsync(string optionName, string optionValue); + + /// + /// get option value + /// + /// + /// + Task> GetValueAsync(string optionName); + } +} diff --git a/RSecurityBackend/Services/Implementation/RGenericOptionsServiceEF.cs b/RSecurityBackend/Services/Implementation/RGenericOptionsServiceEF.cs new file mode 100644 index 00000000..0fc44381 --- /dev/null +++ b/RSecurityBackend/Services/Implementation/RGenericOptionsServiceEF.cs @@ -0,0 +1,75 @@ +using Microsoft.EntityFrameworkCore; +using RSecurityBackend.DbContext; +using RSecurityBackend.Models.Auth.Db; +using RSecurityBackend.Models.Generic; +using RSecurityBackend.Models.Generic.Db; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace RSecurityBackend.Services.Implementation +{ + /// + /// generic options service + /// + public class RGenericOptionsServiceEF : IRGenericOptionsService + { + /// + /// modify an option or add a new one if an option with the requested name does not exist + /// + /// + /// + /// + public async Task> SetAsync(string optionName, string optionValue) + { + var existing = await _context.Options.Where(o => o.Name == optionName).SingleOrDefaultAsync(); + if (existing != null) + { + existing.Value = optionValue; + _context.Options.Update(existing); + await _context.SaveChangesAsync(); + + return new RServiceResult(existing); + } + + var option = new RGenericOption() + { + Name = optionName, + Value = optionValue + }; + + _context.Options.Add(option); + await _context.SaveChangesAsync(); + + return new RServiceResult(option); + } + + /// + /// get option value + /// + /// + /// + public async Task> GetValueAsync(string optionName) + { + var option = await _context.Options.AsNoTracking().Where(o => o.Name == optionName).SingleOrDefaultAsync(); + if (option == null) + return new RServiceResult(null, $"option '{optionName}' not found!"); + return new RServiceResult(option.Value); + } + + /// + /// Database Contetxt + /// + protected readonly RSecurityDbContext _context; + + + /// + /// constructor + /// + /// + public RGenericOptionsServiceEF(RSecurityDbContext context) + { + _context = context; + } + } +}