public async Task <TripDocument> CreateTrip(TripRequest tripRequest, RiderClaim riderClaims) { var tripContainer = _cosmosClient.GetContainer(Constants.ScootersDataBaseId, Constants.TripCollection); var trip = new TripDocument { Id = Guid.NewGuid().ToString(), DocumentType = Constants.TripDocumentType, Rider = new TripRider { UserId = riderClaims.UserId, FirstName = riderClaims.FirstName, LastName = riderClaims.LastName, Email = riderClaims.EmailAddress }, Source = tripRequest.Source, RequestDateTime = tripRequest.RequestDateTime, Duration = 0, ScooterCode = tripRequest.ScooterCode, TripIsEnded = false, Destination = null, StartDateTime = null, EndDateTime = null }; await tripContainer.UpsertItemAsync(trip, new PartitionKey(trip.ScooterCode)); return(trip); }
public static async Task TripOrchestrator( [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req, [DurableClient] IDurableOrchestrationClient starter, ILogger log) { // Function input comes from the request content. string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); TripDocument tripDocument = JsonConvert.DeserializeObject <TripDocument>(requestBody); var instanceId = tripDocument.ScooterCode; try { var tripStatus = await starter.GetStatusAsync(instanceId); string runningStatus = tripStatus == null ? "NULL" : tripStatus.RuntimeStatus.ToString(); log.LogInformation($"Instance running status: '{runningStatus}'."); if (tripStatus == null || tripStatus.RuntimeStatus != OrchestrationRuntimeStatus.Running || tripStatus.RuntimeStatus != OrchestrationRuntimeStatus.Pending) { await starter.StartNewAsync("StartTripManager", instanceId, tripDocument); log.LogInformation($"Started orchestration with ID = '{instanceId}'."); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public static TripDocument CallForUnlockAndStart([ActivityTrigger] TripDocument tripDocument, ILogger log) { log.LogInformation($"The tripDocument with code {tripDocument.ScooterCode} is waiting for unlock"); return(tripDocument); }
public static async Task <object> StartTripManager( [OrchestrationTrigger] IDurableOrchestrationContext context) { TripDocument tripDocument = context.GetInput <TripDocument>(); string tripAcceptCode; // Replace "hello" with the name of your Durable Activity Function. tripDocument = await context.CallActivityAsync <TripDocument>("CallForUnlockAndStart", tripDocument); using (var cts = new CancellationTokenSource()) { var timeoutAt = context.CurrentUtcDateTime.AddSeconds(30); var timeoutTask = context.CreateTimer(timeoutAt, cts.Token); var acknowledgeTask = context.WaitForExternalEvent <string>("RiderUnlockedAndStarted"); var winner = await Task.WhenAny(acknowledgeTask, timeoutTask); if (winner == acknowledgeTask) { tripAcceptCode = acknowledgeTask.Result; cts.Cancel(); // we should cancel the timeout task } else { tripAcceptCode = "Timed Out"; } } return(new { Trip = tripDocument, Code = tripAcceptCode }); }
/// <summary> /// Gets the TripDocument from a userId and date /// </summary> /// <param name="userId">The userId to look for</param> /// <param name="date">The date to look for</param> /// <returns>A TripDocument containing the trips for the specific user and date</returns> public TripDocument GetTripsOfUserAndDate(string userId, string date) { TripsCollection tripCol = _trips.Find(collection => collection.UserId == userId).FirstOrDefault(); TripDocument tripDoc = null; if (tripCol != null) { tripDoc = tripCol.TripDocuments.FirstOrDefault(x => x.DateId == date); } return(tripDoc); }
public async Task <TripDocument> StartTrip([ActivityTrigger] TripDocument trip, ILogger log) { // read current Trip from database var tripDocument = await _tripService.GetTrip(trip.Id, trip.ScooterCode); tripDocument.StartDateTime = DateTime.UtcNow; // update trip await _tripService.UpdateTrip(tripDocument); log.LogInformation($"Trip {tripDocument.Id} with Scooter {tripDocument.ScooterCode} started at {tripDocument.StartDateTime}."); return(tripDocument); }
public void ReceiveIPromiseDocument(TripDocument iPromiseDoc) { var token = _apiUserRepository.GetToken(); _tripRepository.ReceiveTripDocument(new MpEventParticipantDocument { DocumentId = iPromiseDoc.DocumentId, EventParticipantId = iPromiseDoc.EventParticipantId, EventParticipantDocumentId = iPromiseDoc.EventParticipantDocumentId, EventTitle = iPromiseDoc.TripName, Notes = iPromiseDoc.Notes, Received = iPromiseDoc.Received }, token); }
public IHttpActionResult ReceiveIPromiseDocument([FromBody] TripDocument iPromiseDoc) { return(Authorized(token => { try { _tripService.ReceiveIPromiseDocument(iPromiseDoc); return Ok(); } catch (Exception e) { var apiError = new ApiErrorDto("Save I Promise Document Failed", e); throw new HttpResponseException(apiError.HttpResponseMessage); } })); }
public async Task UpdateTrip(TripDocument tripDocument) { var tripContainer = _cosmosClient.GetContainer(Constants.ScootersDataBaseId, Constants.TripCollection); await tripContainer.UpsertItemAsync(tripDocument, new PartitionKey(tripDocument.ScooterCode)); }