Exemplo n.º 1
0
        public async Task <IActionResult> UpdateBikeStatus([FromRoute] Guid bikeId, [FromBody] Bike bikePatch)
        {
            var bike = await _bikeStorage.RetrieveBikeAsync(bikeId);

            bike.UpdateLocation(bikePatch.Latitude, bikePatch.Longitude);
            bike.BatteryPercentage = bikePatch.BatteryPercentage;
            if (bike.State == BikeState.Active && bike.CurrentTripId.HasValue)
            {
                var trip = await _tripStorage.RetrieveTripAsync(bike.CurrentTripId.Value);

                trip.UpdateLocation(bike.Latitude, bike.Longitude);
                await _tripStorage.UpdateTripAsync(trip);
            }
            await _bikeStorage.UpdateBikeAsync(bike);

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> StartTrip([FromBody] Trip trip)
        {
            if (trip.BikeId == null || trip.StartLatitude == null || trip.StartLongitude == null)
            {
                return(BadRequest());
            }
            var bike = await _bikeStorage.RetrieveBikeAsync(Guid.Parse(trip.BikeId));

            if (bike == null)
            {
                return(NotFound(new { message = "Could not find a bike for the given id" }));
            }
            if (bike.State == BikeState.Active)
            {
                return(StatusCode((int)HttpStatusCode.PreconditionFailed, new { message = "The associated bike is already on a trip." }));
            }
            var user = await _userStorage.RetrieveUserAsync(trip.UserId);

            if (user == null)
            {
                return(NotFound(new { message = "Could not find a user for the given id" }));
            }
            trip.EndLatitude  = trip.StartLatitude;
            trip.EndLongitude = trip.StartLongitude;
            trip.TripId       = Guid.NewGuid();
            trip.TripMiles    = 0;
            trip.StartTime    = DateTimeOffset.UtcNow;

            bike.CurrentTripId = trip.TripId;
            bike.State         = BikeState.Active;
            bike.UpdateLocation(trip.StartLatitude.Value, trip.StartLongitude.Value);
            await _tripStorage.InsertTripAsync(trip);

            await _bikeStorage.UpdateBikeAsync(bike);

            return(Ok(trip.TripId));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetStatus([FromRoute] Guid bikeId)
        {
            var bike = await _bikeStorage.RetrieveBikeAsync(bikeId);

            return(Ok(bike.GetStatus()));
        }