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 _settingRepo; private readonly ISettingService _settingService; public SettingController(IRepository settingRepo, ISettingService settingService) { this._settingRepo = settingRepo; this._settingService = settingService; } [Authorize(Roles = "Read-Setting")] public IActionResult Index(PagedListModel model) { if (model is null) { throw new ArgumentNullException(nameof(model)); } var query = _settingService.ReadonlyTable() .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); model.TotalCount = query.Count(); model.List.AddRange(query.Skip(model.PageSize * (model.PageIndex - 1)) .Take(model.PageSize) .ToList() .Select(o => o.To())); ViewData.SelectList(o => model.Query.Type, () => this.GetSelectList(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(); 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(); 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 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(), model, data = ViewData }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); } return View(model); } } }