public override async Task <SaveCustomerPreferencesResponse> Handle(SaveCustomerPreferencesRequest request, CancellationToken cancellationToken) { var optedInPreferences = request.CustomerPreferences .Where(customerPreference => customerPreference.Value == true); var optedOutPreferences = request.CustomerPreferences .Where(customerPreference => customerPreference.Value == false); var invalidPreferences = new List <string>(); var preferences = await _cacheProvider.GetPreferences(cancellationToken); var customerPreferences = await _customerPreferenceService .GetCustomerPreferences(request.CustomerId, request.FromDate, cancellationToken); var savedCustomerPreferences = new List <CustomerPreference>(); foreach (var preference in request.CustomerPreferences) { Preference foundPreference; if ((foundPreference = _preferenceService.GetPreference(preferences, preference.Key)) == null) { invalidPreferences.Add(preference.Key); continue; } var customerPreference = _customerPreferenceService.GetCustomerPreference(customerPreferences, foundPreference.Id); if (customerPreference == null) { customerPreference = new CustomerPreference { CustomerId = request.CustomerId, PreferenceId = foundPreference.Id } } ; customerPreference.OptInDate = request.FromDate; customerPreference.NextCheckInDate = request.NextCheckInDate; savedCustomerPreferences.Add(await _customerPreferenceService .Save(customerPreference, false, false, cancellationToken)); } if (savedCustomerPreferences.Count > 0) { await _customerPreferenceService .CommitChanges(cancellationToken); } return(Response.Success <SaveCustomerPreferencesResponse>(savedCustomerPreferences.ToArray(), model => { model.InalidPreferences = invalidPreferences.ToArray(); model.TotalOptedIn = optedInPreferences.Count(); model.TotalOptedOut = optedOutPreferences.Count(); })); } }
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))); }