Example #1
0
        public async Task AddMetricModifier(Guid animalId, Guid metricId, MetricModifier modifier)
        {
            Animal animal = await GetAnimal(animalId);

            if (animal == null)
            {
                throw new ArgumentException($"Invalid animalId {animalId}", nameof(animalId));
            }

            Metric metric = animal.Metrics.FirstOrDefault(m => m.Id == metricId);

            if (metric == null)
            {
                throw new ArgumentException($"Invalid metricId {metricId}", nameof(metricId));
            }

            if (modifier.ActionDate <= metric.CreationDate)
            {
                throw new ArgumentException($"Invalid date for modifier's action date {modifier.ActionDate}. It can't be before the creation of the metric {metric.CreationDate}", nameof(modifier.ActionDate));
            }

            if (metric.Rate * modifier.ModificationEffect > 0)
            {
                string sign = metric.Rate > 0 ? "negative" : "positive";
                throw new ArgumentException($"Modifier is invalid. Modifier has to be {sign}");
            }

            await _repository.AddMetricModifier(animalId, metricId, modifier);
        }
        public async Task AddMetricModifier(Guid animalId, Guid metricId, MetricModifier modifier)
        {
            Metric metric = await GetMetric(animalId, metricId);

            if (metric.Modifiers == null)
            {
                metric.Modifiers = new List <MetricModifier>();
            }
            List <MetricModifier> modifiers = metric.Modifiers.ToList();

            modifiers.Add(modifier);
            metric.Modifiers = modifiers;

            await _connection.GetDatabase().HashSetAsync($"Metrics:{animalId}", metricId.ToString(),
                                                         JsonSerializer.Serialize(metric));
        }
 public async Task AddMetricModifier([FromRoute] Guid metricId, [FromRoute] Guid animalId, [FromBody] MetricModifier modifier)
 {
     await _animalService.AddMetricModifier(animalId, metricId, modifier);
 }