Ejemplo n.º 1
0
        /// <summary>
        /// Add some debt by caller to mentioned user. Order-less way to alter balance.
        /// </summary>
        private async Task HandleOweAction(SlashCommand command)
        {
            try
            {
                // Example: ['owe', '@user' '4.99']
                var args = command.Text.Split(' ');
                if (args.Length != 3)
                {
                    throw new FormatException();
                }

                var creditor = FoodUser.CreateFromString(args[1].Trim());
                var debtor   = new FoodUser(command.UserId, command.UserName);

                if (debtor.UniqueId == creditor.UniqueId)
                {
                    await SendError(command.ResponseUrl, new BadRequestException("You can't do it to yourself."));

                    return;
                }

                var creditValue = decimal.Parse(args[2].Replace(',', '.'));
                await foodService.OweCredit(debtor, creditor, creditValue);
            }
            catch (FormatException)
            {
                await SendError(command.ResponseUrl,
                                new BadRequestException($"Incorrect format, expected: `/food {CommandTexts.Owe} @<user> <cost>`"));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Forgive mentioned user's debt fully. Order-less way to alter balance.
        /// </summary>
        private async Task HandleForgiveAction(SlashCommand command)
        {
            try
            {
                // Example: ['forgive', '@user']
                var args = command.Text.Split(' ');
                if (args.Length != 2)
                {
                    throw new FormatException();
                }

                var debtor   = FoodUser.CreateFromString(args[1].Trim());
                var creditor = new FoodUser(command.UserId, command.UserName);

                if (debtor.UniqueId == creditor.UniqueId)
                {
                    await SendError(command.ResponseUrl, new BadRequestException("You can't do it to yourself."));

                    return;
                }

                await foodService.ResetCredit(debtor, creditor);
            }
            catch (FormatException)
            {
                await SendError(command.ResponseUrl,
                                new BadRequestException($"Incorrect format, expected: `/food {CommandTexts.Forgive} @<user>`"));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add someone else as eater to currently open order and publish updated order to slack.
        /// Update current entry, if already added.
        /// Can be used to host external guests.
        /// </summary>
        private async Task HandleEatAsOtherAction(SlashCommand command)
        {
            try
            {
                // Example: ['eat', 'batman', '12.50', 'pizza']
                var args = command.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();

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

                // Eater
                var eaterStr = args[1].Trim();
                var eater    = FoodUser.CreateFromString(eaterStr);

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

                // Optional item description
                string item = null !;
                if (args.Count >= 4)
                {
                    item = item = string.Join(' ', args.Skip(3).ToArray());
                }

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