Ejemplo n.º 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)]
            HttpRequest req)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);


            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var data        = JsonConvert.DeserializeObject <UpdateTodoItemDto>(requestBody);
            var entity      = _mapper.Map <TodoItemEntity>(data);

            var listId = data.ListId;

            if (_todoListService.CanUserAccessList(user, listId))
            {
                return(new UnauthorizedResult());
            }

            entity.PartitionKey = listId;
            entity.ETag         = "*";

            _cloudTable.Execute(TableOperation.Merge(entity));

            return(new OkResult());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put",
                         Route = FunctionConstants.TodoList.Members.Update + "/{listId}")]
            HttpRequest req, string listId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, listId, ShareRole.Full))
            {
                return(new UnauthorizedResult());
            }

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var data        = JsonConvert.DeserializeObject <TodoListShareDto>(requestBody);

            var entity = new TodoListMemberEntity
            {
                ListId = listId,
                Role   = data.Role,
                UserId = data.Member.UserId
            };

            _todoListMemberService.Save(entity);

            return(new OkResult());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            var rowKey = Guid.NewGuid().ToString();

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var data        = JsonConvert.DeserializeObject <NewTodoItemDto>(requestBody);
            var listId      = data.ListId;

            if (data.ListId == null)
            {
                return(new BadRequestResult());
            }

            if (!_todoListService.CanUserAccessList(user, listId, ShareRole.Edit))
            {
                return(new UnauthorizedResult());
            }

            _todoItemService.Insert(new TodoItemEntity
            {
                PartitionKey = listId, RowKey = rowKey, Name = data.Name,
                Priority     = (int)data.Priority, Status = data.Status
            });

            return(new OkObjectResult(rowKey));
        }
Ejemplo n.º 4
0
        public IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = FunctionConstants.TodoItem.Get + "/{listId}")]
            HttpRequest req, string listId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, listId, ShareRole.View))
            {
                return(new UnauthorizedResult());
            }

            if (string.IsNullOrEmpty(listId))
            {
                return(new BadRequestErrorMessageResult("Id cannot be empty"));
            }

            var todoList = _todoItemService.GetEntitiesForPartitionKey(listId).ToList();

            var result = new TodoListWithItemsDto {
                Items     = _mapper.Map <List <TodoItemDto> >(todoList),
                Id        = listId,
                ShareRole = _todoListMemberService.GetUserShareRole(user.UserId, listId) ?? ShareRole.Full
            };

            return(new OkObjectResult(result));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = FunctionConstants.DeleteTodoItemFunction + "/{listId}/{itemId}")]
            HttpRequest req,
            string listId,
            string itemId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, listId))
            {
                return(new UnauthorizedResult());
            }

            if (string.IsNullOrEmpty(itemId))
            {
                return(new BadRequestObjectResult("Id or listId cannot be empty"));
            }

            var result = await _cloudTable.ExecuteAsync(TableOperation.Retrieve <TodoItemEntity>(listId, itemId));

            if (result?.Result is TodoItemEntity entity)
            {
                _cloudTable.Execute(TableOperation.Delete(entity));
            }
            else
            {
                return(new NotFoundResult());
            }

            return(new NoContentResult());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = FunctionConstants.TodoItem.Delete + "/{listId}/{itemId}")]
            HttpRequest req,
            string listId,
            string itemId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, listId, ShareRole.Edit))
            {
                return(new UnauthorizedResult());
            }

            if (string.IsNullOrEmpty(itemId))
            {
                return(new BadRequestObjectResult("Id or listId cannot be empty"));
            }


            if (!await _itemService.DeleteAsync(listId, itemId))
            {
                return(new NotFoundResult());
            }

            return(new NoContentResult());
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = FunctionConstants.TodoList.Delete + "/{id}")]
            HttpRequest req, string id)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, id, ShareRole.Full))
            {
                return(new UnauthorizedResult());
            }

            if (string.IsNullOrEmpty(id))
            {
                return(new BadRequestObjectResult("Id or listId cannot be empty"));
            }


            if (_todoListService.DeleteByRowKey(id))
            {
                _itemService.DeleteEntitiesWithPartitionKey(id);
                return(new NoContentResult());
            }

            return(new NotFoundResult());
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)]
            HttpRequest req)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var data        = JsonConvert.DeserializeObject <UpdateTodoListDto>(requestBody);

            if (!_todoListService.CanUserAccessList(user, data.ListId, ShareRole.Full))
            {
                return(new UnauthorizedResult());
            }

            var entity = _mapper.Map <TodoListEntity>(data);

            _todoListService.Save(entity);

            return(new OkResult());
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete",
                         Route = FunctionConstants.TodoList.Members.Remove + "/{listId}/{userId}")]
            HttpRequest req, string listId, string userId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);


            if (!_todoListService.CanUserAccessList(user, listId, ShareRole.Full))
            {
                return(new UnauthorizedResult());
            }

            if (!await _todoListMemberService.DeleteAsync(listId, userId))
            {
                return(new NotFoundResult());
            }

            return(new NoContentResult());
        }
        public IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = FunctionConstants.GetItemsOfTodoListFunction + "{listId}")]
            HttpRequest req, string listId)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            if (!_todoListService.CanUserAccessList(user, listId))
            {
                return(new UnauthorizedResult());
            }

            if (string.IsNullOrEmpty(listId))
            {
                return(new BadRequestErrorMessageResult("Id cannot be empty"));
            }

            var todoList = _todoItemService.GetAllForListId(listId);

            return(new OkObjectResult(_mapper.Map <IEnumerable <TodoItemDto> >(todoList)));
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)]
            HttpRequest req)
        {
            var user = _authService.GetClientPrincipalFromRequest(req);

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var data        = JsonConvert.DeserializeObject <NewTodoListMemberDto>(requestBody);

            if (!_todoListService.CanUserAccessList(user, data.ListId, ShareRole.Full))
            {
                return(new UnauthorizedResult());
            }

            var entity = new TodoListMemberEntity
            {
                PartitionKey = data.ListId,
                RowKey       = data.UserId
            };

            _listMemberService.Insert(entity);

            return(new OkResult());
        }