Ejemplo n.º 1
0
        public async Task <IActionResult> CreateOrUpdateAssignment(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = AssignmentsRoutePath)] HttpRequest request,
            [Table(AssignmentsTableName)] CloudTable assignmentsTable)
        {
            string result = await request.ReadAsStringAsync();

            AssignmentDto    assignmentDto    = JsonConvert.DeserializeObject <AssignmentDto>(result);
            AssignmentEntity assignmentEntity = _mapper.Map <AssignmentEntity>(assignmentDto);

            assignmentEntity.ETag = "*";

            TableOperation insertOrMergeAssignment = TableOperation.InsertOrMerge(assignmentEntity);
            TableResult    insertOrMergeResult     = await assignmentsTable.ExecuteAsync(insertOrMergeAssignment);

            if (insertOrMergeResult.HttpStatusCode < 200 || insertOrMergeResult.HttpStatusCode >= 300)
            {
                _logger.LogError($"Could not save assignment {assignmentEntity.ToAssignmentId()}. Error code: {insertOrMergeResult.HttpStatusCode}.");
                return(new InternalServerErrorResult());
            }

            _logger.LogInformation($"Saved assignment {assignmentEntity.ToAssignmentId()}.");

            string        assignmentUrl      = $"{request.Scheme}://{request.Host}/api/{AssignmentsRoutePath}/{assignmentEntity.ToAssignmentId()}";
            AssignmentDto savedAssignmentDto = _mapper.Map <AssignmentDto>(assignmentEntity);

            return(new CreatedResult(assignmentUrl, savedAssignmentDto));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateOrUpdateAssignment(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = AssignmentsRoutePath)] HttpRequest req,
            [Table(AssignmentsTableName)] CloudTable assignmentsTable,
            [User] UsersClient usersClient)
        {
            string result = await req.ReadAsStringAsync();

            AssignmentDto    assignmentDto    = JsonConvert.DeserializeObject <AssignmentDto>(result);
            AssignmentEntity assignmentEntity = _mapper.Map <AssignmentEntity>(assignmentDto);

            assignmentEntity.ETag = "*";

            #if !DEBUG
            //While debugging, authorization header is empty when this API gets called from either Lti1 API or LtiAdvantage API
            // So to enable seamless debugging, putting this code in #if !DEBUG block

            bool isSystemCallOrUserWithValidEmail = req.Headers.TryGetUserEmails(out List <string> userEmails);
            if (!isSystemCallOrUserWithValidEmail)
            {
                _logger.LogError("Could not get user email.");
                return(new BadRequestErrorMessageResult("Could not get user email."));
            }

            if (userEmails.Count > 0)
            {
                _logger.LogInformation($"Getting user information for '{string.Join(';', userEmails)}'.");

                User[] allUsers = await usersClient.GetAllUsers(assignmentDto.Id);

                User user = allUsers.FirstOrDefault(member => userEmails.Any(userEmail => (member.Email ?? String.Empty).Equals(userEmail)));
                if (user == null || !user.Role.Equals("teacher"))
                {
                    return(new UnauthorizedResult());
                }
            }
            #endif

            ValidationContext context = new ValidationContext(assignmentDto, null, null);
            if (!Validator.TryValidateObject(assignmentDto, context, new List <ValidationResult>(), true))
            {
                _logger.LogError("One or more enteries are incorrect. The length of provided assignment name / course name / assignment description is too long.");
                return(new BadRequestErrorMessageResult("One or more enteries are incorrect. The length of provided assignment name / course name / assignment description is too long."));
            }

            TableOperation insertOrMergeAssignment = TableOperation.InsertOrMerge(assignmentEntity);
            TableResult    insertOrMergeResult     = await assignmentsTable.ExecuteAsync(insertOrMergeAssignment);

            if (insertOrMergeResult.HttpStatusCode < 200 || insertOrMergeResult.HttpStatusCode >= 300)
            {
                _logger.LogError($"Could not save assignment {assignmentEntity.ToAssignmentId()}. Error code: {insertOrMergeResult.HttpStatusCode}.");
                return(new InternalServerErrorResult());
            }

            _logger.LogInformation($"Saved assignment {assignmentEntity.ToAssignmentId()}.");

            string        assignmentUrl      = $"{req.Scheme}://{req.Host}/api/{AssignmentsRoutePath}/{assignmentEntity.ToAssignmentId()}";
            AssignmentDto savedAssignmentDto = _mapper.Map <AssignmentDto>(assignmentEntity);

            return(new CreatedResult(assignmentUrl, savedAssignmentDto));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateOrUpdateAssignment(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = AssignmentsRoutePath)] HttpRequest req,
            [Table(AssignmentsTableName)] CloudTable assignmentsTable,
            [User] UsersClient usersClient)
        {
            string result = await req.ReadAsStringAsync();

            AssignmentDto    assignmentDto    = JsonConvert.DeserializeObject <AssignmentDto>(result);
            AssignmentEntity assignmentEntity = _mapper.Map <AssignmentEntity>(assignmentDto);

            assignmentEntity.ETag = "*";

            bool isSystemCallOrUserWithValidEmail = req.Headers.TryGetUserEmails(out List <string> userEmails);

            if (!isSystemCallOrUserWithValidEmail)
            {
                _logger.LogError("Could not get user email.");
                return(new BadRequestErrorMessageResult("Could not get user email."));
            }

            _logger.LogInformation($"Getting user information for '{string.Join(';', userEmails)}'.");

            if (userEmails.Count > 0)
            {
                User[] allUsers = await usersClient.GetAllUsers(assignmentDto.Id);

                User user = allUsers.FirstOrDefault(member => userEmails.Any(userEmail => (member.Email ?? String.Empty).Equals(userEmail)));
                if (user == null || !user.Role.Equals("teacher"))
                {
                    return(new UnauthorizedResult());
                }
            }

            TableOperation insertOrMergeAssignment = TableOperation.InsertOrMerge(assignmentEntity);
            TableResult    insertOrMergeResult     = await assignmentsTable.ExecuteAsync(insertOrMergeAssignment);

            if (insertOrMergeResult.HttpStatusCode < 200 || insertOrMergeResult.HttpStatusCode >= 300)
            {
                _logger.LogError($"Could not save assignment {assignmentEntity.ToAssignmentId()}. Error code: {insertOrMergeResult.HttpStatusCode}.");
                return(new InternalServerErrorResult());
            }

            _logger.LogInformation($"Saved assignment {assignmentEntity.ToAssignmentId()}.");

            string        assignmentUrl      = $"{req.Scheme}://{req.Host}/api/{AssignmentsRoutePath}/{assignmentEntity.ToAssignmentId()}";
            AssignmentDto savedAssignmentDto = _mapper.Map <AssignmentDto>(assignmentEntity);

            return(new CreatedResult(assignmentUrl, savedAssignmentDto));
        }
