using System;
using FHT.ApiCommon;
using FHT.Management.Interfaces.DependentInterfaces.Services;
using FHT.Management.Interfaces.InputModels;
using FHT.Utilities;
using System.Web.Http;
using System.Web.Http.Description;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using FHT.Agency.Web.Filters;
using FHT.Agency.CommonTypes;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Cors;
namespace FHT.Agency.Web.ApiControllers
{
///
///
///
[Authorize]
[RoutePrefix("api/keyvalues")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class KeyValueApiController : ApiController
{
private readonly IManagementKeyValueService _managementKeyValueService;
public KeyValueApiController(IManagementKeyValueService managementKeyValueService)
{
_managementKeyValueService = managementKeyValueService;
}
#region 自动加链词
[HttpGet]
[ResponseType(typeof(StringInputModel))]
[Route("GetAutoLinkKeywordsContent")]
[ApiClaimsAuthorize(FhtUserClaimType.AutoLinkManagement)]
public async Task GetAutoLinkKeywordsContentAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.AutoLinkKeywords);
var model = new StringInputModel();
if (!string.IsNullOrEmpty(value))
{
List words = JsonConvert.DeserializeObject>(value);
model.Content = string.Join("\r\n", words);
}
return Ok(model);
}
[HttpPost]
[ValidateModel]
[Route("_autoLinkKeywordsCreateOrUpdate")]
[ClaimsAuthorize(FhtUserClaimType.AutoLinkManagement)]
[ResponseType(typeof(void))]
public async Task AutoLinkKeywordsCreateOrUpdateAsync(StringInputModel model)
{
var words = new List();
if (!string.IsNullOrEmpty(model.Content))
{
words = model.Content.Split(new string[] { "\r\n" }, StringSplitOptions.None).Distinct().ToList();
}
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.AutoLinkKeywords, JsonConvert.SerializeObject(words));
return Ok();
}
#endregion
#region 首页菜单导航
[HttpPost]
[Route("_menuNavCreateOrUpdate")]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.MenuNavManagement)]
public async Task MenuNavCreateOrUpdateAsync(MenuNavInputModel model)
{
string strJson = JsonConvert.SerializeObject(model);
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.MenuNavs, strJson);
return Ok();
}
[HttpGet]
[ResponseType(typeof(MenuNavInputModel))]
[Route("GetMenuNavContent")]
[ApiClaimsAuthorize(FhtUserClaimType.MenuNavManagement)]
public async Task GetMenuNavContentAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.MenuNavs);
var menuNav = new MenuNavInputModel();
if (!string.IsNullOrEmpty(value))
{
JObject json = JObject.Parse(value);
menuNav = json.ToObject();
}
return Ok(menuNav);
}
[HttpGet]
[ResponseType(typeof(StringInputModel))]
[Route("GetMenuNavsJson")]
[ApiClaimsAuthorize(FhtUserClaimType.MenuNavManagement)]
public async Task GetMenuNavsJsonAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.MenuNavs);
return Ok(new StringInputModel
{
Content = value
});
}
[HttpPost]
[Route("_menuNavJsonUpdateOrCreate")]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.MenuNavManagement)]
public async Task MenuNavJsonUpdateOrCreateAsync(StringInputModel model)
{
if (!string.IsNullOrEmpty(model.Content))
{
JObject json = JObject.Parse(model.Content);
MenuNavInputModel menuNavInputModel = json.ToObject();
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.MenuNavs, JsonConvert.SerializeObject(menuNavInputModel));
}
return Ok();
}
#endregion
#region 特色货源
[HttpPost]
[Route("_featuredProductsCreateOrUpdate")]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.FeaturedProductManagement)]
public async Task FeaturedProductsCreateOrUpdateAsync(FeaturedProductsInputModel model)
{
string strJson = JsonConvert.SerializeObject(model);
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.FeaturedProducts, strJson);
return Ok();
}
[HttpGet]
[ResponseType(typeof(FeaturedProductsInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.FeaturedProductManagement)]
[Route("GetFeaturedProducts")]
public async Task GetFeaturedProductsAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.FeaturedProducts);
var featuredProducts = new FeaturedProductsInputModel();
if (!string.IsNullOrEmpty(value))
{
JObject json = JObject.Parse(value);
featuredProducts = json.ToObject();
}
return Ok(featuredProducts);
}
[HttpGet]
[ResponseType(typeof(StringInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.FeaturedProductManagement)]
[Route("GetFeaturedProductsJson")]
public async Task GetFeaturedProductsJsonAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.FeaturedProducts);
return Ok(new StringInputModel
{
Content = value
});
}
[HttpPost]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.FeaturedProductManagement)]
[Route("_featuredProductJsonUpdateOrCreate")]
public async Task FeaturedProductJsonUpdateOrCreateAsync(StringInputModel model)
{
if (!string.IsNullOrEmpty(model.Content))
{
JObject json = JObject.Parse(model.Content);
FeaturedProductsInputModel featuredProductsInputModel = json.ToObject();
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.FeaturedProducts, JsonConvert.SerializeObject(featuredProductsInputModel));
}
return Ok();
}
#endregion
#region 热门词广告管理
[HttpPost]
[ValidateModel]
[ResponseType(typeof(void))]
[ApiClaimsAuthorize(FhtUserClaimType.HotWordAdverManagement)]
[Route("_hotWordPromotionsCreateOrUpdate")]
public async Task HotWordPromotionsCreateOrUpdateAsync(HotWordPromotionInputModel model)
{
string strJson = JsonConvert.SerializeObject(model);
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.HotWordPromotions, strJson);
return Ok();
}
[HttpGet]
[ResponseType(typeof(HotWordPromotionInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.HotWordAdverManagement)]
[Route("GetHotWordPromotion")]
public async Task GetHotWordPromotionAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.HotWordPromotions);
var hotwordPromotions = new HotWordPromotionInputModel();
if (!string.IsNullOrEmpty(value))
{
JObject json = JObject.Parse(value);
hotwordPromotions = json.ToObject();
}
return Ok(hotwordPromotions);
}
[HttpGet]
[ResponseType(typeof(StringInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.HotWordAdverManagement)]
[Route("GetHotWordPromotionJson")]
public async Task GetHotWordPromotionJsonAsync()
{
string value = await _managementKeyValueService.GetByKeyAsync(KeyValueKey.HotWordPromotions);
return Ok(new StringInputModel
{
Content = value
});
}
[HttpPost]
[ValidateModel]
[ResponseType(typeof(void))]
[ApiClaimsAuthorize(FhtUserClaimType.HotWordAdverManagement)]
[Route("_hotWordPromotionJsonUpdateOrCreate")]
public async Task HotWordPromotionJsonUpdateOrCreateAsync(StringInputModel model)
{
if (!string.IsNullOrEmpty(model.Content))
{
JObject json = JObject.Parse(model.Content);
FeaturedProductsInputModel featuredProductsInputModel = json.ToObject();
await _managementKeyValueService.CreateOrUpdateAsync(KeyValueKey.HotWordPromotions, JsonConvert.SerializeObject(featuredProductsInputModel));
}
return Ok();
}
#endregion
}
}