refactoring

This commit is contained in:
2017-03-06 02:04:33 +01:00
parent 80e7f5e2fb
commit a863aed34c
5 changed files with 57 additions and 163 deletions

View File

@ -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<IActionResult> 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<IActionResult> DeleteConfirmed(string id)
{
BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id);
_context.BrusherProfile.Remove(brusherProfile);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}

View File

@ -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<FormationSettings>
{
private ApplicationDbContext _context;
public FormationSettingsController(ApplicationDbContext context)
public FormationSettingsController(ApplicationDbContext context) : base(context)
{
_context = context;
}
// GET: FormationSettings
public async Task<IActionResult> Index()
{
return View(await _context.FormationSettings.ToListAsync());
}
// GET: FormationSettings/Details/5
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteConfirmed(string id)
{
FormationSettings formationSettings = await _context.FormationSettings.SingleAsync(m => m.UserId == id);
_context.FormationSettings.Remove(formationSettings);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}

View File

@ -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<TSettings> : Controller where TSettings : class, ISpecializationSettings, new()
{
protected ApplicationDbContext _context;
protected object dbSet;
private object dbSet;
protected IQueryable<TSettings> QueryableDbSet { get {
return (IQueryable<TSettings>) dbSet;
} }
protected ISet<TSettings> RwDbSet { get {
return (ISet<TSettings>) dbSet;
protected DbSet<TSettings> Settings { get {
return (DbSet<TSettings>) dbSet;
} }
public SettingsController(ApplicationDbContext context)
@ -29,7 +24,7 @@ namespace Yavsc.Controllers.Generic
public async Task<IActionResult> 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")]
[ValidateAntiForgeryToken]
public abstract Task<IActionResult> DeleteConfirmed(string id);
// POST: BrusherProfile/Edit/5
// POST: FormationSettings/Create
[HttpPost]
[ValidateAntiForgeryToken]
public abstract Task<IActionResult> Edit(TSettings profile);
public async Task<IActionResult> 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 async Task<IActionResult> 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<IActionResult> DeleteConfirmed(string id)
{
TSettings formationSettings = await Settings.SingleAsync(m => m.UserId == id);
Settings.Remove(formationSettings);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}

View File

@ -27,7 +27,6 @@ namespace Yavsc.Models
using Workflow.Profiles;
using Drawing;
using Yavsc.Attributes;
using System.Collections.Generic;
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
@ -55,10 +54,10 @@ namespace Yavsc.Models
}
}
public ISet<TSettings> GetDbSet<TSettings>() where TSettings : class, ISpecializationSettings
public DbSet<TSettings> GetDbSet<TSettings>() where TSettings : class, ISpecializationSettings
{
return (ISet<TSettings>) GetDbSet(typeof(TSettings).FullName);
return (DbSet<TSettings>) GetDbSet(typeof(TSettings).FullName);
}
public IQueryable<ISpecializationSettings> GetDbSet(string settingsClassName)
{

View File

@ -346,6 +346,7 @@
}
else {
@SR["Aucun profile renseigné"]
<a asp-action="Create" class="btn btn-success">@SR["Renseigner ce paramêtrage"]</a>
}
</div>
<p>