Beispiel #1
0
        public async Task RestaurantRatingChanged(int idRestaurant, decimal newRating)
        {
            var newRatingEvt = new RatingChangedEvent
            {
                idRestaurant = idRestaurant
            };

            using (var scope = _serviceScopeFactory.CreateScope())
            {
                IReviewService reviewService = scope.ServiceProvider.GetRequiredService <IReviewService>();

                var restaurantReviews = await reviewService.GetRestaurantReviews(idRestaurant);

                newRatingEvt.averageRating = restaurantReviews.Sum(rw => rw.Rating) / restaurantReviews.Count();
            }

            IBasicProperties props = _channel.CreateBasicProperties();

            props.Type = nameof(RatingChangedEvent);

            var body = MessageSerializationHelper.SerializeObjectToBin(newRatingEvt);

            _channel.BasicPublish(exchange: string.Empty,
                                  routingKey: ApplicationEvents.RestaurantQueue,
                                  basicProperties: props,
                                  body: body);
        }
 private async Task HandleRatingEvent(RatingChangedEvent ratingChangedEvent)
 {
     using (var scope = _serviceScopeFactory.CreateScope())
     {
         IRestaurantService restaurantService = scope.ServiceProvider.GetRequiredService <IRestaurantService>();
         await restaurantService.UpdateRestaurantAverageRating(ratingChangedEvent.idRestaurant, ratingChangedEvent.averageRating);
     }
 }