public async Task <IActionResult> UpdateFlight(Guid flightId, [FromBody] FlightDetailForUpdateDto flightToUpdate)
        {
            var flight = await _repo.FlightDetailS.Where(x => x.Id.Equals(flightId)).SingleOrDefaultAsync();

            if (flight == null)
            {
                return(NotFound("Flight Id Doesnot Exist"));
            }


            var oldPrice = flight.Price;
            var newPrice = flightToUpdate.Price;

            if (oldPrice == newPrice)
            {
                return(BadRequest("The old price and new price is same"));
            }


            _mapper.Map(flightToUpdate, flight);
            _repo.FlightDetailS.Update(flight);
            await _repo.SaveChangesAsync();


            NotificationMessageDto notificationMessageDto = new NotificationMessageDto()
            {
                FlightCode  = flight.FlightCode,
                WebhookType = "PriceChange",
                OldPrice    = oldPrice,
                NewPrice    = newPrice
            };

            _messageBusClient.SendMessage(notificationMessageDto);
            return(NoContent());
        }
Example #2
0
        public async Task <List <Tour> > Search(SearchPattern searchPattern)
        {
            var request = new ToursRequest(Guid.NewGuid())
            {
                SearchPattern = searchPattern
            };

            // делаем так для того чтобы прервать ожидание, если все провайдеры ответили до истечения таймаута
            _providerNotResponded.TryAdd(request.RequestId, _messageBusClient.GetSubscriberCount <ToursRequest>());

            //отправляем поисковый запрос
            _messageBusClient.SendMessage(request);

            // ожидаем ответа от всех провайдеров, если ответили все до истечения таймаута ожидание будет отменено
            try
            {
                await Task.Delay(_options.Value.RequestTimeout, _delayTokenSource.Token);
            }
            catch (TaskCanceledException)
            {
            }

            List <Tour> result = new List <Tour>();

            _results.TryRemove(request.RequestId, out result);

            return(HandleResults(result, (ToursOrderEnum)searchPattern.ToursOrder));
        }
        private async Task <T> DoRequest <T>(Request request)
        {
            // делаем так для того чтобы прервать ожидание, если все провайдеры ответили до истечения таймаута
            _providerNotResponded.TryAdd(request.RequestId, _messageBusClient.GetSubscriberCount(request.GetType()));
            _messageBusClient.SendMessage(request);
            await Wait();

            return(GetResult <T>(request.RequestId));
        }
        public IActionResult UpdateFlightDetail(string flightCode, FlightDetailUpdateDto flightToUpdate)
        {
            var flight = _context.FlightDetails.FirstOrDefault(f => f.FlightCode == flightCode);

            if (flight == null)
            {
                return(NotFound());
            }

            var oldPrice = flight.PricePerSeat;

            _mapper.Map(flightToUpdate, flight);

            try
            {
                _context.SaveChanges();

                if (oldPrice != flight.PricePerSeat)
                {
                    Console.WriteLine("Price changed - Place message on bus");

                    var message = new NotificationMessageDto
                    {
                        WebhookType     = "pricechange",
                        OldPricePerSeat = oldPrice,
                        NewPricePerSeat = flight.PricePerSeat,
                        FlightCode      = flight.FlightCode
                    };

                    _messageBus.SendMessage(message);
                }
                else
                {
                    Console.WriteLine("No Price change");
                }

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(NoContent());
        }
        public async Task <ActionResult> UpdateFlightDetail(int id, FlightDetailUpdateDto flightDetailUpdateDto)
        {
            var flight = await _context.FlightDetails.FirstOrDefaultAsync(f => f.Id == id);

            if (flight is null)
            {
                return(await Task.FromResult(NotFound()));
            }

            decimal oldPrice = flight.Price;

            _mapper.Map(flightDetailUpdateDto, flight);

            try
            {
                await _context.SaveChangesAsync();

                if (oldPrice != flight.Price)
                {
                    Console.WriteLine("Price changed - Place message on bus");
                    var message = new NotificationMessageDto
                    {
                        WebhookType = "pricechange",
                        OldPrice    = oldPrice,
                        NewPrice    = flight.Price,
                        FlightCode  = flight.FlightCode
                    };
                    _messageBusClient.SendMessage(message);
                }
                else
                {
                    Console.WriteLine("No price change");
                }

                return(await Task.FromResult(NoContent()));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Example #6
0
        public ActionResult UpdateFlightDetail(int id, FlightDetailUpdateDto flightDetailUpdateDto)
        {
            var flight = _context.FlightDetails.FirstOrDefault(f => f.Id == id);

            if (flight == null)
            {
                return(NotFound());
            }

            decimal oldPrice = flight.Price;

            _mapper.Map(flightDetailUpdateDto, flight);

            try
            {
                _context.SaveChanges();

                if (oldPrice != flight.Price)
                {
                    Console.WriteLine("Price changed - Place message on bus");

                    var message = new NotificationMessageDto
                    {
                        WebhookType = "Price Change",
                        OldPrice    = oldPrice,
                        // TODO: TYPO
                        NewPrice   = flight.Price,
                        FlightCode = flight.FlightCode,
                    };
                    _messageBus.SendMessage(message);
                }
                else
                {
                    Console.WriteLine("No price change");
                }
                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <ActionResult> UpdateFlightDetails(string flightCode, [FromBody] FlightDetailUpdateDto flightDetailsUpdateDto)
        {
            var flight = await _context.FlightDetails
                         .FirstOrDefaultAsync(
                e => e.FlightCode == flightCode);

            if (flight == null)
            {
                return(NoContent());
            }

            decimal oldPrice = flight.Price;

            _mapper.Map(flightDetailsUpdateDto, flight);

            try
            {
                await _context.SaveChangesAsync();

                if (oldPrice != flight.Price)
                {
                    Console.WriteLine("Price changed - PLace message on bus");
                    var message = new Dtos.NotificationMessageDto {
                        Id          = flight.Id.ToString(),
                        FlightCode  = flight.FlightCode,
                        NewPrice    = flight.Price,
                        OldPrice    = oldPrice,
                        WebhookType = "pricechange"
                    };

                    _messageBus.SendMessage(message);
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(NoContent());
        }
Example #8
0
        private async Task ReceiveHotelsRequest(HotelsRequest request)
        {
            var hotels = await _dictionariesRepository.GetAllHotels();

            _messageBusClient.SendMessage(new HotelsResponse(request.RequestId, nameof(OtherSearchService))
            {
                Data = hotels.Select(hotel => new Hotel
                {
                    Id           = hotel.Id,
                    Name         = hotel.Name,
                    CityName     = hotel.City.Name,
                    Address      = hotel.Address,
                    BuildingYear = hotel.BuildingYear
                }).ToList()
            });
        }