From a863aed34cd970a93a4f308aea6a2da2a9584b98 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 6 Mar 2017 02:04:33 +0100 Subject: [PATCH] refactoring --- Yavsc/Controllers/BrusherProfileController.cs | 33 ------ .../FormationSettingsController.cs | 111 +----------------- .../Controllers/Generic/SettingsController.cs | 68 ++++++++--- Yavsc/Models/ApplicationDbContext.cs | 5 +- Yavsc/Views/BrusherProfile/Index.cshtml | 1 + 5 files changed, 56 insertions(+), 162 deletions(-) diff --git a/Yavsc/Controllers/BrusherProfileController.cs b/Yavsc/Controllers/BrusherProfileController.cs index a181bf1b..02280412 100644 --- a/Yavsc/Controllers/BrusherProfileController.cs +++ b/Yavsc/Controllers/BrusherProfileController.cs @@ -1,7 +1,3 @@ -using System.Threading.Tasks; -using Microsoft.AspNet.Mvc; -using System.Security.Claims; -using Microsoft.Data.Entity; using Yavsc.Models; using Yavsc.Models.Haircut; using Microsoft.AspNet.Authorization; @@ -16,35 +12,6 @@ namespace Yavsc.Controllers public BrusherProfileController(ApplicationDbContext context) : base(context) { } - override public async Task Edit(BrusherProfile brusherProfile) - { - if (string.IsNullOrEmpty(brusherProfile.UserId)) - { - // a creation - brusherProfile.UserId = User.GetUserId(); - if (ModelState.IsValid) - { - _context.BrusherProfile.Add(brusherProfile); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } - } - else if (ModelState.IsValid) - { - _context.Update(brusherProfile); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } - return View(brusherProfile); - } - // POST: BrusherProfile/Delete/5 - override public async Task DeleteConfirmed(string id) - { - BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id); - _context.BrusherProfile.Remove(brusherProfile); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } } } diff --git a/Yavsc/Controllers/FormationSettingsController.cs b/Yavsc/Controllers/FormationSettingsController.cs index 3b4a4974..7df2a455 100644 --- a/Yavsc/Controllers/FormationSettingsController.cs +++ b/Yavsc/Controllers/FormationSettingsController.cs @@ -1,120 +1,15 @@ -using System.Threading.Tasks; -using Microsoft.AspNet.Mvc; -using Microsoft.Data.Entity; +using Yavsc.Controllers.Generic; using Yavsc.Models; using Yavsc.Models.Workflow.Profiles; namespace Yavsc.Controllers { - public class FormationSettingsController : Controller + public class FormationSettingsController : SettingsController { - private ApplicationDbContext _context; - public FormationSettingsController(ApplicationDbContext context) + public FormationSettingsController(ApplicationDbContext context) : base(context) { - _context = context; } - // GET: FormationSettings - public async Task Index() - { - return View(await _context.FormationSettings.ToListAsync()); - } - - // GET: FormationSettings/Details/5 - public async Task Details(string id) - { - if (id == null) - { - return HttpNotFound(); - } - - FormationSettings formationSettings = await _context.FormationSettings.SingleAsync(m => m.UserId == id); - if (formationSettings == null) - { - return HttpNotFound(); - } - - return View(formationSettings); - } - - // GET: FormationSettings/Create - public IActionResult Create() - { - return View(); - } - - // POST: FormationSettings/Create - [HttpPost] - [ValidateAntiForgeryToken] - public async Task Create(FormationSettings formationSettings) - { - if (ModelState.IsValid) - { - _context.FormationSettings.Add(formationSettings); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } - return View(formationSettings); - } - - // GET: FormationSettings/Edit/5 - public async Task Edit(string id) - { - if (id == null) - { - return HttpNotFound(); - } - - FormationSettings formationSettings = await _context.FormationSettings.SingleAsync(m => m.UserId == id); - if (formationSettings == null) - { - return HttpNotFound(); - } - return View(formationSettings); - } - - // POST: FormationSettings/Edit/5 - [HttpPost] - [ValidateAntiForgeryToken] - public async Task Edit(FormationSettings formationSettings) - { - if (ModelState.IsValid) - { - _context.Update(formationSettings); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } - return View(formationSettings); - } - - // GET: FormationSettings/Delete/5 - [ActionName("Delete")] - public async Task Delete(string id) - { - if (id == null) - { - return HttpNotFound(); - } - - FormationSettings formationSettings = await _context.FormationSettings.SingleAsync(m => m.UserId == id); - if (formationSettings == null) - { - return HttpNotFound(); - } - - return View(formationSettings); - } - - // POST: FormationSettings/Delete/5 - [HttpPost, ActionName("Delete")] - [ValidateAntiForgeryToken] - public async Task DeleteConfirmed(string id) - { - FormationSettings formationSettings = await _context.FormationSettings.SingleAsync(m => m.UserId == id); - _context.FormationSettings.Remove(formationSettings); - await _context.SaveChangesAsync(); - return RedirectToAction("Index"); - } } } diff --git a/Yavsc/Controllers/Generic/SettingsController.cs b/Yavsc/Controllers/Generic/SettingsController.cs index 42b4511c..f36acbf9 100644 --- a/Yavsc/Controllers/Generic/SettingsController.cs +++ b/Yavsc/Controllers/Generic/SettingsController.cs @@ -1,24 +1,19 @@ -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; + private object dbSet; - protected IQueryable QueryableDbSet { get { - return (IQueryable) dbSet; - } } - protected ISet RwDbSet { get { - return (ISet) dbSet; + protected DbSet Settings { get { + return (DbSet) dbSet; } } public SettingsController(ApplicationDbContext context) @@ -29,7 +24,7 @@ namespace Yavsc.Controllers.Generic public async Task Index() { - var existing = await this.QueryableDbSet.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId()); + var existing = await this.Settings.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId()); return View(existing); } // GET: BrusherProfile/Details/5 @@ -40,7 +35,7 @@ namespace Yavsc.Controllers.Generic id = User.GetUserId(); } - var profile = await QueryableDbSet.SingleAsync(m => m.UserId == id); + var profile = await Settings.SingleAsync(m => m.UserId == id); if (profile == null) { return HttpNotFound(); @@ -53,7 +48,7 @@ namespace Yavsc.Controllers.Generic // GET: BrusherProfile/Create public IActionResult Create() { - return View(); + return View("Edit", new TSettings()); } // GET: BrusherProfile/Edit/5 @@ -64,7 +59,7 @@ namespace Yavsc.Controllers.Generic id = User.GetUserId(); } - TSettings setting = await QueryableDbSet.SingleOrDefaultAsync(m => m.UserId == id); + TSettings setting = await Settings.SingleOrDefaultAsync(m => m.UserId == id); if (setting == null) { setting = new TSettings { }; @@ -83,7 +78,7 @@ namespace Yavsc.Controllers.Generic return HttpNotFound(); } - var brusherProfile = await QueryableDbSet.SingleAsync(m => m.UserId == id); + var brusherProfile = await Settings.SingleAsync(m => m.UserId == id); if (brusherProfile == null) { return HttpNotFound(); @@ -92,13 +87,50 @@ namespace Yavsc.Controllers.Generic return View(brusherProfile); } - // POST: BrusherProfile/Delete/5 - [HttpPost, ActionName("Delete")] + // POST: FormationSettings/Create + [HttpPost] [ValidateAntiForgeryToken] - public abstract Task DeleteConfirmed(string id); - // POST: BrusherProfile/Edit/5 + public async Task Create(TSettings settings) + { + if (settings.UserId == null) settings.UserId = User.GetUserId(); + + if (ModelState.IsValid) + { + Settings.Add(settings); + await _context.SaveChangesAsync(); + return RedirectToAction("Index"); + } + return View("Edit",settings); + } + + // POST: FormationSettings/Edit/5 [HttpPost] [ValidateAntiForgeryToken] - public abstract Task Edit(TSettings profile); + public async Task Edit(TSettings settings) + { + if (settings.UserId == null) { + settings.UserId = User.GetUserId(); + Settings.Add(settings); + } else + _context.Update(settings); + if (ModelState.IsValid) + { + await _context.SaveChangesAsync(); + return RedirectToAction("Index"); + } + return View(settings); + } + + // POST: FormationSettings/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(string id) + { + TSettings formationSettings = await Settings.SingleAsync(m => m.UserId == id); + Settings.Remove(formationSettings); + await _context.SaveChangesAsync(); + return RedirectToAction("Index"); + } + } } \ No newline at end of file diff --git a/Yavsc/Models/ApplicationDbContext.cs b/Yavsc/Models/ApplicationDbContext.cs index ff53e8a3..0c3627f6 100644 --- a/Yavsc/Models/ApplicationDbContext.cs +++ b/Yavsc/Models/ApplicationDbContext.cs @@ -27,7 +27,6 @@ namespace Yavsc.Models using Workflow.Profiles; using Drawing; using Yavsc.Attributes; - using System.Collections.Generic; public class ApplicationDbContext : IdentityDbContext { @@ -55,10 +54,10 @@ namespace Yavsc.Models } } - public ISet GetDbSet() where TSettings : class, ISpecializationSettings + public DbSet GetDbSet() where TSettings : class, ISpecializationSettings { - return (ISet) GetDbSet(typeof(TSettings).FullName); + return (DbSet) GetDbSet(typeof(TSettings).FullName); } public IQueryable GetDbSet(string settingsClassName) { diff --git a/Yavsc/Views/BrusherProfile/Index.cshtml b/Yavsc/Views/BrusherProfile/Index.cshtml index c44b13aa..1be2eda5 100644 --- a/Yavsc/Views/BrusherProfile/Index.cshtml +++ b/Yavsc/Views/BrusherProfile/Index.cshtml @@ -346,6 +346,7 @@ } else { @SR["Aucun profile renseignĂ©"] + @SR["Renseigner ce paramĂȘtrage"] }