Ejemplo n.º 4
0
        private async Task <IActionResult> ChangePublishStatus(HttpRequest req, CloudTable table, IAsyncCollector <AssignmentEntity> assignmentEntityCollector, string assignmentId, UsersClient usersClient, PublishStatus newPublishStatus)
        {
            bool isSystemCallOrUserWithValidEmail = req.Headers.TryGetUserEmails(out List <string> userEmails);

            if (!isSystemCallOrUserWithValidEmail)
            {
                _logger.LogError("Could not get user email.");
                return(new BadRequestErrorMessageResult("Could not get user email."));
            }

            if (userEmails.Count > 0)
            {
                _logger.LogInformation($"Getting user information for '{string.Join(';', userEmails)}'.");

                User[] allUsers = await usersClient.GetAllUsers(assignmentId);

                User user = allUsers.FirstOrDefault(member => userEmails.Any(userEmail => (member.Email ?? String.Empty).Equals(userEmail)));
                if (user == null || !user.Role.Equals("teacher"))
                {
                    return(new UnauthorizedResult());
                }
            }

            AssignmentEntity assignmentEntity = await FetchAssignment(table, assignmentId);

            if (assignmentEntity == null)
            {
                return(new NotFoundResult());
            }

            assignmentEntity.PublishStatus = newPublishStatus.ToString();
            assignmentEntity.ETag          = "*";

            AssignmentDto assignmentDto = _mapper.Map <AssignmentDto>(assignmentEntity);

            ValidationContext context = new ValidationContext(assignmentDto, null, null);

            if (!Validator.TryValidateObject(assignmentDto, context, new List <ValidationResult>(), true))
            {
                _logger.LogError("One or more enteries are incorrect. Incorrect publish status entered.");
                return(new BadRequestErrorMessageResult("The publish status entered is incorrect."));
            }

            await assignmentEntityCollector.AddAsync(assignmentEntity);

            await assignmentEntityCollector.FlushAsync();

            return(new OkResult());
        }
Ejemplo n.º 5
0
        private async Task <IActionResult> ChangePublishStatus(CloudTable table, IAsyncCollector <AssignmentEntity> assignmentEntityCollector, string assignmentId, PublishStatus newPublishStatus)
        {
            AssignmentEntity assignmentEntity = await FetchAssignment(table, assignmentId);

            if (assignmentEntity == null)
            {
                return(new NotFoundResult());
            }

            assignmentEntity.PublishStatus = newPublishStatus.ToString();
            assignmentEntity.ETag          = "*";

            await assignmentEntityCollector.AddAsync(assignmentEntity);

            await assignmentEntityCollector.FlushAsync();

            return(new OkResult());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> GetAssignment(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = AssignmentsRoutePath + "/{assignmentId}")] HttpRequest req,
            [Table(AssignmentsTableName)] CloudTable table,
            [Platform] PlatformsClient platformsClient,
            string assignmentId)
        {
            AssignmentEntity assignmentEntity = await FetchAssignment(table, assignmentId);

            if (assignmentEntity == null)
            {
                return(new NotFoundResult());
            }

            AssignmentDto assignmentDto = _mapper.Map <AssignmentDto>(assignmentEntity);

            if (assignmentEntity.LtiVersion == LtiAdvantageVersionString)
            {
                Platform platform = await platformsClient.GetPlatform(assignmentEntity.PlatformId);

                assignmentDto.PlatformPersonalization = _mapper.Map <PlatformPersonalizationDto>(platform);
            }

            return(new OkObjectResult(assignmentDto));
        }