Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

76 lines
2.4 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Squidex.Areas.Api.Controllers.News.Models;
using Squidex.ClientLibrary;
namespace Squidex.Areas.Api.Controllers.News.Service
{
public sealed class FeaturesService
{
private const int FeatureVersion = 18;
private readonly QueryContext flatten = QueryContext.Default.Flatten();
private readonly IContentsClient<NewsEntity, FeatureDto> client;
public sealed class NewsEntity : Content<FeatureDto>
{
}
public FeaturesService(IOptions<MyNewsOptions> options)
{
if (options.Value.IsConfigured())
{
var squidexOptions = new SquidexOptions
{
AppName = options.Value.AppName,
ClientId = options.Value.ClientId,
ClientSecret = options.Value.ClientSecret,
Url = "https://cloud.squidex.io"
};
var clientManager = new SquidexClientManager(squidexOptions);
client = clientManager.CreateContentsClient<NewsEntity, FeatureDto>("feature-news");
}
}
public async Task<FeaturesDto> GetFeaturesAsync(int version = 0)
{
var result = new FeaturesDto
{
Version = FeatureVersion
};
if (client != null && version < FeatureVersion)
{
try
{
var query = new ContentQuery
{
Filter = $"data/version/iv ge {FeatureVersion}"
};
var features = await client.GetAsync(query, flatten);
result.Features = features.Items.Select(x => x.Data).ToList();
}
catch
{
result.Features = new List<FeatureDto>();
}
}
result.Features ??= new List<FeatureDto>();
return result;
}
}
}