From 80e7f5e2fb7180b7214b9dec5f788f4849a8c01f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 6 Mar 2017 01:30:02 +0100 Subject: [PATCH] factorisation --- Yavsc/Controllers/BrusherProfileController.cs | 81 +------------- Yavsc/Controllers/DoController.cs | 12 +- .../Controllers/Generic/SettingsController.cs | 104 ++++++++++++++++++ 3 files changed, 114 insertions(+), 83 deletions(-) create mode 100644 Yavsc/Controllers/Generic/SettingsController.cs diff --git a/Yavsc/Controllers/BrusherProfileController.cs b/Yavsc/Controllers/BrusherProfileController.cs index a1a79074..a181bf1b 100644 --- a/Yavsc/Controllers/BrusherProfileController.cs +++ b/Yavsc/Controllers/BrusherProfileController.cs @@ -5,69 +5,18 @@ using Microsoft.Data.Entity; using Yavsc.Models; using Yavsc.Models.Haircut; using Microsoft.AspNet.Authorization; +using Yavsc.Controllers.Generic; namespace Yavsc.Controllers { [Authorize(Roles="Performer")] - public class BrusherProfileController : Controller + public class BrusherProfileController : SettingsController { - private ApplicationDbContext _context; - public BrusherProfileController(ApplicationDbContext context) + public BrusherProfileController(ApplicationDbContext context) : base(context) { - _context = context; } - - // GET: BrusherProfile - public async Task Index() - { - var existing = await _context.BrusherProfile.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId()); - return View(existing); - } - - // GET: BrusherProfile/Details/5 - public async Task Details(string id) - { - if (id == null) - { - id = User.GetUserId(); - } - - BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id); - if (brusherProfile == null) - { - return HttpNotFound(); - } - - return View(brusherProfile); - } - - // GET: BrusherProfile/Create - public IActionResult Create() - { - return View(); - } - - // GET: BrusherProfile/Edit/5 - public async Task Edit(string id) - { - if (id == null) - { - id = User.GetUserId(); - } - - BrusherProfile brusherProfile = await _context.BrusherProfile.SingleOrDefaultAsync(m => m.UserId == id); - if (brusherProfile == null) - { - brusherProfile = new BrusherProfile { }; - } - return View(brusherProfile); - } - - // POST: BrusherProfile/Edit/5 - [HttpPost] - [ValidateAntiForgeryToken] - public async Task Edit(BrusherProfile brusherProfile) + override public async Task Edit(BrusherProfile brusherProfile) { if (string.IsNullOrEmpty(brusherProfile.UserId)) { @@ -89,28 +38,8 @@ namespace Yavsc.Controllers return View(brusherProfile); } - // GET: BrusherProfile/Delete/5 - [ActionName("Delete")] - public async Task Delete(string id) - { - if (id == null) - { - return HttpNotFound(); - } - - BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id); - if (brusherProfile == null) - { - return HttpNotFound(); - } - - return View(brusherProfile); - } - // POST: BrusherProfile/Delete/5 - [HttpPost, ActionName("Delete")] - [ValidateAntiForgeryToken] - public async Task DeleteConfirmed(string id) + override public async Task DeleteConfirmed(string id) { BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id); _context.BrusherProfile.Remove(brusherProfile); diff --git a/Yavsc/Controllers/DoController.cs b/Yavsc/Controllers/DoController.cs index 8b9488db..4a25cb8c 100644 --- a/Yavsc/Controllers/DoController.cs +++ b/Yavsc/Controllers/DoController.cs @@ -12,6 +12,7 @@ namespace Yavsc.Controllers using Models.Workflow; using Yavsc.Exceptions; using Yavsc.ViewModels.Workflow; + using YavscLib; [Authorize] public class DoController : Controller @@ -56,16 +57,13 @@ namespace Yavsc.Controllers bool hasConfigurableSettings = (userActivity.Does.SettingsClassName != null); if (hasConfigurableSettings) { - ViewBag.ProfileType = Startup.ProfileTypes.Single(t=>t.FullName==userActivity.Does.SettingsClassName); - - - var dbset = _context.GetDbSet(userActivity.Does.SettingsClassName); + var dbset = (IQueryable) _context.GetDbSet(userActivity.Does.SettingsClassName); if (dbset == null) throw new InvalidWorkflowModelException($"pas de db set pour {userActivity.Does.SettingsClassName}, vous avez peut-être besoin de décorer votre propriété avec l'attribut [ActivitySettings]"); return View(new UserActivityViewModel { - Declaration = userActivity, - HasSettings = dbset?.Any(ua=>ua.UserId==id) ?? false, - NeedsSettings = hasConfigurableSettings + Declaration = userActivity, + HasSettings = dbset.Any(ua=>ua.UserId==id), + NeedsSettings = hasConfigurableSettings } ); } return View(new UserActivityViewModel { diff --git a/Yavsc/Controllers/Generic/SettingsController.cs b/Yavsc/Controllers/Generic/SettingsController.cs new file mode 100644 index 00000000..42b4511c --- /dev/null +++ b/Yavsc/Controllers/Generic/SettingsController.cs @@ -0,0 +1,104 @@ +using System.Collections.Generic; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; +using Microsoft.Data.Entity; +using Yavsc.Models; +using YavscLib; +using System.Linq; + +namespace Yavsc.Controllers.Generic +{ + public abstract class SettingsController : Controller where TSettings : class, ISpecializationSettings, new() + { + protected ApplicationDbContext _context; + protected object dbSet; + + protected IQueryable QueryableDbSet { get { + return (IQueryable) dbSet; + } } + protected ISet RwDbSet { get { + return (ISet) dbSet; + } } + + public SettingsController(ApplicationDbContext context) + { + _context = context; + dbSet=_context.GetDbSet(); + } + + public async Task Index() + { + var existing = await this.QueryableDbSet.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId()); + return View(existing); + } + // GET: BrusherProfile/Details/5 + public async Task Details(string id) + { + if (id == null) + { + id = User.GetUserId(); + } + + var profile = await QueryableDbSet.SingleAsync(m => m.UserId == id); + if (profile == null) + { + return HttpNotFound(); + } + + return View(profile); + } + + + // GET: BrusherProfile/Create + public IActionResult Create() + { + return View(); + } + + // GET: BrusherProfile/Edit/5 + public async Task Edit(string id) + { + if (id == null) + { + id = User.GetUserId(); + } + + TSettings setting = await QueryableDbSet.SingleOrDefaultAsync(m => m.UserId == id); + if (setting == null) + { + setting = new TSettings { }; + } + return View(setting); + } + + + + // GET: BrusherProfile/Delete/5 + [ActionName("Delete")] + public async Task Delete(string id) + { + if (id == null) + { + return HttpNotFound(); + } + + var brusherProfile = await QueryableDbSet.SingleAsync(m => m.UserId == id); + if (brusherProfile == null) + { + return HttpNotFound(); + } + + return View(brusherProfile); + } + + // POST: BrusherProfile/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public abstract Task DeleteConfirmed(string id); + // POST: BrusherProfile/Edit/5 + [HttpPost] + [ValidateAntiForgeryToken] + public abstract Task Edit(TSettings profile); + } +} \ No newline at end of file