public override async Task HandleAsync(CallbackQueryUpdateMessage message)
        {
            if (!long.TryParse(Parameter, out long metricId))
            {
                throw new ArgumentException($"Can't perfom {Button} command, id parameter is not a number - {Parameter}");
            }

            var source = _sourceRepository.GetByExternalId(message.ChatId)
                         ?? throw new ArgumentException($"Can't perfom {Button} command, there is no source with id {message.ChatId}");

            var metric = _metricRepository.GetById(metricId)
                         ?? throw new ArgumentException($"Can't perfom {Button} command, there is no metric with id {metricId}");

            metric.SetDeleted();
            _metricRepository.Update(metric);

            if (metric.IsMain)
            {
                // after removed the main metric, set any other main
                var hasMainMain = _metricRepository.SetMainMetricIfHasNo(source.Id);
                source.UpdateState(hasMainMain ? StateType.HasMetric : StateType.NeedAddMetric);
                _sourceRepository.Update(source);
            }

            await _botClient.SendTextMessageAsync(message, MessageCode.Done);
        }
Beispiel #2
0
        public async Task Visit(DocumentUpdateMessage message)
        {
            var source = GetSource(message);

            string jsonMimeType = "application/json";

            if (message.DocumentMimeType != jsonMimeType)
            {
                await _botClient.SendTextMessageAsync(message, MessageCode.ExpectedJsonFile);

                return;
            }

            try
            {
                var parser     = new MetricsJsonParser(_botClient);
                var newMetrics = await parser.ParseAsync(message.DocumentFileId);

                _metricRepository.UpdateMetricData(source, newMetrics);
                await _botClient.SendTextMessageAsync(message, MessageCode.Done);
            }
            catch
            {
                await _botClient.SendTextMessageAsync(message, MessageCode.JsonError);

                throw;
            }

            // If there is no main metric, set any other
            var hasMainMetric = _metricRepository.SetMainMetricIfHasNo(source.Id);

            source.UpdateState(hasMainMetric ? StateType.HasMetric : StateType.NeedAddMetric);
            source.UpdateLastActionDate();
            _sourceRepository.Update(source);
        }