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.
iot/projects/Infrastructure/Areas/Admin/Controllers/SettingController.cs

128 lines
3.9 KiB

using Infrastructure.Application;
using Infrastructure.Application.Entites.Settings;
using Infrastructure.Application.Models;
using Infrastructure.Application.Services.Settings;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Infrastructure.Web.Mvc
{
[Area("Admin")]
public class SettingController : BaseController
{
private readonly ISettingService _settingService;
public SettingController(ISettingService settingService)
{
this._settingService = settingService;
}
[Authorize(Roles = "Read-Setting")]
public IActionResult Index(PagedList<Setting> 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());
return View(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 });
}
}
}