factorisation
This commit is contained in:
parent
f60584b0e4
commit
d14ee05a5d
3 changed files with 114 additions and 83 deletions
|
|
@ -5,69 +5,18 @@ using Microsoft.Data.Entity;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
using Yavsc.Models.Haircut;
|
using Yavsc.Models.Haircut;
|
||||||
using Microsoft.AspNet.Authorization;
|
using Microsoft.AspNet.Authorization;
|
||||||
|
using Yavsc.Controllers.Generic;
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
[Authorize(Roles="Performer")]
|
[Authorize(Roles="Performer")]
|
||||||
public class BrusherProfileController : Controller
|
public class BrusherProfileController : SettingsController<BrusherProfile>
|
||||||
{
|
{
|
||||||
private ApplicationDbContext _context;
|
|
||||||
|
|
||||||
public BrusherProfileController(ApplicationDbContext context)
|
public BrusherProfileController(ApplicationDbContext context) : base(context)
|
||||||
{
|
{
|
||||||
_context = context;
|
|
||||||
}
|
}
|
||||||
|
override public async Task<IActionResult> Edit(BrusherProfile brusherProfile)
|
||||||
// GET: BrusherProfile
|
|
||||||
public async Task<IActionResult> Index()
|
|
||||||
{
|
|
||||||
var existing = await _context.BrusherProfile.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId());
|
|
||||||
return View(existing);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET: BrusherProfile/Details/5
|
|
||||||
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> Edit(BrusherProfile brusherProfile)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(brusherProfile.UserId))
|
if (string.IsNullOrEmpty(brusherProfile.UserId))
|
||||||
{
|
{
|
||||||
|
|
@ -89,28 +38,8 @@ namespace Yavsc.Controllers
|
||||||
return View(brusherProfile);
|
return View(brusherProfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: BrusherProfile/Delete/5
|
|
||||||
[ActionName("Delete")]
|
|
||||||
public async Task<IActionResult> 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
|
// POST: BrusherProfile/Delete/5
|
||||||
[HttpPost, ActionName("Delete")]
|
override public async Task<IActionResult> DeleteConfirmed(string id)
|
||||||
[ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> DeleteConfirmed(string id)
|
|
||||||
{
|
{
|
||||||
BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id);
|
BrusherProfile brusherProfile = await _context.BrusherProfile.SingleAsync(m => m.UserId == id);
|
||||||
_context.BrusherProfile.Remove(brusherProfile);
|
_context.BrusherProfile.Remove(brusherProfile);
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ namespace Yavsc.Controllers
|
||||||
using Models.Workflow;
|
using Models.Workflow;
|
||||||
using Yavsc.Exceptions;
|
using Yavsc.Exceptions;
|
||||||
using Yavsc.ViewModels.Workflow;
|
using Yavsc.ViewModels.Workflow;
|
||||||
|
using YavscLib;
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class DoController : Controller
|
public class DoController : Controller
|
||||||
|
|
@ -56,16 +57,13 @@ namespace Yavsc.Controllers
|
||||||
|
|
||||||
bool hasConfigurableSettings = (userActivity.Does.SettingsClassName != null);
|
bool hasConfigurableSettings = (userActivity.Does.SettingsClassName != null);
|
||||||
if (hasConfigurableSettings) {
|
if (hasConfigurableSettings) {
|
||||||
|
|
||||||
ViewBag.ProfileType = Startup.ProfileTypes.Single(t=>t.FullName==userActivity.Does.SettingsClassName);
|
ViewBag.ProfileType = Startup.ProfileTypes.Single(t=>t.FullName==userActivity.Does.SettingsClassName);
|
||||||
|
var dbset = (IQueryable<ISpecializationSettings>) _context.GetDbSet(userActivity.Does.SettingsClassName);
|
||||||
|
|
||||||
var dbset = _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]");
|
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 {
|
return View(new UserActivityViewModel {
|
||||||
Declaration = userActivity,
|
Declaration = userActivity,
|
||||||
HasSettings = dbset?.Any(ua=>ua.UserId==id) ?? false,
|
HasSettings = dbset.Any(ua=>ua.UserId==id),
|
||||||
NeedsSettings = hasConfigurableSettings
|
NeedsSettings = hasConfigurableSettings
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
return View(new UserActivityViewModel {
|
return View(new UserActivityViewModel {
|
||||||
|
|
|
||||||
104
Yavsc/Controllers/Generic/SettingsController.cs
Normal file
104
Yavsc/Controllers/Generic/SettingsController.cs
Normal file
|
|
@ -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<TSettings> : Controller where TSettings : class, ISpecializationSettings, new()
|
||||||
|
{
|
||||||
|
protected ApplicationDbContext _context;
|
||||||
|
protected object dbSet;
|
||||||
|
|
||||||
|
protected IQueryable<TSettings> QueryableDbSet { get {
|
||||||
|
return (IQueryable<TSettings>) dbSet;
|
||||||
|
} }
|
||||||
|
protected ISet<TSettings> RwDbSet { get {
|
||||||
|
return (ISet<TSettings>) dbSet;
|
||||||
|
} }
|
||||||
|
|
||||||
|
public SettingsController(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
dbSet=_context.GetDbSet<TSettings>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
|
{
|
||||||
|
var existing = await this.QueryableDbSet.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId());
|
||||||
|
return View(existing);
|
||||||
|
}
|
||||||
|
// GET: BrusherProfile/Details/5
|
||||||
|
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteConfirmed(string id);
|
||||||
|
// POST: BrusherProfile/Edit/5
|
||||||
|
[HttpPost]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
|
public abstract Task<IActionResult> Edit(TSettings profile);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue