Esempio n. 1
0
        public ActionResult UpdateExperimentForPlant([FromRoute] Guid plantId,
                                                     [FromRoute] Guid experimentId, [FromBody] ExperimentForUpdateDto experiment)
        {
            if (!_experimentRepository.PlantExists(plantId))
            {
                NotFound();
            }

            var experimentFromRepo = _experimentRepository.GetExperiment(plantId, experimentId);

            if (experimentFromRepo == null)
            {
                var experimentToAdd = _mapper.Map <Entities.Experiment>(experiment);
                experimentToAdd.Id = experimentId;

                _experimentRepository.AddExperiment(plantId, experimentToAdd);
                _experimentRepository.Save();

                return(CreatedAtRoute("GetExperiment",
                                      new { plantId, experimentId },
                                      experimentToAdd));
            }

            //entity -> ExperimetForUpdateDto
            //ExperimetForUpdateDto -> experiment
            //experiment -> entity
            _mapper.Map(experiment, experimentFromRepo);

            _experimentRepository.UpdateExperiment(experimentFromRepo);
            _experimentRepository.Save();

            return(NoContent());
        }
        private void HandleUpdateExperiment(UpdateExperiment updateExperiment)
        {
            Result <Experiment> updatedExperimentResult = _repository.UpdateExperiment(updateExperiment);

            if (updatedExperimentResult.IsFailure)
            {
                ExperimentUpdateFailed failedExperimentUpdate =
                    new ExperimentUpdateFailed(
                        updatedExperimentResult.Error,
                        updateExperiment.LoggedInUserId,
                        updateExperiment.SagaId
                        );
                _kafkaProducer.Produce(failedExperimentUpdate, EXPERIMENT_TOPIC);
                return;
            }

            ExperimentUpdated updatedExperiment = new ExperimentUpdated(
                updatedExperimentResult.Value.Id,
                updatedExperimentResult.Value.Creator,
                updatedExperimentResult.Value.Name,
                updatedExperimentResult.Value.CreationDate,
                updateExperiment.LoggedInUserId,
                updateExperiment.SagaId
                );

            _kafkaProducer.Produce(updatedExperiment, EXPERIMENT_TOPIC);
        }