You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.5 KiB
112 lines
3.5 KiB
using System;
|
|
using System.Linq;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Configuration;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Infrastructure.Web.Mvc
|
|
{
|
|
[Area("Admin")]
|
|
public class ConfigurationController : BaseController
|
|
{
|
|
private readonly IConfigurationRoot _configuration;
|
|
|
|
public ConfigurationController(IConfigurationRoot configuration)
|
|
{
|
|
this._configuration = configuration;
|
|
}
|
|
|
|
[Authorize(Roles = "Read-EFConfigurationValue")]
|
|
public IActionResult Index()
|
|
{
|
|
using var db = GetContext();
|
|
var model = db.Values.ToList();
|
|
return View(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-EFConfigurationValue")]
|
|
public IActionResult Add()
|
|
{
|
|
return View(new EditEFConfigurationValueModel());
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-EFConfigurationValue")]
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public IActionResult Add(EditEFConfigurationValueModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
using var db = GetContext();
|
|
try
|
|
{
|
|
var entity = new EFConfigurationValue();
|
|
entity.From(model);
|
|
db.Values.Add(entity);
|
|
if (db.SaveChanges() > 0)
|
|
{
|
|
this._configuration.Reload();
|
|
}
|
|
return RedirectToAction("Index");
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", ex.Message);
|
|
}
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-EFConfigurationValue")]
|
|
public IActionResult Edit(string id)
|
|
{
|
|
using var db = GetContext();
|
|
var entity = db.Values.FirstOrDefault(o => o.Id == id);
|
|
var model = entity.To<EditEFConfigurationValueModel>();
|
|
return View(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-EFConfigurationValue")]
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public IActionResult Edit(EditEFConfigurationValueModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
using var db = GetContext();
|
|
try
|
|
{
|
|
var entity = db.Values.FirstOrDefault(o => o.Id == model.Id);
|
|
entity.From(model);
|
|
if (db.SaveChanges() > 0)
|
|
{
|
|
this._configuration.Reload();
|
|
}
|
|
return RedirectToAction("Index");
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", ex.Message);
|
|
}
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
public IActionResult Ajax(InputType type)
|
|
{
|
|
return PartialView("_Ajax", new EditEFConfigurationValueModel { Type = type });
|
|
}
|
|
|
|
private static EFConfigurationContext GetContext()
|
|
{
|
|
var builder = new DbContextOptionsBuilder<EFConfigurationContext>().UseSqlite("Data Source = configuration.db");
|
|
return new EFConfigurationContext(builder.Options);
|
|
}
|
|
}
|
|
} |