private static Task <ValidateResult> ValidateHoursPerDay(TimeRegistrationModel model, object state) { ValidateResult result = new ValidateResult() { IsValid = true }; // if the input is not a string, it's not valid in any case if (!(state is string)) { result.Value = false; result.Feedback = "That doesn't seem to be a correct input, please retry. I need 5 numbers, separated by spaces."; return(Task.FromResult(result)); } string inputToValidate = (string)state; // split the input string by space and ensure string[] hours = inputToValidate.Trim().Split(' '); if (!hours.All(h => { if (double.TryParse(h, out double parseResult)) { return(true); } else { return(false); } })) { result.Value = false; result.Feedback = "Seems you entered a value that's not a number, please use only numbers."; Task.FromResult(result); } if (hours.Length != 5) { result.IsValid = false; result.Feedback = "That input was not correct. You need to specify five values, separated by spaces."; Task.FromResult(result); } result.Value = inputToValidate; return(Task.FromResult(result)); }
private async Task <TimeRegistrationModel> TimeRegistrationCompleted(IBotContext context, TimeRegistrationModel model) { var message = "Booking your hours in Exact, just a sec..."; await context.PostAsync(message); ExactOnlineConnector connector = ExactOnlineHelper.GetConnector(); TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); Guid projectId = String.IsNullOrEmpty(model.Project) || model.Project == "none" ? Guid.Empty : new Guid(model.Project); Guid customerId = String.IsNullOrEmpty(model.Customer) ? Guid.Empty : new Guid(model.Customer); Guid hourTypeId = String.IsNullOrEmpty(model.HourType) ? Guid.Empty : new Guid(model.HourType); try { // the user will have booked time for either this week or for a specific date if (!model.ThisWeek) { timeConnector.BookHours(connector.EmployeeId, customerId, hourTypeId, projectId, model.Date, model.Amount, connector); } else { // if the hours were booked for the entire will, there will be 5 numbers in the string that need to be split // out and entered for each day of the week individually int dayOfWeek = DateTimeUtils.GetISODayOfWeek(DateTime.Now); DateTime currentDay = DateTime.Now.AddDays((dayOfWeek - 1) * -1); string[] hours = model.AmountPerDay.Trim().Split(' '); for (int i = 0; i < 5; i++) { double amount = Double.Parse(hours[i]); if (amount > 0) { timeConnector.BookHours(connector.EmployeeId, customerId, hourTypeId, projectId, currentDay, amount, connector); } currentDay = currentDay.AddDays(1); } } } catch (RequestFailedException ex) { await context.PostAsync($"Hmm, that didn't work. The request failed, it returned the following:"); await context.PostAsync($"\"{ ex.Message}\""); await context.PostAsync($"Sorry about that. Please try again or notify my maker."); return(null); } await context.PostAsync("All set! Anything else I can do for you?"); return(model); }