Beispiel #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "sessions/{sessionId}/questions/{questionId}/release")] HttpRequest req,
            string sessionId,
            string questionId,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring",
                 PartitionKey = "session-{sessionId}",
                 Id = "{sessionId}")] CosmosModels.Session session,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring")] IAsyncCollector <CosmosModels.Session> updatedSessions,
            ILogger log)
        {
            log.LogInformation("ReleaseQuestion was called.");

            if (session == null)
            {
                log.LogError("Invalid session id provided");
                return(new BadRequestObjectResult("Please provide a valid session id"));
            }
            else if (!session.Questions.Any(q => q.Id == questionId))
            {
                log.LogError("Invalid question id provided");
                return(new BadRequestObjectResult("Please provide valid question id"));
            }
            var question = session.Questions.First(q => q.Id == questionId);

            if (question.IsReleased == true)
            {
                log.LogError("Question was already released");
                return(new BadRequestObjectResult("Question was already released"));
            }

            // release question
            session.Questions.First(q => q.Id == questionId).IsReleased = true;
            session.LastReleasedQuestionId = questionId;
            await updatedSessions.AddAsync(session);

            log.LogInformation("Released question with id: " + questionId + "  on session: " + sessionId);

            // return ok
            return(new OkResult());
        }
Beispiel #2
0
        public static async Task <IActionResult> RunGetSessionSummary(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "sessions/{sessionId}/summary")] HttpRequest req,
            string sessionId,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring",
                 PartitionKey = "session-{sessionId}",
                 Id = "{sessionId}")] CosmosModels.Session session,
            ILogger log)
        {
            log.LogInformation("GetSessionSummary was called.");

            if (String.IsNullOrWhiteSpace(sessionId))
            {
                log.LogError("Invalid session id provided");
                return(new BadRequestObjectResult("Please provide a valid session id"));
            }
            else
            {
                if (session == null)
                {
                    log.LogError("Session not found: " + sessionId);
                    return(new NotFoundObjectResult("Session not found: " + sessionId));
                }
                else
                {
                    log.LogInformation("Returned session summary with id: " + sessionId);

                    // return ok
                    return(new OkObjectResult(
                               session.ToSummaryWireModel()
                               ));
                }
            }
        }
Beispiel #3
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "sessions/{sessionId}/questions")] WireModels.Question questionNotSerializedDeepEnough,
            HttpRequest req,
            string sessionId,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring",
                 PartitionKey = "session-{sessionId}",
                 Id = "{sessionId}")] CosmosModels.Session session,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring")] out CosmosModels.Session updatedSession,
            ILogger log)
        {
            log.LogInformation("AddQuestion was called.");

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            var    question    = JsonConvert.DeserializeObject <WireModels.Question>(requestBody);

            if (string.IsNullOrWhiteSpace(question.Id))
            {
                log.LogError("Invalid question id provided");
                updatedSession = null;
                return(new BadRequestObjectResult("Please provide a valid question id"));
            }
            else if (question.AnswerOptions == null ||
                     question.AnswerOptions.Count == 0 ||
                     question.AnswerOptions.Any(o => string.IsNullOrWhiteSpace(o.Id)))
            {
                updatedSession = null;
                log.LogError("Invalid answer options provided with question");
                return(new BadRequestObjectResult("Please provide valid answer options"));
            }

            // throw is not exists yet
            if (session == null)
            {
                updatedSession = null;
                return(new BadRequestObjectResult("Session does not exist: " + sessionId));
            }

            // ensure question added is a new one
            if (session.Questions != null && session.Questions.Any(q => q.Id == question.Id))
            {
                updatedSession = null;
                log.LogError("Question with same id exists already: " + question.Id);
                return(new BadRequestObjectResult("Question with same id exists already: " + question.Id));
            }

            // make sure a question starts out unreleased
            question.IsReleased = false;

            session.Questions.Add(Qna.Backend.CosmosModels.Question.FromWireModel(question));
            updatedSession = session;

            log.LogInformation("Added question to session with id: " + sessionId);

            // return ok
            return(new OkResult());
        }
Beispiel #4
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] SessionCreationDetails creationDetails,
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring",
                 PartitionKey = "session-{id}",
                 Id = "{id}")] CosmosModels.Session session, // {id} comes from creationDetails in POST body
            [CosmosDB(
                 databaseName: "%cosmosdbname%",
                 collectionName: "%cosmoscollectionname%",
                 ConnectionStringSetting = "cosmosconnectionstring")] out CosmosModels.Session sessionToCreate,
            ClaimsPrincipal principal,
            ILogger log)
        {
            log.LogInformation($"CreateSession was called for session {creationDetails.name} with id {creationDetails.id}.");

            // write out incoming claims:
            var isLoggedIn = false;

            if (principal != null && principal.Claims != null && principal.Claims.Count() > 0)
            {
                foreach (Claim claim in principal.Claims)
                {
                    log.LogInformation("CLAIM TYPE: " + claim.Type + "; CLAIM VALUE: " + claim.Value);
                }
                isLoggedIn = true;
            }
            else
            {
                log.LogInformation("Not found any claims.");
            }

            if (String.IsNullOrWhiteSpace(creationDetails.id))
            {
                log.LogError("Invalid session id provided");
                sessionToCreate = null;
                return(new BadRequestObjectResult("Please provide a valid session id"));
            }
            else if (String.IsNullOrWhiteSpace(creationDetails.name))
            {
                log.LogError("Invalid session name provided");
                sessionToCreate = null;
                return(new BadRequestObjectResult("Please provide a valid session name"));
            }
            else if (session != null)
            {
                sessionToCreate = null;
                log.LogError("Session exists already and cannot be created twice: " + session.SessionId);
                return(new BadRequestObjectResult("Session exists already"));
            }
            else
            {
                log.LogTrace("writing session to Cosmosdb");

                var authorSub       = "";
                var authorFirstName = "";
                var authorLastName  = "";
                var authorFullName  = "";
                var authorEmail     = "";

                if (isLoggedIn)
                {
                    log.LogInformation("recording author on the cosmos object");
                    authorSub       = principal.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value ?? "";
                    authorFirstName = principal.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")?.Value ?? "";
                    authorLastName  = principal.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname")?.Value ?? "";
                    authorFullName  = $"{authorFirstName} {authorLastName}";
                    log.LogInformation($"author: {authorFirstName} {authorLastName} with sub: {authorSub}");
                    authorEmail = principal.Claims.FirstOrDefault(c => c.Type == "emails")?.Value ?? "";
                }

                sessionToCreate = new CosmosModels.Session()
                {
                    Id          = creationDetails.id,
                    PartitionId = $"session-{creationDetails.id}",
                    SessionId   = creationDetails.id,
                    SessionName = creationDetails.name,
                    AuthorSub   = authorSub,
                    AuthorName  = authorFullName,
                    AuthorEmail = authorEmail
                };

                // return ok
                return(new OkResult());
            }
        }