using FHT.Agency.CommonTypes;
using FHT.Agency.Web.Filters;
using FHT.ApiCommon;
using FHT.Core.CommonTypes;
using FHT.Core.UI.ViewModels;
using FHT.DDD;
using FHT.Management.Interfaces.DependentInterfaces.Services;
using FHT.Management.Interfaces.InputModels;
using FHT.Management.Interfaces.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Description;
namespace FHT.Agency.Web.ApiControllers
{
[Authorize]
[RoutePrefix("api/faqs")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class FaqApiController : ApiController
{
private readonly IManagementFaqService _faqService;
public FaqApiController(IManagementFaqService faqService)
{
_faqService = faqService;
}
///
/// 检索数据
///
///
///
///
///
///
[HttpGet]
[Route("_search")]
[ResponseType(typeof(QueryResult))]
[ApiClaimsAuthorize(FhtUserClaimType.FaqManage)]
public async Task SearchAsync(FaqType? faqType = null, string q = "", int currPage = 1, int limit = 6)
{
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
QueryResult results = await _faqService.SearchAsync(faqType, q, limit, offset);
return Ok(results);
}
[HttpGet]
[Route("{id:int:min(1)}")]
[ResponseType(typeof(FaqInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.FaqCreate)]
public async Task GetAsync(int id)
{
FaqInputModel model = await _faqService.GetAsync(id);
return Ok(model);
}
[HttpPost]
[Route("_create", Name = nameof(CreateFaqAsync))]
[ResponseType(typeof(CreatedViewModel))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.FaqCreate)]
public async Task CreateFaqAsync(FaqInputModel model)
{
int faqId = await _faqService.CreateOrUpdateAsync(model);
string url = Url.Link(nameof(CreateFaqAsync), new { faqId });
return Created(url, new CreatedViewModel { Id = faqId, Location = url });
}
[HttpPost]
[Route("_update")]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.FaqCreate)]
public async Task UpdateAsync(FaqInputModel model)
{
await _faqService.CreateOrUpdateAsync(model);
return Ok();
}
[HttpPost]
[Route("_delete")]
[ResponseType(typeof(void))]
[ApiClaimsAuthorize(FhtUserClaimType.FaqManage)]
public async Task DeleteByIdsAsync(int[] faqIds)
{
await _faqService.DeleteByIdsAsync(faqIds);
return Ok();
}
[HttpGet]
[AllowAnonymous]
[ResponseType(typeof(List))]
[Route("_getByType")]
public async Task GetByTypeAsync(FaqType? faqType = null)
{
List res = await _faqService.GetAllByTypeAsync(faqType);
return Ok(res);
}
}
}