public override async Task <SavePreferenceResponse> Handle(SavePreferenceRequest request, CancellationToken cancellationToken) { var isNewCategory = false; var preferences = await _cacheProvider.GetPreferences(cancellationToken); var preference = _preferenceService.GetPreference(preferences, request.Name); if (preference != null) { return(Response.Failed <SavePreferenceResponse>(new ValidationFailure(nameof(request.Name), "Preference already exists"))); } var categories = await _cacheProvider.GetCategories(cancellationToken); var category = string.IsNullOrWhiteSpace(request.CategoryName) ? _categoryService.GetCategory(categories, request.CategoryId) : _categoryService.GetCategory(categories, request.CategoryName); preference = new Preference { Name = request.Name, }; if (category == null) { if (string.IsNullOrWhiteSpace(request.CategoryName)) { return(Response.Failed <SavePreferenceResponse>( new ValidationFailure(nameof(request.CategoryId), "Unable to find category"))); } category = await _categoryService.Save(new Category { Name = request.CategoryName }, false, false, cancellationToken); preference.Category = category; isNewCategory = true; } else { preference.CategoryId = category.Id; } preference = await _preferenceService.Save(preference, true, true, cancellationToken); return(Response.Success <SavePreferenceResponse>(preference, configure => { configure.IsNewCategory = isNewCategory; configure.CategoryId = category.Id; })); }
public override async Task <GetPreferencesResponse> Handle(GetPreferencesRequest request, CancellationToken cancellationToken) { var preferences = await _crmCacheProvider.GetPreferences(cancellationToken); if (string.IsNullOrWhiteSpace(request.CategoryName)) { return(Response.Success <GetPreferencesResponse>(preferences)); } var categories = await _crmCacheProvider.GetCategories(cancellationToken); Category foundCategory; if ((foundCategory = _categoryService.GetCategory(categories, request.CategoryName)) == null) { return(Response.Failed <GetPreferencesResponse>(new ValidationFailure(nameof(request.CategoryName), $"Unable to find a category named { request.CategoryName }"))); } return(Response.Success <GetPreferencesResponse>(_preferenceService .GetPreferencesByCategory(preferences, foundCategory.Id))); }