コード例 #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 IActionResult GetSingleLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName, "{assignmentId}", "{linkId}")] AssignmentLinkEntity linkEntity)
        {
            if (linkEntity == null)
            {
                return(new NotFoundResult());
            }

            return(new OkObjectResult(_mapper.Map <AssignmentLinkDto>(linkEntity)));
        }
コード例 #3
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));
        }
コード例 #4
0
        public IActionResult GetSingleLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName, "{assignmentId}", "{linkId}")] AssignmentLinkEntity linkEntity)
        {
            if (linkEntity == null)
            {
                return(new NotFoundResult());
            }
            else if (!Guid.TryParse(linkEntity.RowKey, out Guid result))
            {
                return(new BadRequestErrorMessageResult("The provided link Id is malformed."));
            }

            return(new OkObjectResult(_mapper.Map <AssignmentLinkDto>(linkEntity)));
        }
コード例 #5
0
        public async Task <IActionResult> DeleteLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName)] CloudTable assignmentLinksTable,
            [Table(AssignmentLinksTableName, "{assignmentId}", "{linkId}")] AssignmentLinkEntity entityToDelete,
            string assignmentId,
            [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());
                }
            }

            if (entityToDelete == null)
            {
                return(new NoContentResult());
            }

            TableOperation deleteTable  = TableOperation.Delete(entityToDelete);
            TableResult    deleteResult = await assignmentLinksTable.ExecuteAsync(deleteTable);

            if (deleteResult.HttpStatusCode < 200 || deleteResult.HttpStatusCode >= 300)
            {
                return(new InternalServerErrorResult());
            }

            return(new OkResult());
        }
コード例 #6
0
        public async Task <IActionResult> DeleteLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "assignments/{assignmentId}/links/{linkId}")] HttpRequest req,
            [Table(AssignmentLinksTableName)] CloudTable assignmentLinksTable,
            [Table(AssignmentLinksTableName, "{assignmentId}", "{linkId}")] AssignmentLinkEntity entityToDelete)
        {
            if (entityToDelete == null)
            {
                return(new NoContentResult());
            }

            TableOperation deleteTable  = TableOperation.Delete(entityToDelete);
            TableResult    deleteResult = await assignmentLinksTable.ExecuteAsync(deleteTable);

            if (deleteResult.HttpStatusCode < 200 || deleteResult.HttpStatusCode >= 300)
            {
                return(new InternalServerErrorResult());
            }

            return(new OkResult());
        }