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.
158 lines
5.1 KiB
158 lines
5.1 KiB
using Infrastructure.Application;
|
|
using Infrastructure.Application.Entites.Settings;
|
|
using Infrastructure.Application.Models;
|
|
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Infrastructure.Web.Mvc
|
|
{
|
|
[Area("Admin")]
|
|
public class SettingController : BaseController
|
|
{
|
|
private readonly IRepository<Setting> _settingRepo;
|
|
private readonly ISettingService _settingService;
|
|
|
|
public SettingController(IRepository<Setting> settingRepo,ISettingService settingService)
|
|
{
|
|
this._settingRepo = settingRepo;
|
|
this._settingService = settingService;
|
|
}
|
|
|
|
[Authorize(Roles = "Read-Setting")]
|
|
public IActionResult Index(PagedListModel<EditSettingModel> model)
|
|
{
|
|
if (model is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
var query = _settingService.ReadonlyTable();
|
|
model.TotalCount = query.Count();
|
|
model.List.AddRange(query.Skip(model.PageSize * (model.PageIndex - 1))
|
|
.Take(model.PageSize)
|
|
.ToList()
|
|
.Select(o=>o.To<EditSettingModel>()));
|
|
ViewData.SelectList(o=>model.Query.Type,()=>this.GetSelectList<SettingType>(model.Query.Type));
|
|
return Result(model);
|
|
}
|
|
|
|
[HttpGet]
|
|
public virtual IActionResult Details(Guid id)
|
|
{
|
|
var entity = this._settingRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == id);
|
|
var model = entity.To<EditSettingModel>();
|
|
return Result(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-Setting")]
|
|
public IActionResult Add()
|
|
{
|
|
return View(new EditSettingModel());
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-Setting")]
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public IActionResult Add(EditSettingModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var entity = new Setting();
|
|
entity.From(model);
|
|
_settingService.AddSetting(entity);
|
|
return RedirectToAction("Index");
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", ex.Message);
|
|
}
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-Setting")]
|
|
public IActionResult Edit(Guid id)
|
|
{
|
|
var entity = _settingService.ReadonlyTable().FirstOrDefault(o => o.Id == id);
|
|
var model = entity.To<EditSettingModel>();
|
|
return View(model);
|
|
}
|
|
|
|
[Authorize(Roles = "Edit-Setting")]
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public IActionResult Edit(EditSettingModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var entity = _settingService.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
entity.From(model);
|
|
_settingService.UpdateSetting(entity);
|
|
return RedirectToAction("Index");
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", ex.Message);
|
|
}
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public virtual IActionResult Delete(List<Guid> list)
|
|
{
|
|
if (list == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(list));
|
|
}
|
|
try
|
|
{
|
|
foreach (var id in list)
|
|
{
|
|
var entity = _settingService.Table().FirstOrDefault(o => o.Id == id);
|
|
_settingService.DeleteSetting(entity);
|
|
}
|
|
return RedirectTo();
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
return RedirectTo(rawMesage: ex.Message);
|
|
}
|
|
}
|
|
|
|
public IActionResult Ajax(SettingType type)
|
|
{
|
|
return PartialView("_Ajax", new EditSettingModel { Type = type });
|
|
}
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
private IActionResult Result(object model)
|
|
{
|
|
if (this.Request.Headers["accept"].ToString().Contains("json", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return Json(new
|
|
{
|
|
schema = this.GetJsonSchema<EditSettingModel>(),
|
|
model,
|
|
data = ViewData
|
|
}, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
|
|
}
|
|
return View(model);
|
|
}
|
|
}
|
|
} |