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.
117 lines
4.0 KiB
117 lines
4.0 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 Infrastructure.Web.Mvc;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Infrastructure.Areas.Admin.Controllers
|
|
{
|
|
[Area("Admin")]
|
|
public class SettingController : CrudController<Setting, EditSettingModel>
|
|
{
|
|
private readonly IRepository<Setting> _settingRepo;
|
|
private readonly ISettingService _settingService;
|
|
|
|
public SettingController(IRepository<Setting> settingRepo, ISettingService settingService) : base(settingRepo)
|
|
{
|
|
this._settingRepo = settingRepo;
|
|
this._settingService = settingService;
|
|
}
|
|
|
|
public override IQueryable<Setting> Query(PagedListModel<EditSettingModel> model, IQueryable<Setting> query)
|
|
{
|
|
return base.Query(model, query).WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name))
|
|
.WhereIf(model.Query.Type.HasValue, o => o.Type == model.Query.Type.Value)
|
|
.WhereIf(!string.IsNullOrEmpty(model.Query.Value), o => o.Value.Contains(model.Query.Value))
|
|
.OrderBy(o => o.Name);
|
|
}
|
|
|
|
public override void ToDisplayModel(Setting entity, EditSettingModel model)
|
|
{
|
|
ViewData.SelectList(o => model.Type, () => entity.Type.GetSelectList());
|
|
}
|
|
|
|
public override void ToEditModel(Setting entity, EditSettingModel model)
|
|
{
|
|
ViewData.SelectList(o => model.Type, () => this.GetSelectList<SettingType>(model.Type));
|
|
}
|
|
|
|
public override IActionResult Add(EditSettingModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var entity = new Setting();
|
|
entity.From(model);
|
|
_settingService.AddSetting(entity);
|
|
return this.Success();
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", $"{ex.Message}{ex.InnerException?.Message}");
|
|
}
|
|
}
|
|
this.ToEditModel(null, model);
|
|
return Result(model);
|
|
}
|
|
|
|
public override IActionResult Edit(EditSettingModel model)
|
|
{
|
|
var entity = _settingService.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
entity.From(model, skipReadonly: true);
|
|
_settingService.UpdateSetting(entity);
|
|
return this.Success();
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
ModelState.AddModelError("", ex.Message);
|
|
}
|
|
}
|
|
this.ToEditModel(entity, model);
|
|
return Result(model);
|
|
}
|
|
|
|
public override IActionResult Delete(List<Guid> list)
|
|
{
|
|
if (list == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(list));
|
|
}
|
|
try
|
|
{
|
|
foreach (var id in list)
|
|
{
|
|
var query = this.Repo.Table();
|
|
var entity = query.FirstOrDefault(o => o.Id == id);
|
|
_settingService.DeleteSetting(entity);
|
|
}
|
|
return this.Success();
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ex.PrintStack();
|
|
return this.Error(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Ajax(SettingType type)
|
|
{
|
|
return PartialView("_Ajax", new EditSettingModel { Type = type });
|
|
}
|
|
}
|
|
} |