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); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!long.TryParse(Parameter, out long id)) { throw new ArgumentException($"Can't perfom {Button} command, id parameter is not a number - {Parameter}"); } var newMainMetric = _metricRepository.GetById(id) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no metric with id {id}"); var oldMainMetric = _metricRepository.GetMainByExternalId(message.ChatId); if (oldMainMetric != null && oldMainMetric.Id != newMainMetric.Id) { oldMainMetric.UpdateMain(false); _metricRepository.Update(oldMainMetric); newMainMetric.UpdateMain(true); _metricRepository.Update(newMainMetric); } await _botClient.SendTextMessageAsync(message, MessageCode.Done); }
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)); }