public static async Task <IActionResult> StartTodaysWorkout(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "progress/program/start")] HttpRequest req,
            [CosmosDB(
                 databaseName: "UserProgress",
                 collectionName: "UserProgramDetails",
                 ConnectionStringSetting = "AzureWebJobsStorage")] DocumentClient client,
            ILogger log)
        {
            log.LogInformation("Starting today's program");

            // Get the active program for this user
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("UserProgress", "UserProgramDetails");
            IDocumentQuery <UserProgramDetail> query = client.CreateDocumentQuery <UserProgramDetail>(collectionUri)
                                                       .Where(p => p.Id.Equals(req.Query["id"]))
                                                       .Where(p => p.Username.Equals(req.Query["username"]))
                                                       .AsDocumentQuery();

            try
            {
                while (query.HasMoreResults)
                {
                    //Document d = query.ExecuteNextAsync().Result.FirstOrDefault();
                    foreach (Document doc in await query.ExecuteNextAsync())
                    {
                        UserProgramDetail userProgDetail = (dynamic)doc;

                        // Update the day number count
                        userProgDetail.DayNo++;
                        await client.ReplaceDocumentAsync(doc.SelfLink, userProgDetail);

                        // Return the details used to build todays workout
                        return(new OkObjectResult(userProgDetail));
                    }
                }
            }
            catch (Exception e)
            {
                return(new BadRequestResult());
            }

            return(new BadRequestObjectResult("Program detail wasn't able to update"));
        }
        public static async Task <IActionResult> InitializeUserProgram(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "progress/program")] HttpRequest req,
            [CosmosDB(
                 databaseName: "UserProgress",
                 collectionName: "UserPrograms",
                 ConnectionStringSetting = "AzureWebJobsStorage")] DocumentClient userProgsCient,
            [CosmosDB(
                 databaseName: "UserProgress",
                 collectionName: "UserProgramDetails",
                 ConnectionStringSetting = "AzureWebJobsStorage")]
            IAsyncCollector <UserProgramDetail> userProgDetsOut,
            ILogger log)
        {
            List <Document> output = new List <Document>();

            log.LogInformation("Initializa a new user program");

            // Get the program details
            string requestBody      = await new StreamReader(req.Body).ReadToEndAsync();
            var    inputProgDetails = JsonConvert.DeserializeObject <UserProgramDetailCreateModel>(requestBody);

            // Get the active program for this user
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("UserProgress", "UserPrograms");
            IDocumentQuery <UserProgram> query = userProgsCient.CreateDocumentQuery <UserProgram>(collectionUri)
                                                 .Where(p => p.Username.Equals(inputProgDetails.Username))
                                                 .Where(p => p.Status.Equals("active"))
                                                 .AsDocumentQuery();

            try
            {
                while (query.HasMoreResults)
                {
                    foreach (Document doc in await query.ExecuteNextAsync())
                    {
                        UserProgram userProgram = (dynamic)doc;

                        // If the program has already been started let the caller know and don't continue
                        foreach (var program in userProgram.Programs)
                        {
                            if (program.Id == inputProgDetails.ProgId)
                            {
                                return(new ConflictObjectResult("Program already started"));
                            }
                        }

                        // If the program hasn't started then you're here, update the active program list
                        userProgram.Programs.Add(new Program()
                        {
                            Id   = inputProgDetails.ProgId,
                            Name = inputProgDetails.ProgName
                        });
                        output.Add(await userProgsCient.ReplaceDocumentAsync(doc.SelfLink, userProgram));

                        // Map the create model to the database required model
                        var progDetail = new UserProgramDetail()
                        {
                            Username  = inputProgDetails.Username,
                            ProgId    = inputProgDetails.ProgId,
                            ProgName  = inputProgDetails.ProgName,
                            TotalDays = inputProgDetails.TotalDays,
                            Exersices = inputProgDetails.Exersices
                        };

                        // Create the framework in the program details container
                        await userProgDetsOut.AddAsync(progDetail);
                    }
                }
            }
            catch (Exception e)
            {
                return(new BadRequestResult());
            }

            return(new OkObjectResult(output));
        }