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, Guid? userId) { var existing = await _context.Options.Where(o => o.Name == optionName && o.RAppUserId == userId).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, RAppUserId = userId }; _context.Options.Add(option); await _context.SaveChangesAsync(); return new RServiceResult(option); } /// /// get option value /// /// /// /// public async Task> GetValueAsync(string optionName, Guid? userId) { var option = await _context.Options.AsNoTracking().Where(o => o.Name == optionName && o.RAppUserId == userId).SingleOrDefaultAsync(); if (option == null) return new RServiceResult(""); //empty value return new RServiceResult(option.Value); } /// /// Database Contetxt /// protected readonly RSecurityDbContext _context; /// /// constructor /// /// public RGenericOptionsServiceEF(RSecurityDbContext context) { _context = context; } } }