Skip to content

Commit

Permalink
#106 Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusMeyer13 committed Mar 5, 2020
1 parent f0466cc commit df946d6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 32 deletions.
2 changes: 1 addition & 1 deletion PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public DeleteOrderDialog(IOptions<BotConfig> config, IBotTelemetryClient telemet
this.AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
this.AddDialog(new TextPrompt(nameof(TextPrompt)));
this.AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
this.AddDialog(new NextOrder(config, telemetryClient));
this.AddDialog(new NextOrder(config, telemetryClient, this.clientFactory));

// The initial child Dialog to run.
this.InitialDialogId = nameof(WaterfallDialog);
Expand Down
91 changes: 68 additions & 23 deletions PlanB.Butler.Bot/Dialogs/NextOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Resources;
using System.Threading;
Expand All @@ -17,6 +18,7 @@
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using PlanB.Butler.Bot.Services;

namespace PlanB.Butler.Bot.Dialogs
{
Expand All @@ -25,6 +27,11 @@ namespace PlanB.Butler.Bot.Dialogs
/// </summary>
public class NextOrder : ComponentDialog
{
/// <summary>
/// The client factory.
/// </summary>
private readonly IHttpClientFactory clientFactory;

private static Plan plan = new Plan();
private static int dayId;
private static bool valid;
Expand Down Expand Up @@ -85,7 +92,7 @@ public class NextOrder : ComponentDialog
/// NextOrderConstructor.
/// </summary>
/// <param name="config">The configuration.</param>
public NextOrder(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient)
public NextOrder(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient, IHttpClientFactory httpClientFactory)
: base(nameof(NextOrder))
{
ResourceManager rm = new ResourceManager("PlanB.Butler.Bot.Dictionary.Dialogs", Assembly.GetExecutingAssembly());
Expand All @@ -105,6 +112,7 @@ public NextOrder(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient

this.botConfig = config;
this.telemetryClient = telemetryClient;
this.clientFactory = httpClientFactory;

//for (int i = 0; i < weekDays.Length; i++)
//{
Expand Down Expand Up @@ -141,21 +149,41 @@ public NextOrder(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient
this.InitialDialogId = nameof(WaterfallDialog);
}

private PlanDay planDay;

private async Task<DialogTurnResult> CompanyStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Get the Plan
try
{
string food = BotMethods.GetDocument("eatingplan", "ButlerOverview.json", this.botConfig.Value.StorageAccountUrl, this.botConfig.Value.StorageAccountKey);
plan = JsonConvert.DeserializeObject<Plan>(food);
dayId = plan.Planday.FindIndex(x => x.Name == DateTime.Now.DayOfWeek.ToString().ToLower());
valid = true;
}
catch
//try
//{
// string food = BotMethods.GetDocument("eatingplan", "ButlerOverview.json", this.botConfig.Value.StorageAccountUrl, this.botConfig.Value.StorageAccountKey);
// plan = JsonConvert.DeserializeObject<Plan>(food);
// dayId = plan.Planday.FindIndex(x => x.Name == DateTime.Now.DayOfWeek.ToString().ToLower());
// valid = true;
//}
//catch
//{
// valid = false;
//}

IMealService mealService = new MealService(this.clientFactory.CreateClient(), this.botConfig.Value);
var meals = await mealService.GetMeals(string.Empty, string.Empty);
var mealEnumerator = meals.GetEnumerator();
this.planDay = new PlanDay();
while (mealEnumerator.MoveNext())
{
valid = false;
if (string.IsNullOrEmpty(this.planDay.Restaurant1))
{
this.planDay.Restaurant1 = mealEnumerator.Current.Restaurant;
}

if (string.IsNullOrEmpty(this.planDay.Restaurant2) && this.planDay.Restaurant1 != mealEnumerator.Current.Restaurant)
{
this.planDay.Restaurant2 = mealEnumerator.Current.Restaurant;
}
}


stepContext.Values["name"] = stepContext.Context.Activity.From.Name;
if (companyStatus == "extern")
{
Expand Down Expand Up @@ -239,10 +267,9 @@ private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext s
return await stepContext.NextAsync(null, cancellationToken);
}

private static async Task<DialogTurnResult> RestaurantStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
private async Task<DialogTurnResult> RestaurantStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{


if (stepContext.Values["companyStatus"].ToString().ToLower() == "kunde" || stepContext.Values["companyStatus"].ToString().ToLower() == "extern")
{
if (companyName == " ")
Expand All @@ -261,6 +288,26 @@ private static async Task<DialogTurnResult> RestaurantStepAsync(WaterfallStepCon
stepContext.Values["companyName"] = (string)stepContext.Result;
}

if (string.IsNullOrEmpty(this.planDay.Restaurant2))
{
stepContext.Values["restaurant"] = this.planDay.Restaurant1;
return await stepContext.NextAsync(null, cancellationToken);
}
else
{

return await stepContext.PromptAsync(
nameof(ChoicePrompt),
new PromptOptions
{
Prompt = MessageFactory.Text(nextOrderDialogRestaurantPrompt),
Choices = GetChoice("restaurant", plan),
Style = ListStyle.HeroCard,
}, cancellationToken);

}

/*
if (string.IsNullOrEmpty(plan.Planday[indexer].Restaurant2))
{
stepContext.Values["restaurant"] = plan.Planday[indexer].Restaurant1;
Expand All @@ -279,6 +326,7 @@ private static async Task<DialogTurnResult> RestaurantStepAsync(WaterfallStepCon
}, cancellationToken);
}
*/
}

/// <summary>
Expand Down Expand Up @@ -577,11 +625,6 @@ private async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepC

}

/// <summary>
///
/// </summary>
/// <param name="order"></param>

/// <summary>
/// Gets the chioses corresponding to the identifier you sepcify
/// </summary>
Expand All @@ -590,18 +633,18 @@ private async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepC
/// <returns>Returnds the specified choises</returns>
private static IList<Choice> GetChoice(string identifier, Plan plan)
{
List<string> choise = new List<string>();
List<string> choice = new List<string>();
var day = plan.Planday[dayId];
if (identifier == "restaurant")
{
if (day.Restaurant1 != null)
{
choise.Add(day.Restaurant1);
choice.Add(day.Restaurant1);
}

if (day.Restaurant2 != null)
{
choise.Add(day.Restaurant2);
choice.Add(day.Restaurant2);
}
}
else if (identifier == "food1")
Expand All @@ -610,9 +653,10 @@ private static IList<Choice> GetChoice(string identifier, Plan plan)
{
meal1List.Add(food.Name);
}

foreach (var food in day.Meal1)
{
choise.Add(food.Name + " " + food.Price + "€");
choice.Add(food.Name + " " + food.Price + "€");
meal1ListwithMoney.Add(food.Name + " " + food.Price + "€");
}
}
Expand All @@ -622,14 +666,15 @@ private static IList<Choice> GetChoice(string identifier, Plan plan)
{
meal2List.Add(food.Name);
}

foreach (var food in day.Meal2)
{
choise.Add(food.Name + " " + food.Price + "€");
choice.Add(food.Name + " " + food.Price + "€");
meal2ListWithMoney.Add(food.Name + " " + food.Price + "€");
}
}

return ChoiceFactory.ToChoices(choise);
return ChoiceFactory.ToChoices(choice);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion PlanB.Butler.Bot/Dialogs/OrderDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public OrderDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryClie
this.AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
this.AddDialog(new TextPrompt(nameof(TextPrompt)));
this.AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
this.AddDialog(new NextOrder(config, telemetryClient));
this.AddDialog(new NextOrder(config, telemetryClient, this.clientFactory));

// The initial child Dialog to run.
this.InitialDialogId = nameof(WaterfallDialog);
Expand Down
20 changes: 17 additions & 3 deletions PlanB.Butler.Bot/Dialogs/OrderForOtherDayDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Resources;
using System.Threading;
Expand All @@ -26,6 +27,11 @@ namespace PlanB.Butler.Bot.Dialogs
/// <seealso cref="Microsoft.Bot.Builder.Dialogs.ComponentDialog" />
public class OrderForOtherDayDialog : ComponentDialog
{
/// <summary>
/// The client factory.
/// </summary>
private readonly IHttpClientFactory clientFactory;

private static Plan plan = new Plan();
private static int valueDay;
private const double grand = 3.30;
Expand Down Expand Up @@ -65,12 +71,19 @@ public class OrderForOtherDayDialog : ComponentDialog
private static string nextOrderDialogSaveOrder = string.Empty;

private IBotTelemetryClient telemetryClient;

/// <summary>
/// The bot configuration.
/// </summary>
private readonly IOptions<BotConfig> botConfig;

public OrderForOtherDayDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient)
/// <summary>
/// Initializes a new instance of the <see cref="OrderForOtherDayDialog"/> class.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="botTelemetryClient">The telemetry client.</param>
/// <param name="httpClientFactory">The HTTP client factory.</param>
public OrderForOtherDayDialog(IOptions<BotConfig> config, IBotTelemetryClient botTelemetryClient, IHttpClientFactory httpClientFactory)
: base(nameof(OrderForOtherDayDialog))
{
ResourceManager rm = new ResourceManager("PlanB.Butler.Bot.Dictionary.Dialogs", Assembly.GetExecutingAssembly());
Expand All @@ -85,8 +98,9 @@ public OrderForOtherDayDialog(IOptions<BotConfig> config, IBotTelemetryClient te
otherDayDialogOrder = rm.GetString("OtherDayDialog_Order");
nextOrderDialogSaveOrder = rm.GetString("NextOrderDialog_SaveOrder");

this.telemetryClient = telemetryClient;
this.telemetryClient = botTelemetryClient;
this.botConfig = config;
this.clientFactory = httpClientFactory;

// This array defines how the Waterfall will execute.
var waterfallSteps = new WaterfallStep[]
Expand All @@ -103,7 +117,7 @@ public OrderForOtherDayDialog(IOptions<BotConfig> config, IBotTelemetryClient te
this.AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
this.AddDialog(new TextPrompt(nameof(TextPrompt)));
this.AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
this.AddDialog(new NextOrder(config, telemetryClient));
this.AddDialog(new NextOrder(config, this.telemetryClient, this.clientFactory));

// The initial child Dialog to run.
this.InitialDialogId = nameof(WaterfallDialog);
Expand Down
8 changes: 4 additions & 4 deletions PlanB.Butler.Bot/Dialogs/OverviewDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public OverviewDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryC

choices = new string[] { overviewDialogOrderFood, overviewDialogOtherDay, overviewDialogDeleteOrder, overviewDialogShowDepts, overviewDialogDaysOrder };
OrderDialog orderDialog = new OrderDialog(this.botConfig, telemetryClient, this.clientFactory);
NextOrder nextorderDialog = new NextOrder(this.botConfig, telemetryClient);
NextOrder nextorderDialog = new NextOrder(this.botConfig, telemetryClient, this.clientFactory);
PlanDialog planDialog = new PlanDialog(this.botConfig, telemetryClient);
CreditDialog creditDialog = new CreditDialog(this.botConfig, telemetryClient);
OrderForOtherDayDialog orderForAnotherDay = new OrderForOtherDayDialog(this.botConfig, telemetryClient);
OrderForOtherDayDialog orderForAnotherDay = new OrderForOtherDayDialog(this.botConfig, telemetryClient, this.clientFactory);
DeleteOrderDialog deleteOrderDialog = new DeleteOrderDialog(this.botConfig, telemetryClient, this.clientFactory);
List<ComponentDialog> dialogsList = new List<ComponentDialog>();
DailyCreditDialog dailyCreditDialog = new DailyCreditDialog(this.botConfig, telemetryClient);
Expand Down Expand Up @@ -127,9 +127,9 @@ public OverviewDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryC
this.AddDialog(new OrderDialog(this.botConfig, telemetryClient, this.clientFactory));
this.AddDialog(new CreditDialog(this.botConfig, telemetryClient));
this.AddDialog(new PlanDialog(this.botConfig, telemetryClient));
this.AddDialog(new OrderForOtherDayDialog(this.botConfig, telemetryClient));
this.AddDialog(new OrderForOtherDayDialog(this.botConfig, telemetryClient, this.clientFactory));
this.AddDialog(new DeleteOrderDialog(this.botConfig, telemetryClient, this.clientFactory));
this.AddDialog(new NextOrder(this.botConfig, telemetryClient));
this.AddDialog(new NextOrder(this.botConfig, telemetryClient, this.clientFactory));
this.AddDialog(new DailyCreditDialog(this.botConfig, telemetryClient));
this.AddDialog(new ExcellDialog(this.botConfig, telemetryClient));
this.AddDialog(new TextPrompt(nameof(TextPrompt)));
Expand Down
36 changes: 36 additions & 0 deletions PlanB.Butler.Bot/Models/OrderRelationship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PlanB.Butler.Bot.Models
{
/// <summary>
/// OrderRelationship.
/// </summary>
public enum OrderRelationship
{
/// <summary>
/// External.
/// </summary>
External = 1,

/// <summary>
/// Internal.
/// </summary>
Internal = 2,

/// <summary>y
/// Client.
/// </summary>
Client = 3,

/// <summary>
/// Intership.
/// </summary>
Intership = 4,
}
}

0 comments on commit df946d6

Please sign in to comment.