public async Task <Boolean> ApplyBackendConfiguration() { // Ignore if we're not authenticated. if (CurrentUser.AccessToken == null || CurrentUser.AccessToken == "") { return(false); } Boolean result = false; // Retrieve preferences from the backend and apply them here. UserPreferenceDTO prefs = await WebAPI.GetUserPrefsAsync(); if (prefs != null) { result = true; CurrentUser.ConversationLimit = prefs.ConversationLimit; // Go through all categories and change IsActive flags depending on how they're // set in the preferences we just retrieved. foreach (Category c in categories) { c.IsActive = prefs.Categories.Contains(c.Name); await DataAccessLayer.Current.UpdateOrAddCategoryAsync(c, CategoryKeepActive.Force); } CurrentUser.ActiveCategories = GetCategoryList(CategorySet.Active); } await CurrentUser.Save(); return(result); }
private async Task HandleMessage(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { _logger.LogInformation("Running dialog with Message Activity."); var conversation = await _helper.UserAccessor.GetAsync(turnContext, () => new UserConversationDTO()); string input = turnContext.Activity.Text?.Trim(); // await turnContext.SendActivityAsync(MessageFactory.Text($"Response {input}."), cancellationToken); // check if user has given the expected option if (string.IsNullOrWhiteSpace(conversation?.UserPreference?.UserOption) && !string.IsNullOrWhiteSpace(input)) { var option = UserPreferenceDTO.ChatbotOptions().FirstOrDefault(opt => opt.IsEqual(input)); if (option != null) { conversation.UserPreference.UserOption = option; } } if (!string.IsNullOrWhiteSpace(conversation?.UserPreference?.UserOption)) { // Run the Dialog with the new message Activity. await _dialog.Run(turnContext, _helper.ConversationState.CreateProperty <DialogState>(nameof(DialogState)), cancellationToken); } else { await turnContext.SendActivityAsync(MessageFactory.Text("Eu espero alguma das opções acima."), cancellationToken); } }
public async Task PutUpdatesDB() { var data = new List <UserPreference>().AsQueryable(); var categories = new List <Category>() { new Category { Name = "CAT1" }, new Category { Name = "CAT2" }, }.AsQueryable(); // Mock DbSet with no existing user preferences var mockSet = Helpers.CreateMockSet(data); mockCtx.SetupGet(mc => mc.UserPreferences).Returns(mockSet.Object); var mockCategorySet = Helpers.CreateMockSet(categories); mockCtx.SetupGet(mc => mc.Categories).Returns(mockCategorySet.Object); var dto = new UserPreferenceDTO { Categories = new string[] { "CAT1", "CAT2" } }; var result = await controller.PutUserPreference(dto); mockSet.Verify(m => m.Add(It.IsAny <UserPreference>()), Times.Once()); mockCtx.Verify(m => m.SaveChangesAsync(), Times.Once()); }
public async Task PutInvalidCategoryReturnsBadRequest() { var categories = new List <Category>() { new Category { Name = "CAT1" }, new Category { Name = "CAT2" }, }.AsQueryable(); // Mock DbSet with no existing user preferences var mockCategorySet = Helpers.CreateMockSet(categories); mockCtx.SetupGet(mc => mc.Categories).Returns(mockCategorySet.Object); var dto = new UserPreferenceDTO { Categories = new string[] { "CAT1", "CAT3" } // Includes bogus category }; var result = await controller.PutUserPreference(dto); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); }
public async Task PutNullCategoryReturnsBadRequest() { var parent = new UserPreferenceDTO { Categories = null }; var result = await controller.PutUserPreference(parent); Assert.IsInstanceOfType(result, typeof(BadRequestResult)); }
private async Task SendHelpSuggestionsCardAsync(ITurnContext turnContext, CancellationToken cancellationToken) { var card = new ThumbnailCard(); card.Subtitle = "Seja bem-vindo! Eu sou o seu novo Assistente Virtual.\nA minha função é ajudá-lo a obter o seu microcrédito de modo interativo. Como eu posso te ajudar?"; card.Buttons = UserPreferenceDTO.ChatbotOptions().Select(option => new CardAction( ActionTypes.ImBack, title: option, value: option )).ToList(); var response = MessageFactory.Attachment(card.ToAttachment()); await turnContext.SendActivityAsync(response, cancellationToken); }
public async Task <IHttpActionResult> PutUserPreference(UserPreferenceDTO userPrefDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (userPrefDto == null) { return(BadRequest("Null object")); } if (userPrefDto.Categories == null) { return(BadRequest()); // DataAnnotations is not supported in PLC :-( } var userId = User.Identity.GetUserId(); var categories = db.Categories .Where(x => userPrefDto.Categories.Contains(x.Name)) .ToList(); if (categories.Count != userPrefDto.Categories.Count) { // Client sent a bad category name return(BadRequest("Invalid category name")); } UserPreference userPreference = new UserPreference { ApplicationUser_Id = userId, ConversationLimit = userPrefDto.ConversationLimit, SortOrder = userPrefDto.SortOrder, UserCategory = new List <UserCategory>() }; if (UserPreferenceExists(userId)) { db.UserPreferences.Attach(userPreference); db.Entry(userPreference).State = EntityState.Modified; db.Entry(userPreference).Collection(x => x.UserCategory).Load(); userPreference.UserCategory.Clear(); foreach (var c in categories) { userPreference.UserCategory.Add(new UserCategory { Category = c, ApplicationUser_Id = userId }); } } else { foreach (var c in categories) { userPreference.UserCategory.Add(new UserCategory { Category = c, ApplicationUser_Id = userId }); } db.UserPreferences.Add(userPreference); } await db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); }