public static async Task <IActionResult> GetSong( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { var queryUri = UriFactory.CreateDocumentCollectionUri("RogueSound", "Sessions"); var feedOptions = new FeedOptions { PartitionKey = new PartitionKey(0) }; var partitionOptions = new RequestOptions { PartitionKey = new PartitionKey(0) }; var todayDate = DateTime.SpecifyKind(DateTime.Today, DateTimeKind.Utc); log.LogInformation($"Querying session for {todayDate}"); var currentSessionQuery = client.CreateDocumentQuery <RoomSessionModel>(queryUri, feedOptions) .Where(x => x.SessionDate == todayDate) .OrderBy(x => x.CreatedAt) .Take(1) .AsDocumentQuery(); var sessionsReturned = new List <RoomSessionModel>(); while (currentSessionQuery.HasMoreResults) { sessionsReturned.AddRange(await currentSessionQuery.ExecuteNextAsync <RoomSessionModel>()); } var currentSession = sessionsReturned.FirstOrDefault(); log.LogInformation($"Returned {sessionsReturned.Count} sessions"); if (currentSession == null) { log.LogInformation($"Null current session, creating new"); currentSession = new RoomSessionModel { id = Guid.NewGuid().ToString(), RoomId = 0, SessionDate = todayDate, Songs = Enumerable.Empty <SongQueueModel>() }; await client.CreateDocumentAsync(queryUri, currentSession, partitionOptions); } log.LogInformation($"Retrieved {currentSession.Songs.Count()} songs"); currentSession.Songs = currentSession.Songs.OrderByDescending(x => x.StartTime); return(new OkObjectResult(currentSession.ToResponseModel())); }
public static RoomSessionResponseModel ToResponseModel(this RoomSessionModel model) { var currentSong = model.Songs.FirstOrDefault(x => x.StartTime < DateTime.UtcNow); return(new RoomSessionResponseModel { RoomId = model.RoomId, SessionDate = model.SessionDate, Current = (currentSong != null && currentSong.EndTime < DateTime.UtcNow) ? null : currentSong.ToResponseModel(), Songs = model.Songs }); }