Esempio n. 1
0
        /// <summary>
        /// Add caller as eater to currently open order and publish updated order to slack.
        /// Update current entry, if already added.
        /// </summary>
        private async Task HandleEatAsCallerAction(SlashCommand command)
        {
            try
            {
                // Example: ['eat', '12.50', 'pizza']
                var args = command.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();

                if (args.Count < 2)
                {
                    throw new FormatException();
                }

                // Cost
                args[1] = args[1].Replace(',', '.');
                var cost = decimal.Parse(args[1], CultureInfo.InvariantCulture);

                // Optional item description
                string item = null !;
                if (args.Count >= 3)
                {
                    // Join all remaining items because spaces may have been used in item description
                    item = string.Join(' ', args.Skip(2).ToArray());
                }

                var updatedOrder = await foodService.AddEater(eater : new FoodUser(command.UserId, command.UserName), cost, item);
                await SendCommandResponse(command, SlackFormatter.BuildOrderMessage(updatedOrder));
            }
            catch (FormatException)
            {
                await SendError(command.ResponseUrl,
                                new BadRequestException($"Incorrect format, expected: /{CommandTexts.Eat} <cost> [description]"));
            }
            catch (Exception x)
            {
                await SendError(command.ResponseUrl, x);
            }
        }