#182 basic model and service for generic options

This commit is contained in:
Hamid Reza Mohammadi 2021-07-20 20:03:32 +04:30
parent 6b2efba59c
commit 7015087393
5 changed files with 210 additions and 11 deletions

View File

@ -62,9 +62,12 @@ namespace RSecurityBackend.DbContext
builder.Entity<RVerifyQueueItem>()
.HasIndex(u => u.Secret)
.IsUnique();
builder.Entity<RGenericOption>()
.HasIndex(o => o.Name)
.IsUnique();
}
#region Auth
/// <summary>
/// Permissions
/// </summary>
@ -91,29 +94,24 @@ namespace RSecurityBackend.DbContext
/// </summary>
public DbSet<RCaptchaImage> CaptchaImages { get; set; }
#endregion
#region Audit
/// <summary>
/// Audit Logs Events
/// </summary>
public DbSet<REvent> AuditLogs { get; set; }
#endregion
#region
/// <summary>
/// long running jobs
/// </summary>
public DbSet<RLongRunningJobStatus> LongRunningJobs { get; set; }
#endregion
#region Notifications
/// <summary>
/// Notifications
/// </summary>
public DbSet<RUserNotification> Notifications { get; set; }
#endregion
/// <summary>
/// Options
/// </summary>
public DbSet<RGenericOption> Options { get; set; }
}
}

View File

@ -0,0 +1,23 @@
namespace RSecurityBackend.Models.Generic.Db
{
/// <summary>
/// generic option
/// </summary>
public class RGenericOption
{
/// <summary>
/// id
/// </summary>
public int Id { get; set; }
/// <summary>
/// name
/// </summary>
public string Name { get; set; }
/// <summary>
///value
/// </summary>
public string Value { get; set; }
}
}

View File

@ -643,6 +643,11 @@
Notifications
</summary>
</member>
<member name="P:RSecurityBackend.DbContext.RSecurityDbContext`3.Options">
<summary>
Options
</summary>
</member>
<member name="T:RSecurityBackend.Models.Audit.Db.REvent">
<summary>
Audit Event
@ -1744,6 +1749,26 @@
Amount
</summary>
</member>
<member name="T:RSecurityBackend.Models.Generic.Db.RGenericOption">
<summary>
generic option
</summary>
</member>
<member name="P:RSecurityBackend.Models.Generic.Db.RGenericOption.Id">
<summary>
id
</summary>
</member>
<member name="P:RSecurityBackend.Models.Generic.Db.RGenericOption.Name">
<summary>
name
</summary>
</member>
<member name="P:RSecurityBackend.Models.Generic.Db.RGenericOption.Value">
<summary>
value
</summary>
</member>
<member name="T:RSecurityBackend.Models.Generic.Db.RLongRunningJobStatus">
<summary>
Long Running Job Status
@ -3270,6 +3295,37 @@
</summary>
<param name="connectionString"></param>
</member>
<member name="T:RSecurityBackend.Services.Implementation.RGenericOptionsServiceEF">
<summary>
generic options service
</summary>
</member>
<member name="M:RSecurityBackend.Services.Implementation.RGenericOptionsServiceEF.SetAsync(System.String,System.String)">
<summary>
modify an option or add a new one if an option with the requested name does not exist
</summary>
<param name="optionName"></param>
<param name="optionValue"></param>
<returns></returns>
</member>
<member name="M:RSecurityBackend.Services.Implementation.RGenericOptionsServiceEF.GetValueAsync(System.String)">
<summary>
get option value
</summary>
<param name="optionName"></param>
<returns></returns>
</member>
<member name="F:RSecurityBackend.Services.Implementation.RGenericOptionsServiceEF._context">
<summary>
Database Contetxt
</summary>
</member>
<member name="M:RSecurityBackend.Services.Implementation.RGenericOptionsServiceEF.#ctor(RSecurityBackend.DbContext.RSecurityDbContext{RSecurityBackend.Models.Auth.Db.RAppUser,RSecurityBackend.Models.Auth.Db.RAppRole,System.Guid})">
<summary>
constructor
</summary>
<param name="context"></param>
</member>
<member name="T:RSecurityBackend.Services.Implementation.RNotificationService">
<summary>
Internal messaging system implementation
@ -3472,6 +3528,26 @@
</summary>
<param name="appUserService"></param>
</member>
<member name="T:RSecurityBackend.Services.IRGenericOptionsService">
<summary>
generic options service
</summary>
</member>
<member name="M:RSecurityBackend.Services.IRGenericOptionsService.SetAsync(System.String,System.String)">
<summary>
modify an option or add a new one if an option with the requested name does not exist
</summary>
<param name="optionName"></param>
<param name="optionValue"></param>
<returns></returns>
</member>
<member name="M:RSecurityBackend.Services.IRGenericOptionsService.GetValueAsync(System.String)">
<summary>
get option value
</summary>
<param name="optionName"></param>
<returns></returns>
</member>
<member name="T:RSecurityBackend.Services.IRNotificationService">
<summary>
Internal messaging system interface

View File

@ -0,0 +1,27 @@
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Models.Generic.Db;
using System.Threading.Tasks;
namespace RSecurityBackend.Services
{
/// <summary>
/// generic options service
/// </summary>
public interface IRGenericOptionsService
{
/// <summary>
/// modify an option or add a new one if an option with the requested name does not exist
/// </summary>
/// <param name="optionName"></param>
/// <param name="optionValue"></param>
/// <returns></returns>
Task<RServiceResult<RGenericOption>> SetAsync(string optionName, string optionValue);
/// <summary>
/// get option value
/// </summary>
/// <param name="optionName"></param>
/// <returns></returns>
Task<RServiceResult<string>> GetValueAsync(string optionName);
}
}

View File

@ -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
{
/// <summary>
/// generic options service
/// </summary>
public class RGenericOptionsServiceEF : IRGenericOptionsService
{
/// <summary>
/// modify an option or add a new one if an option with the requested name does not exist
/// </summary>
/// <param name="optionName"></param>
/// <param name="optionValue"></param>
/// <returns></returns>
public async Task<RServiceResult<RGenericOption>> 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<RGenericOption>(existing);
}
var option = new RGenericOption()
{
Name = optionName,
Value = optionValue
};
_context.Options.Add(option);
await _context.SaveChangesAsync();
return new RServiceResult<RGenericOption>(option);
}
/// <summary>
/// get option value
/// </summary>
/// <param name="optionName"></param>
/// <returns></returns>
public async Task<RServiceResult<string>> GetValueAsync(string optionName)
{
var option = await _context.Options.AsNoTracking().Where(o => o.Name == optionName).SingleOrDefaultAsync();
if (option == null)
return new RServiceResult<string>(null, $"option '{optionName}' not found!");
return new RServiceResult<string>(option.Value);
}
/// <summary>
/// Database Contetxt
/// </summary>
protected readonly RSecurityDbContext<RAppUser, RAppRole, Guid> _context;
/// <summary>
/// constructor
/// </summary>
/// <param name="context"></param>
public RGenericOptionsServiceEF(RSecurityDbContext<RAppUser, RAppRole, Guid> context)
{
_context = context;
}
}
}