Beispiel #1
0
        /// <inheritdoc />
        /// <summary>
        /// Returns a gradebook column from a course.
        /// </summary>
        /// <returns>The line item corresponding to the gradebook column.</returns>
        protected override async Task <ActionResult <LineItem> > OnGetLineItemAsync(GetLineItemRequest request)
        {
            if (!int.TryParse(request.ContextId, out var contextId))
            {
                var name = $"{nameof(request)}.{nameof(request.ContextId)}";
                ModelState.AddModelError(name, $"The {name} field cannot be converted into a course id.");
            }

            if (!int.TryParse(request.LineItemId, out var lineItemId))
            {
                var name = $"{nameof(request)}.{nameof(request.LineItemId)}";
                ModelState.AddModelError(name, $"The {name} field cannot be converted into a gradebook column id.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }

            if (!await _courseValidator.UserHasAccess(contextId))
            {
                return(Unauthorized(new ProblemDetails
                {
                    Title = "Not authorized",
                    Detail = "User not authorized to access the requested course."
                }));
            }

            var course = await _context.GetCourseAsync(contextId);

            if (course == null)
            {
                return(NotFound(new ProblemDetails
                {
                    Title = ReasonPhrases.GetReasonPhrase(StatusCodes.Status404NotFound),
                    Detail = "Course not found"
                }));
            }

            var gradebookColumn = course.GradebookColumns.SingleOrDefault(c => c.Id == lineItemId);

            if (gradebookColumn == null)
            {
                return(NotFound(new ProblemDetails
                {
                    Title = ReasonPhrases.GetReasonPhrase(StatusCodes.Status404NotFound),
                    Detail = "Gradebook column not found"
                }));
            }

            return(new LineItem
            {
                Id = Request.GetDisplayUrl(),
                EndDateTime = gradebookColumn.EndDateTime,
                Label = gradebookColumn.Label,
                ResourceId = gradebookColumn.ResourceId,
                ResourceLinkId = gradebookColumn.ResourceLink.Id.ToString(),
                ScoreMaximum = gradebookColumn.ScoreMaximum,
                StartDateTime = gradebookColumn.StartDateTime,
                Tag = gradebookColumn.Tag
            });
        }
Beispiel #2
0
        protected override Task <ActionResult <LineItem> > OnGetLineItemAsync(GetLineItemRequest request)
        {
            var result = (ActionResult <LineItem>) new ObjectResult(new LineItem());

            return(Task.FromResult(result));
        }