From 76626f3ac4946a67d803f7882abc0e9c7735b645 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 24 Feb 2025 23:29:48 +0000 Subject: [PATCH] User settings --- .../Business/FrontOfficeApiController.cs | 7 +-- .../Business/PerformersApiController.cs | 4 +- .../Interfaces/IBillingService.cs | 9 +--- .../Workflow/ISpecializationSettings.cs | 2 +- src/Yavsc.Server/Helpers/WorkflowHelpers.cs | 15 +++--- .../Models/HairCut/BrusherProfile.cs | 2 +- .../Models/Musical/Profiles/DjSettings.cs | 2 +- .../Musical/Profiles/GeneralSettings.cs | 2 +- .../Musical/Profiles/Instrumentation.cs | 2 +- .../Workflow/Profiles/FormationSettings.cs | 2 +- src/Yavsc.Server/Services/BillingService.cs | 48 +++++++++++-------- .../Workflow/UserActivityViewModel.cs | 4 +- .../Controllers/Contracting/DoController.cs | 2 +- .../FormationSettingsController.cs | 1 + .../Contracting/FrontOfficeController.cs | 12 ++--- .../Controllers/Generic/SettingsController.cs | 29 ++++++----- 16 files changed, 76 insertions(+), 67 deletions(-) diff --git a/src/Api/Controllers/Business/FrontOfficeApiController.cs b/src/Api/Controllers/Business/FrontOfficeApiController.cs index 1f770dd9..d063d5bc 100644 --- a/src/Api/Controllers/Business/FrontOfficeApiController.cs +++ b/src/Api/Controllers/Business/FrontOfficeApiController.cs @@ -22,9 +22,9 @@ namespace Yavsc.ApiControllers } [HttpGet("profiles/{actCode}")] - IEnumerable Profiles(string actCode) + async Task > Profiles(string actCode) { - return dbContext.ListPerformers(billing, actCode); + return await dbContext.ListPerformersAsync(billing, actCode); } [HttpPost("query/reject")] @@ -39,7 +39,8 @@ namespace Yavsc.ApiControllers dbContext.SaveChanges(); return Ok(); } - [HttpPost("query/reject")] + + [HttpPost("query/reject")] public IActionResult AcceptQuery(string billingCode, long queryId) { if (billingCode == null) return BadRequest("billingCode"); diff --git a/src/Api/Controllers/Business/PerformersApiController.cs b/src/Api/Controllers/Business/PerformersApiController.cs index 8f7603ae..15372b95 100644 --- a/src/Api/Controllers/Business/PerformersApiController.cs +++ b/src/Api/Controllers/Business/PerformersApiController.cs @@ -57,9 +57,9 @@ namespace Yavsc.Controllers } [HttpGet("doing/{id}"),AllowAnonymous] - public IActionResult ListPerformers(string id) + public async Task ListPerformers(string id) { - return Ok(dbContext.ListPerformers(billing, id)); + return Ok(await dbContext.ListPerformersAsync(billing, id)); } } } diff --git a/src/Yavsc.Abstract/Interfaces/IBillingService.cs b/src/Yavsc.Abstract/Interfaces/IBillingService.cs index 99317145..5c99212a 100644 --- a/src/Yavsc.Abstract/Interfaces/IBillingService.cs +++ b/src/Yavsc.Abstract/Interfaces/IBillingService.cs @@ -28,20 +28,13 @@ namespace Yavsc.Services /// La facture Task GetBillAsync(string billingCode, long queryId); - /// - /// All performer setting in this activity - /// - /// - /// - Task> GetPerformersSettingsAsync(string activityCode); - /// /// Perfomer settings for the specified performer in the activity /// /// activityCode /// performer uid /// - Task GetPerformerSettingsAsync(string activityCode, string userId); + Task GetPerformersSettingsAsync(string activityCode, string userId); } } diff --git a/src/Yavsc.Abstract/Workflow/ISpecializationSettings.cs b/src/Yavsc.Abstract/Workflow/ISpecializationSettings.cs index 919e469b..5198b922 100644 --- a/src/Yavsc.Abstract/Workflow/ISpecializationSettings.cs +++ b/src/Yavsc.Abstract/Workflow/ISpecializationSettings.cs @@ -1,6 +1,6 @@ namespace Yavsc { - public interface ISpecializationSettings + public interface IUserSettings { string UserId { get ; set ; } } diff --git a/src/Yavsc.Server/Helpers/WorkflowHelpers.cs b/src/Yavsc.Server/Helpers/WorkflowHelpers.cs index d4653a5d..4f382f7a 100644 --- a/src/Yavsc.Server/Helpers/WorkflowHelpers.cs +++ b/src/Yavsc.Server/Helpers/WorkflowHelpers.cs @@ -11,22 +11,23 @@ namespace Yavsc.Helpers public static class WorkflowHelpers { - public static List ListPerformers(this ApplicationDbContext context, + public static async Task> ListPerformersAsync(this ApplicationDbContext context, IBillingService billing, string actCode) { - var settings = billing.GetPerformersSettingsAsync(actCode).Result?.ToArray(); var actors = context.Performers .Include(p=>p.Activity) .Include(p=>p.Performer) - .Include(p=>p.Performer.Posts) - .Include(p=>p.Performer.DeviceDeclaration) .Where(p => p.Active && p.Activity.Any(u=>u.DoesCode==actCode)).OrderBy( x => x.Rate ) .ToArray(); - List result = new List (); - result.AddRange( - actors.Select(a=> new PerformerProfileViewModel(a, actCode, settings?.FirstOrDefault(s => s.UserId == a.PerformerId)))); + + List result = new (); + foreach (var a in actors) + { + var settings = await billing.GetPerformersSettingsAsync(actCode, a.PerformerId); + result.Add(new PerformerProfileViewModel(a, actCode,settings)); + } return result; } diff --git a/src/Yavsc.Server/Models/HairCut/BrusherProfile.cs b/src/Yavsc.Server/Models/HairCut/BrusherProfile.cs index 6a48c51f..9e141701 100644 --- a/src/Yavsc.Server/Models/HairCut/BrusherProfile.cs +++ b/src/Yavsc.Server/Models/HairCut/BrusherProfile.cs @@ -31,7 +31,7 @@ namespace Yavsc.Models.Haircut using Relationship; using Calendar; - public class BrusherProfile : ISpecializationSettings + public class BrusherProfile : IUserSettings { public BrusherProfile() { diff --git a/src/Yavsc.Server/Models/Musical/Profiles/DjSettings.cs b/src/Yavsc.Server/Models/Musical/Profiles/DjSettings.cs index 40a3fcef..d74a0284 100644 --- a/src/Yavsc.Server/Models/Musical/Profiles/DjSettings.cs +++ b/src/Yavsc.Server/Models/Musical/Profiles/DjSettings.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace Yavsc.Models.Musical.Profiles { - public class DjSettings : ISpecializationSettings + public class DjSettings : IUserSettings { public string SoundCloudId { get; set; } diff --git a/src/Yavsc.Server/Models/Musical/Profiles/GeneralSettings.cs b/src/Yavsc.Server/Models/Musical/Profiles/GeneralSettings.cs index a038f53c..85400812 100644 --- a/src/Yavsc.Server/Models/Musical/Profiles/GeneralSettings.cs +++ b/src/Yavsc.Server/Models/Musical/Profiles/GeneralSettings.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace Yavsc.Models.Musical.Profiles { - public class GeneralSettings : ISpecializationSettings + public class GeneralSettings : IUserSettings { public virtual List SoundColor { get; set; } [Key] diff --git a/src/Yavsc.Server/Models/Musical/Profiles/Instrumentation.cs b/src/Yavsc.Server/Models/Musical/Profiles/Instrumentation.cs index ae91898d..a2e2878c 100644 --- a/src/Yavsc.Server/Models/Musical/Profiles/Instrumentation.cs +++ b/src/Yavsc.Server/Models/Musical/Profiles/Instrumentation.cs @@ -3,7 +3,7 @@ using Yavsc.Models.Workflow; namespace Yavsc.Models.Musical.Profiles { - public class Instrumentation : ISpecializationSettings + public class Instrumentation : IUserSettings { public string UserId { diff --git a/src/Yavsc.Server/Models/Workflow/Profiles/FormationSettings.cs b/src/Yavsc.Server/Models/Workflow/Profiles/FormationSettings.cs index 74744b80..2f7095da 100644 --- a/src/Yavsc.Server/Models/Workflow/Profiles/FormationSettings.cs +++ b/src/Yavsc.Server/Models/Workflow/Profiles/FormationSettings.cs @@ -6,7 +6,7 @@ namespace Yavsc.Models.Workflow.Profiles { using Models.Workflow; using Yavsc; - public class FormationSettings : ISpecializationSettings + public class FormationSettings : IUserSettings { public virtual List CoWorking { get; set; } diff --git a/src/Yavsc.Server/Services/BillingService.cs b/src/Yavsc.Server/Services/BillingService.cs index 1aaefe8b..214e336f 100644 --- a/src/Yavsc.Server/Services/BillingService.cs +++ b/src/Yavsc.Server/Services/BillingService.cs @@ -8,15 +8,16 @@ namespace Yavsc.Services public class BillingService : IBillingService { public ApplicationDbContext DbContext { get; private set; } - public static Dictionary> Billing = - new Dictionary> (); + public static Dictionary> Billing = + new Dictionary>(); public static List UserSettings = new List(); - - public static Dictionary GlobalBillingMap = - new Dictionary(); - public Dictionary BillingMap { - get { return GlobalBillingMap; } + public static Dictionary GlobalBillingMap = + new Dictionary(); + + public Dictionary BillingMap + { + get { return GlobalBillingMap; } } public BillingService(ApplicationDbContext dbContext) @@ -26,23 +27,30 @@ namespace Yavsc.Services public Task GetBillAsync(string billingCode, long queryId) { - return Task.FromResult(GetBillable(DbContext,billingCode,queryId)); + return Task.FromResult(GetBillable(DbContext, billingCode, queryId)); } - public static IDecidableQuery GetBillable(ApplicationDbContext context, string billingCode, long queryId ) => Billing[billingCode](context, queryId); - public async Task GetPerformerSettingsAsync(string activityCode, string userId) + public static IDecidableQuery GetBillable(ApplicationDbContext context, string billingCode, long queryId) => Billing[billingCode](context, queryId); + public async Task GetPerformersSettingsAsync(string activityCode, string userId) { - return (await GetPerformersSettingsAsync(activityCode)).SingleOrDefault(s=> s.UserId == userId); + var activity = await DbContext.Activities.SingleAsync(a => a.Code == activityCode); + + if (activity.SettingsClassName == null) return null; + + var dbSetGetter = + UserSettings.SingleOrDefault(s => s.Name == activity.SettingsClassName); + + if (dbSetGetter==null) return null; + var dbSet = dbSetGetter.GetValue(DbContext); + + if (dbSet == null) return null; + + if (dbSet is DbSet userSettings) + { + return userSettings.FirstOrDefault(s => s.UserId == userId); + } + return null; } - public async Task> GetPerformersSettingsAsync(string activityCode) - { - var activity = await DbContext.Activities.SingleAsync(a=>a.Code == activityCode); - - if (activity.SettingsClassName==null) return null; - - return UserSettings.Where(s => s.PropertyType.GenericTypeArguments[0].FullName == activity.SettingsClassName) - .Cast(); - } } } diff --git a/src/Yavsc.Server/ViewModels/Workflow/UserActivityViewModel.cs b/src/Yavsc.Server/ViewModels/Workflow/UserActivityViewModel.cs index 37b3012f..36d96e7d 100644 --- a/src/Yavsc.Server/ViewModels/Workflow/UserActivityViewModel.cs +++ b/src/Yavsc.Server/ViewModels/Workflow/UserActivityViewModel.cs @@ -6,6 +6,6 @@ namespace Yavsc.ViewModels.Workflow { public UserActivity Declaration { get; set; } public bool NeedsSettings { get; set; } - public ISpecializationSettings Settings { get; set; } + public IUserSettings Settings { get; set; } } -} \ No newline at end of file +} diff --git a/src/Yavsc/Controllers/Contracting/DoController.cs b/src/Yavsc/Controllers/Contracting/DoController.cs index 9db529da..359cb89a 100644 --- a/src/Yavsc/Controllers/Contracting/DoController.cs +++ b/src/Yavsc/Controllers/Contracting/DoController.cs @@ -59,7 +59,7 @@ namespace Yavsc.Controllers return NotFound(); } bool hasConfigurableSettings = (userActivity.Does.SettingsClassName != null); - var settings = await billing.GetPerformerSettingsAsync(activityCode,id); + var settings = await billing.GetPerformersSettingsAsync(activityCode, id); ViewBag.ProfileType = Config.ProfileTypes.Single(t=>t.FullName==userActivity.Does.SettingsClassName); var gift = new UserActivityViewModel { diff --git a/src/Yavsc/Controllers/Contracting/FormationSettingsController.cs b/src/Yavsc/Controllers/Contracting/FormationSettingsController.cs index bece3484..686d4c63 100644 --- a/src/Yavsc/Controllers/Contracting/FormationSettingsController.cs +++ b/src/Yavsc/Controllers/Contracting/FormationSettingsController.cs @@ -9,6 +9,7 @@ namespace Yavsc.Controllers public FormationSettingsController(ApplicationDbContext context) : base(context) { + } } diff --git a/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs b/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs index 666fa501..cf906292 100644 --- a/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs +++ b/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs @@ -53,26 +53,26 @@ namespace Yavsc.Controllers } [AllowAnonymous] - public ActionResult Profiles(string id) + public async Task Profiles(string id) { if (id == null) { throw new NotImplementedException("No Activity code"); } - ViewBag.Activity = _context.Activities.FirstOrDefault(a => a.Code == id); - var result = _context.ListPerformers(_billing, id); + ViewBag.Activity = await _context.Activities.FirstOrDefaultAsync(a => a.Code == id); + var result = await _context.ListPerformersAsync(_billing, id); return View(result); } [AllowAnonymous] - public ActionResult HairCut(string id) + public async Task HairCut(string id) { if (id == null) { throw new NotImplementedException("No Activity code"); } - ViewBag.Activity = _context.Activities.FirstOrDefault(a => a.Code == id); - var result = _context.ListPerformers(_billing, id); + ViewBag.Activity = await _context.Activities.FirstOrDefaultAsync(a => a.Code == id); + var result = await _context.ListPerformersAsync(_billing, id); return View(result); } diff --git a/src/Yavsc/Controllers/Generic/SettingsController.cs b/src/Yavsc/Controllers/Generic/SettingsController.cs index 9f7450f3..96002836 100644 --- a/src/Yavsc/Controllers/Generic/SettingsController.cs +++ b/src/Yavsc/Controllers/Generic/SettingsController.cs @@ -10,7 +10,7 @@ namespace Yavsc.Controllers.Generic using Yavsc.Services; [Authorize] - public abstract class SettingsController : Controller where TSettings : class, ISpecializationSettings, new() + public abstract class SettingsController : Controller where TSettings : class, IUserSettings, new() { protected ApplicationDbContext _context; DbSet dbSet=null; @@ -21,11 +21,17 @@ namespace Yavsc.Controllers.Generic if (dbSet == null) { dbSet = (DbSet) BillingService.UserSettings.Single(s=>s.Name == typeof(TSettings).Name).GetValue(_context); } - - return dbSet; } } + virtual protected async Task GetSettingsAsync( + string userId + ) + { + return await Settings.SingleOrDefaultAsync(p=>p.UserId == userId); + } + + public SettingsController(ApplicationDbContext context) { _context = context; @@ -33,8 +39,7 @@ namespace Yavsc.Controllers.Generic public async Task Index() { - var existing = await this.Settings.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId()); - return View(existing); + return View(await GetSettingsAsync(User.GetUserId())); } // GET: BrusherProfile/Details/5 public async Task Details(string id) @@ -44,7 +49,7 @@ namespace Yavsc.Controllers.Generic id = User.GetUserId(); } - var profile = await Settings.SingleAsync(m => m.UserId == id); + var profile = await GetSettingsAsync(id); if (profile == null) { return NotFound(); @@ -68,7 +73,7 @@ namespace Yavsc.Controllers.Generic id = User.GetUserId(); } - TSettings setting = await Settings.SingleOrDefaultAsync(m => m.UserId == id); + TSettings setting = await GetSettingsAsync(id); if (setting == null) { setting = new TSettings { }; @@ -87,13 +92,13 @@ namespace Yavsc.Controllers.Generic return NotFound(); } - var brusherProfile = await Settings.SingleAsync(m => m.UserId == id); - if (brusherProfile == null) + var profile = await GetSettingsAsync(id); + if (profile == null) { return NotFound(); } - return View(brusherProfile); + return View(profile); } // POST: FormationSettings/Create @@ -135,8 +140,8 @@ namespace Yavsc.Controllers.Generic [ValidateAntiForgeryToken] public async Task DeleteConfirmed(string id) { - TSettings formationSettings = await Settings.SingleAsync(m => m.UserId == id); - Settings.Remove(formationSettings); + TSettings userSettings = await GetSettingsAsync(id); + Settings.Remove(userSettings); await _context.SaveChangesAsync(); return RedirectToAction("Index"); }