Exemple #1
0
        public async Task Handle(AddOrUpdateSuggestionCommand command)
        {
            var entity = new Suggestion(command.Id, command.MovieId)
            {
                Affinity = command.Affinity
            };

            bool exists = await _suggestionsDataAccessObject.AnyAsync(d => d.Id == command.Id);

            if (exists)
            {
                await _suggestionRepository.UpdateAsync(entity);

                _logger.LogInformation("Suggestion {value} updated", command.Id);

                await _bus.Publish(command.CreateUpdatedEvent());
            }
            else
            {
                _logger.LogInformation("Suggestion {value} added", command.Id);

                await _suggestionRepository.AddAsync(entity);

                await _bus.Publish(command.CreateAddedEvent());
            }
        }
Exemple #2
0
        public async Task Handle(AddMovieCommand command)
        {
            if (await _dataAccessObject.AnyAsync(m => m.Id == command.Id))
            {
                _logger.LogWarning("Movie {title} already added", command.Title);

                await _bus.Publish(new MovieAddedEvent { MovieId = command.Id });

                return;
            }

            Message rawMessage = MessageContext.Current.Message;

            _logger.LogInformation("Add movie {title} requested by {user}", command.Title, _userAccessor.User?.Identity.Name);

            //using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            //{
            //    transaction.EnlistRebus();

            var movie = new Movie(command.Id, command.Title)
            {
                Year = command.Year
            };
            await _repository.AddAsync(movie);

            await _bus.Publish(new MovieAddedEvent { MovieId = command.Id });

            //    transaction.Complete();
            //}

            await _bus.Reply(command.Id);
        }
        public async Task Handle(AddVehicleCommand command)
        {
            if (await _dataAccessObject.AnyAsync(m => m.Id == command.Id))
            {
                _logger.LogWarning("Vehicle {id} already added", command.Id);

                await _bus.Publish(new VehicleAddedEvent { VehicleId = command.Id });

                return;
            }

            _logger.LogInformation("Add vehicle with {plate} requested by {user}", command.Plate, _userAccessor.User?.Identity.Name);

            //using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            //{
            //    transaction.EnlistRebus();

            var vehicle = new DomainModel.Vehicle(command.Id)
            {
                Plate    = command.Plate,
                Model    = command.Model,
                Brand    = command.Brand,
                Color    = command.Color,
                Customer = command.Customer,
                Year     = command.Year,
            };
            await _repository.AddAsync(vehicle);

            await _bus.Publish(new VehicleAddedEvent { VehicleId = command.Id });

            //    transaction.Complete();
            //}

            await _bus.Reply(command.Id);
        }
Exemple #4
0
        public async Task Handle(AddOrUpdateMovieCommand command)
        {
            // Validate title uniqueness
            if (await _movieDataAccessObject.AnyAsync(m => m.Title == command.OriginalTitle))
            {
                _logger.LogError("A movie with the title {title} already exists", command.OriginalTitle);

                await _bus.Publish(command.CreateValidationEvent(new ValidationResult(409, $"A movie with the title {command.OriginalTitle} already exists")));

                return;
            }

            var entity = new Movie(command.Id, command.OriginalCulture, command.OriginalTitle)
            {
                Year = command.Year
            };

            foreach (var pair in command.Translation)
            {
                entity.Translation.Add(pair.Key, pair.Value);
            }

            bool exists = await _movieDataAccessObject.AnyAsync(e => e.Id == command.Id);

            if (exists)
            {
                await _movieRepository.UpdateAsync(entity);

                _logger.LogInformation("Movie {value} updated", command.Id);

                await _bus.Publish(command.CreateUpdatedEvent());
            }
            else
            {
                _logger.LogInformation("Movie {value} added", command.Id);

                await _movieRepository.AddAsync(entity);

                await _bus.Publish(command.CreateAddedEvent());
            }
        }
        public async Task Handle(AddPartCommand command)
        {
            if (await _dataAccessObject.AnyAsync(m => m.Code == command.Code))
            {
                _logger.LogWarning("Part {code} already added", command.Code);

                await _bus.Publish(new PartAddedEvent { PartCode = command.Code });

                return;
            }

            _logger.LogInformation("Add part with {code} requested by {user}", command.Code, _userAccessor.User?.Identity.Name);

            var part = new DomainModel.Part(command.Code, command.Name);
            await _repository.AddAsync(part);

            await _bus.Publish(new PartAddedEvent { PartCode = command.Code });
        }
Exemple #6
0
        public async Task Handle(EvaluateSuggestionCommand message)
        {
            if (!await _dataAccessObject.AnyAsync(m => m.MovieId == message.MovieId))
            {
                _logger.LogInformation("Evaluating suggestion for {movieId}", message.MovieId);

                //if (Environment.TickCount % 2d == 0d)
                //{
                //    throw new Exception("Random error");
                //}

                await _repository.AddAsync(new Suggestion(message.MovieId)
                {
                    Affinity = (float)Math.Round(new Random(Environment.TickCount).NextDouble(), 2)
                });
            }

            await _bus.Publish(new SuggestionEvaluatedEvent { MovieId = message.MovieId });
        }
Exemple #7
0
        public async Task Handle(AddHistoryCommand command)
        {
            if (await _dataAccessObject.AnyAsync(m => m.Id == command.Id))
            {
                _logger.LogWarning("Part history {code} for {vehicleId} already added", command.PartCode, command.VehicleId);

                await _bus.Publish(new HistoryAddedEvent { PartCode = command.PartCode, VehicleId = command.VehicleId });

                return;
            }

            _logger.LogInformation("Add part history with {code} for {vehicleId} requested by {user}", command.PartCode, command.VehicleId, _userAccessor.User?.Identity.Name);

            var part = new DomainModel.History(command.Id, command.PartCode, command.VehicleId);
            await _repository.AddAsync(part);

            await _bus.Publish(new HistoryAddedEvent { PartCode = command.PartCode, VehicleId = command.VehicleId });

            await _bus.Reply(command.Id);
        }