yavsc/Yavsc/Controllers/Generic/SettingsController.cs

140 lines
4.0 KiB
C#

8 years ago
using System.Security.Claims;
using System.Threading.Tasks;
8 years ago
using Microsoft.AspNet.Authorization;
8 years ago
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
8 years ago
using Yavsc.Exceptions;
8 years ago
using Yavsc.Models;
using YavscLib;
namespace Yavsc.Controllers.Generic
{
8 years ago
[Authorize]
8 years ago
public abstract class SettingsController<TSettings> : Controller where TSettings : class, ISpecializationSettings, new()
{
protected ApplicationDbContext _context;
8 years ago
private object dbSet;
8 years ago
8 years ago
protected DbSet<TSettings> Settings { get {
return (DbSet<TSettings>) dbSet;
8 years ago
} }
public SettingsController(ApplicationDbContext context)
{
_context = context;
dbSet=_context.GetDbSet<TSettings>();
8 years ago
if (dbSet==null) throw new InvalidWorkflowModelException(this.GetType().Name);
8 years ago
}
public async Task<IActionResult> Index()
{
8 years ago
var existing = await this.Settings.SingleOrDefaultAsync(p=>p.UserId == User.GetUserId());
8 years ago
return View(existing);
}
// GET: BrusherProfile/Details/5
public async Task<IActionResult> Details(string id)
{
if (id == null)
{
id = User.GetUserId();
}
8 years ago
var profile = await Settings.SingleAsync(m => m.UserId == id);
8 years ago
if (profile == null)
{
return HttpNotFound();
}
return View(profile);
}
// GET: BrusherProfile/Create
public IActionResult Create()
{
8 years ago
return View("Edit", new TSettings());
8 years ago
}
// GET: BrusherProfile/Edit/5
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
id = User.GetUserId();
}
8 years ago
TSettings setting = await Settings.SingleOrDefaultAsync(m => m.UserId == id);
8 years ago
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();
}
8 years ago
var brusherProfile = await Settings.SingleAsync(m => m.UserId == id);
8 years ago
if (brusherProfile == null)
{
return HttpNotFound();
}
return View(brusherProfile);
}
8 years ago
// POST: FormationSettings/Create
[HttpPost]
8 years ago
[ValidateAntiForgeryToken]
8 years ago
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
8 years ago
[HttpPost]
[ValidateAntiForgeryToken]
8 years ago
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");
}
8 years ago
}
}