Пример #1
0
        public async Task Visit(TextUpdateMessage message)
        {
            var source = GetSource(message);

            source.UpdateLastActionDate();

            // Need to check the user's state to understand how to process text
            if (source.State == StateType.HasMetric)
            {
                // Processing the value for the main metric
                await _valueHandlerService.HandleAsync(message);

                _sourceRepository.Update(source);
                return;
            }
            else if (source.State == StateType.NeedAddMetric)
            {
                // Create new metric with name
                var newMetric = new Metric(message.Content);
                newMetric.UpdateSource(source);
                var mainMetric = _metricRepository.GetMainBySourceId(source.Id);
                newMetric.UpdateMain(mainMetric == null);
                _metricRepository.Add(newMetric);
                source.UpdateState(StateType.NeedAddUnit);
            }
            else if (source.State == StateType.NeedAddUnit)
            {
                // Adding metric unit for existing metric
                var editedMetric = _metricRepository.GetNotCreatedMetric(source.Id)
                                   ?? throw new ArgumentException($"Can't add main unit, there is no edited metric for source {message.ChatId}");

                editedMetric.UpdateUnit(message.Content);
                editedMetric.SetCreated();
                _metricRepository.Update(editedMetric);
                source.UpdateState(StateType.HasMetric);
            }

            _sourceRepository.Update(source);
            await _botClient.SendTextMessageAsync(message, GetMessageCode(source.State));
        }