using FHT.Agency.CommonTypes; using FHT.Agency.UI.InputModels; using FHT.Agency.Web.Filters; using FHT.ApiCommon; using FHT.Core.Agency.Interfaces; using FHT.Core.Queries.ImplementedInterfaces; using FHT.Core.UI.ViewModels; using FHT.Core.UI.ViewModels.TopicPost; using FHT.Utilities; 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/topictypes")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class TopicTypeApiController : ApiController { private readonly ITopicTypeQuery _topicTypeQuery; private readonly ITopicTypeService _topicTypeService; public TopicTypeApiController(ITopicTypeQuery topicTypeQuery, ITopicTypeService topicTypeService) { _topicTypeQuery = topicTypeQuery; _topicTypeService = topicTypeService; } [HttpGet] [ResponseType(typeof(List))] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task AllTopicTypesAsync() { List topicTypes = await _topicTypeQuery.GetAllTopicTypesAsync(); return Ok(topicTypes); } [HttpPost] [Route("_create", Name = nameof(CreateTopicTypeAsync))] [ResponseType(typeof(void))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task CreateTopicTypeAsync(CreateTopicTypeInputModel model) { await _topicTypeService.CreateOrUpdateAsync(null, model.Title, model.EnglishTitle, model.Pinyin, model.Description, ImageConverter.ToRelativeUrl(model.PublishIconUrl), ImageConverter.ToRelativeUrl(model.WebIconUrl), ImageConverter.ToRelativeUrl(model.BusinessOpportunityIconUrl),model.Order, true); return Ok(); } [HttpGet] [Route("{id:int:min(1)}")] [ResponseType(typeof(CreateTopicTypeInputModel))] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task GetAsync(int id) { TopicTypeViewModel topicPost = await _topicTypeQuery.GetTopicTypeFromDbAsync(id); var model = new CreateTopicTypeInputModel() { TopicTypeId = id, Description = topicPost.Description, EnglishTitle = topicPost.EnglishTitle, Order = topicPost.Order, Pinyin = topicPost.PinYin, PublishIconUrl = new ImageViewModel(topicPost.PublishIcon).AbsoluteUrl, BusinessOpportunityIconUrl = new ImageViewModel(topicPost.BusinessOpportunityIcon).AbsoluteUrl, WebIconUrl = new ImageViewModel(topicPost.WebIcon).AbsoluteUrl, Title = topicPost.Name }; return Ok(model); } [HttpPost] [Route("_update")] [ResponseType(typeof(void))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task UpdateAsync(CreateTopicTypeInputModel model) { bool isActive = true; if (model.TopicTypeId.HasValue) { TopicTypeViewModel topicType = await _topicTypeQuery.GetTopicTypeFromDbAsync(model.TopicTypeId.Value); isActive = topicType.IsActive; } await _topicTypeService.CreateOrUpdateAsync(model.TopicTypeId, model.Title, model.EnglishTitle, model.Pinyin, model.Description, ImageConverter.ToRelativeUrl(model.PublishIconUrl), ImageConverter.ToRelativeUrl(model.WebIconUrl), ImageConverter.ToRelativeUrl(model.BusinessOpportunityIconUrl), model.Order, isActive); return Ok(); } [HttpPost] [Route("_enabled")] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task EnabledAsync(int topicTypeId) { await _topicTypeService.ChangeActiveStatusAsync(topicTypeId,true); return Ok(); } [HttpPost] [Route("_disabled")] [ResponseType(typeof(void))] [ApiClaimsAuthorize(FhtUserClaimType.TopicTypeManagement)] public async Task DisabledAsync(int topicTypeId) { await _topicTypeService.ChangeActiveStatusAsync(topicTypeId, false); return Ok(); } } }