public async Task ReturnBike(Bike bike)
        {
            var location = await _locationService.GetCurrentLocation();

            if (location == null)
            {
                return;
            }

            if (bike.HasLock)
            {
                if (!await _lockService.CloseLock(bike))
                {
                    return;
                }
            }

            bike.ReservedUntil = null;
            bike.CurrentUser   = null;

            var stations = await _stationRepository.GetStationsFromCommunity(bike.CommunityId);

            var closeStation = stations.SingleOrDefault(station =>
                                                        station.Location.CalculateDistance(location, DistanceUnits.Kilometers) < 0.1);

            if (closeStation != null)
            {
                bike.StationId = closeStation.Id;

                await _context.RunTransactionAsync(transaction =>
                {
                    _stationRepository.Update(closeStation, nameof(Station.NumberOfBikes), FieldValue.Increment(1), transaction);
                    _bikeRepository.Update(bike, transaction);
                });

                return;
            }

            bike.Location = location;

            await _bikeRepository.Update(bike);
        }
Пример #2
0
 private Task TakeBreak(Bike bike) => _lockService.CloseLock(bike);