Skip to content

Instantly share code, notes, and snippets.

View AndyButland's full-sized avatar

Andy Butland AndyButland

View GitHub Profile
public async Task<CollectionResult<PackageDto>> GetPackages(PackageSearchQuery query)
{
if (!IsSearchServiceConfigured())
{
return CollectionResult<PackageDto>.Empty();
}
SearchClient searchClient = GetSearchClient();
string searchText = GetSearchText(query);
public async Task<bool> AddOrUpdatePackage(PackageDto packageDto)
{
if (!IsSearchServiceConfigured())
{
_logger.LogWarning($"Package with Id {packageDto.PackageId} failed to index with Azure search as the search service is not configured.");
return false;
}
SearchIndexClient indexClient = GetIndexClient();
await CreateIndexIfNotExists(indexClient);
private async Task CreateIndex(SearchIndexClient indexClient)
{
var fieldBuilder = new FieldBuilder();
IList<SearchField> searchFields = fieldBuilder.Build(typeof(PackageModel));
var definition = new SearchIndex(_searchSettings.IndexName, searchFields);
// Add a scoring profile to boost text matches on more relevent fields.
const string DefaultScoringProfileName = "PackageSearch";
var scoringProfile = new ScoringProfile(DefaultScoringProfileName)
public async Task<bool> AddOrUpdatePackage(PackageDto packageDto)
{
if (!IsSearchServiceConfigured())
{
_logger.LogWarning($"Package with Id {packageDto.PackageId} failed to index with Azure search as the search service is not configured.");
return false;
}
SearchIndexClient indexClient = GetIndexClient();
await CreateIndexIfNotExists(indexClient);
public class PackageModel
{
private const string DateFormat = "yyyy-MM-dd";
public PackageModel()
{
}
public PackageModel(Guid id) => Id = id.ToString();
public static class DistributedCacheExtensions
{
public static async Task RemoveWithPrefixAsync(this IDistributedCache cache, string keyPrefix)
{
if (cache is RedisCache redisCache)
{
ConnectionMultiplexer connection = await redisCache.GetConnectionAsync();
IDatabase db = connection.GetDatabase();
foreach (EndPoint? endpoint in connection.GetEndPoints())
{
public static class DistributedCacheExtensions
{
public static async Task<T?> GetAsync<T>(this IDistributedCache cache, string key)
{
var val = await cache.GetAsync(key);
if (val == null || val.Length == 0)
{
return default;
}
var cacheKey = string.Format(ApplicationServices.Constants.DistributedCache.CacheKeyFormats.CategoryById, id);
CategoryDto? categoryDto = await _distributedCache.GetAsync<CategoryDto>(cacheKey);
if (categoryDto is not null)
{
return categoryDto;
}
categoryDto = await _categoryService.GetCategoryById(id);
if (categoryDto is null)
{
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
IConfigurationSection apiSettingsSection = Configuration.GetSection(Constants.ConfigurationKeys.ApiSettingsKeyName);
services.Configure<ApiSettings>(apiSettingsSection);
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Persistence.Dtos;
using Umbraco.Forms.Core.Services;
namespace Umbraco.Forms.Testsite.Controllers
{
public class SubmitFormTestController : UmbracoApiController
{