Пример #1
0
        public async Task <IActionResult> SaveLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName)] IAsyncCollector <AssignmentLinkEntity> linksCollector,
            string assignmentId,
            string linkId)
        {
            string linkJson = await req.ReadAsStringAsync();

            AssignmentLinkDto linkDto = JsonConvert.DeserializeObject <AssignmentLinkDto>(linkJson);

            if (linkId != linkDto.Id)
            {
                return(new BadRequestErrorMessageResult("The provided link content doesn't match the path."));
            }

            _logger.LogInformation($"Starting the save process of link with ID [{linkId}] to assignment [{assignmentId}].");

            AssignmentLinkEntity assignmentLinkEntity = _mapper.Map <AssignmentLinkEntity>(linkDto);

            assignmentLinkEntity.PartitionKey = assignmentId;
            assignmentLinkEntity.ETag         = "*";

            await linksCollector.AddAsync(assignmentLinkEntity);

            await linksCollector.FlushAsync();

            _logger.LogInformation("Link saved.");

            AssignmentLinkDto savedLinkDto  = _mapper.Map <AssignmentLinkDto>(assignmentLinkEntity);
            string            assignmentUrl = $"{req.Scheme}://{req.Host}/api/assignments/{assignmentId}/links/{savedLinkDto.Id}";

            return(new CreatedResult(assignmentUrl, savedLinkDto));
        }
Пример #2
0
        public async Task <IActionResult> SaveLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName)] IAsyncCollector <AssignmentLinkEntity> linksCollector,
            string assignmentId,
            string linkId,
            [User] UsersClient usersClient)
        {
            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(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());
                }
            }

            string linkJson = await req.ReadAsStringAsync();

            AssignmentLinkDto linkDto = JsonConvert.DeserializeObject <AssignmentLinkDto>(linkJson);

            if (linkId != linkDto.Id)
            {
                return(new BadRequestErrorMessageResult("The provided link content doesn't match the path."));
            }

            _logger.LogInformation($"Starting the save process of link with ID [{linkId}] to assignment [{assignmentId}].");

            AssignmentLinkEntity assignmentLinkEntity = _mapper.Map <AssignmentLinkEntity>(linkDto);

            assignmentLinkEntity.PartitionKey = assignmentId;
            assignmentLinkEntity.ETag         = "*";

            await linksCollector.AddAsync(assignmentLinkEntity);

            await linksCollector.FlushAsync();

            AssignmentLinkDto savedLinkDto  = _mapper.Map <AssignmentLinkDto>(assignmentLinkEntity);
            string            assignmentUrl = $"{req.Scheme}://{req.Host}/api/assignments/{assignmentId}/links/{savedLinkDto.Id}";

            return(new CreatedResult(assignmentUrl, savedLinkDto));
        }