|
| 1 | +using Core.Services; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Common.Widgets |
| 8 | +{ |
| 9 | + [ViewComponent(Name = "Categories")] |
| 10 | + public class Categories : ViewComponent |
| 11 | + { |
| 12 | + IDataService _db; |
| 13 | + |
| 14 | + public Categories(IDataService db) |
| 15 | + { |
| 16 | + _db = db; |
| 17 | + } |
| 18 | + |
| 19 | + public async Task<IViewComponentResult> InvokeAsync(string theme, string widget) |
| 20 | + { |
| 21 | + var keyMax = $"{theme}-{widget}-max"; |
| 22 | + var keyTmpl = $"{theme}-{widget}-tmpl"; |
| 23 | + |
| 24 | + var max = _db.CustomFields.GetCustomValue(keyMax); |
| 25 | + var tmpl = _db.CustomFields.GetCustomValue(keyTmpl); |
| 26 | + |
| 27 | + if (string.IsNullOrEmpty(max)) max = "10"; |
| 28 | + if (string.IsNullOrEmpty(tmpl)) tmpl = "<a href=\"/categories/{0}\" class=\"list-group-item list-group-item-action\">{0}</a>"; |
| 29 | + |
| 30 | + var cats = await _db.BlogPosts.Categories(); |
| 31 | + var model = new List<string>(); |
| 32 | + |
| 33 | + foreach (var cat in cats.Take(int.Parse(max))) |
| 34 | + { |
| 35 | + model.Add(string.Format(tmpl, cat)); |
| 36 | + } |
| 37 | + |
| 38 | + return View("~/Views/Widgets/Categories/Index.cshtml", model); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [Route("widgets/api/categories")] |
| 43 | + public class CategoriesController : Controller |
| 44 | + { |
| 45 | + IDataService _db; |
| 46 | + |
| 47 | + public CategoriesController(IDataService db) |
| 48 | + { |
| 49 | + _db = db; |
| 50 | + } |
| 51 | + |
| 52 | + [HttpPost] |
| 53 | + [Route("edit")] |
| 54 | + public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecords, string txtTemplate, string hdnWidget, string hdnTheme) |
| 55 | + { |
| 56 | + var keyMax = $"{hdnTheme}-{hdnWidget}-max"; |
| 57 | + var keyTmpl = $"{hdnTheme}-{hdnWidget}-tmpl"; |
| 58 | + |
| 59 | + _db.CustomFields.SaveCustomValue(keyMax, txtMaxRecords); |
| 60 | + _db.CustomFields.SaveCustomValue(keyTmpl, txtTemplate); |
| 61 | + |
| 62 | + return Redirect("~/admin/settings/themes"); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments