コード例 #1
0
        public async Task <IActionResult> DeleteTaskCompletionByTaskId(Guid taskId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            Task completedTask = await _taskBookBusiness.GetUsersTaskCompletionByUserAndTaskId(loggedOnUser.Id, taskId);

            if (completedTask?.DateTimeCompleted == null || completedTask.AssignedToUserId == null)
            {
                return(NotFound());
            }

            if (completedTask.AssignedToUserId != loggedOnUser.Id)
            {
                return(Forbid());
            }

            bool deleted = await _taskBookBusiness.DeleteTaskCompletion(taskId);

            if (!deleted)
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> GetMembershipByMemberUserName(Guid groupId, string memberUserName)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId);

            if (!loggedOnUserIsGroupOwner)
            {
                return(NotFound());
            }

            User memberUser = await _identityBusiness.FindByNameAsync(memberUserName);

            if (memberUser == null)
            {
                return(NotFound());
            }

            UserGroup membership = await _taskBookBusiness.GetGroupMembership(memberUser.Id, groupId);

            if (membership == null)
            {
                return(NotFound());
            }

            var viewModel = _mapper.Map <GroupMembershipViewModel>(membership);

            return(Ok(viewModel));
        }
コード例 #3
0
        public async Task <ActionResult> DeleteTask(Guid groupId, Guid taskId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            bool isUserRelatedWithGroup = await _taskBookBusiness.IsUserRelatedWithGroup(loggedOnUser.Id, groupId);

            if (!isUserRelatedWithGroup)
            {
                return(NotFound());
            }

            Task task = await _taskBookBusiness.GetTaskByTaskId(taskId);

            if (task == null)
            {
                return(NotFound());
            }

            bool isUserTaskCreator = await _taskBookBusiness.IsUserTaskCreator(loggedOnUser.Id, taskId);

            if (!isUserTaskCreator)
            {
                return(Forbid());
            }

            bool deleted = await _taskBookBusiness.DeleteTask(taskId);

            if (!deleted)
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <IActionResult> DeleteMembership(Guid groupId, string memberUserName)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId);

            if (!loggedOnUserIsGroupOwner)
            {
                return(NotFound());
            }

            User memberUser = await _identityBusiness.FindByNameAsync(memberUserName);

            if (memberUser == null)
            {
                return(NotFound());
            }

            UserGroup membership = await _taskBookBusiness.GetGroupMembership(memberUser.Id, groupId);

            if (membership == null)
            {
                return(NotFound());
            }

            if (membership.RelationType != UserGroupRelationType.Member)
            {
                return(Forbid());
            }

            await _taskBookBusiness.DeleteGroupMembership(memberUser.Id, groupId);

            return(NoContent());
        }
コード例 #5
0
        public async Task <IActionResult> UpdateUserGroup(Guid groupId, [FromBody] UpdateUserGroupParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            UserGroup groupToUpdateGroup = await _taskBookBusiness.GetUserGroupByGroupId(loggedOnUser.Id, groupId);

            if (groupToUpdateGroup == null)
            {
                return(NotFound());
            }

            if (groupToUpdateGroup.RelationType != UserGroupRelationType.Owner)
            {
                return(Forbid());
            }

            UserGroup updatedGroup = await _taskBookBusiness.UpdateGroup(loggedOnUser.Id, groupId, parameters.GroupName, parameters.IsActive);

            var viewModel = _mapper.Map <UserGroupViewModel>(updatedGroup);

            return(Ok(viewModel));
        }
コード例 #6
0
        public async Task <ActionResult> UpdateTask(Guid groupId, Guid taskId, [FromBody] UpdateGroupTaskParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            bool isUserRelatedWithGroup = await _taskBookBusiness.IsUserRelatedWithGroup(loggedOnUser.Id, groupId);

            if (!isUserRelatedWithGroup)
            {
                return(NotFound());
            }

            bool isUserTaskCreator = await _taskBookBusiness.IsUserTaskCreator(loggedOnUser.Id, taskId);

            if (!isUserTaskCreator)
            {
                return(Forbid());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            if (!parameters.Deadline.HasValue)
            {
                ModelState.AddModelError(nameof(parameters.Deadline), "Please provide a deadline date.");
                return(UnprocessableEntity(ModelState));
            }

            DateTime serverDateTime = await _taskBookBusiness.GetServerDateTime();

            if (parameters.Deadline < serverDateTime)
            {
                ModelState.AddModelError(nameof(parameters.Deadline), "Deadline cannot be in the past.");
                return(UnprocessableEntity(ModelState));
            }

            Task updatedTask = await _taskBookBusiness.UpdateGroupTask(
                taskId,
                parameters.Title,
                parameters.Description,
                parameters.Deadline.Value);

            if (updatedTask == null)
            {
                return(NotFound());
            }

            var model = _mapper.Map <TaskViewModel>(updatedTask);

            return(Ok(model));
        }
コード例 #7
0
        public async Task <IActionResult> GetUserMemberships()
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            UserGroup[] memberships = await _taskBookBusiness.GetUserMemberships(loggedOnUser.Id);

            var models = _mapper.Map <UserMembershipsViewModel[]>(memberships);

            return(Ok(models));
        }
コード例 #8
0
        public async Task <IActionResult> GetAllUserTaskCompletions()
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            Task[] userTasks = await _taskBookBusiness.GetUsersTaskCompletionsByUserId(loggedOnUser.Id);

            var models = _mapper.Map <TaskViewModel[]>(userTasks);

            return(Ok(models));
        }
コード例 #9
0
        public async Task <IActionResult> GetAllUserGroups()
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            UserGroup[] userGroups = await _taskBookBusiness.GetUserGroupsByUserId(loggedOnUser.Id);

            var viewModels = _mapper.Map <UserGroupViewModel[]>(userGroups);

            return(Ok(viewModels));
        }
コード例 #10
0
        public async Task <IActionResult> PartiallyUpdateProfile(
            [FromRoute] string userName,
            [FromBody] JsonPatchDocument <UpdateProfileParameters> patchDocument)
        {
            if (patchDocument == null)
            {
                return(BadRequest());
            }

            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            var userToPatch = _mapper.Map <UpdateProfileParameters>(loggedOnUser);

            patchDocument.ApplyTo(userToPatch, ModelState);

            TryValidateModel(userToPatch);

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            _mapper.Map(userToPatch, loggedOnUser);

            IdentityResult result = await _identityBusiness.UpdateAsync(loggedOnUser);

            if (!result.Succeeded)
            {
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }

                return(UnprocessableEntity(ModelState));
            }

            return(NoContent());
        }
コード例 #11
0
        public async Task <IActionResult> GetUserGroupById(Guid groupId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            UserGroup userGroup = await _taskBookBusiness.GetUserGroupByGroupId(loggedOnUser.Id, groupId);

            if (userGroup == null)
            {
                return(NotFound());
            }

            var viewModel = _mapper.Map <UserGroupViewModel>(userGroup);

            return(Ok(viewModel));
        }
コード例 #12
0
        public async Task <IActionResult> GetUserTaskCompletionByTaskId(Guid taskId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            Task completedTask = await _taskBookBusiness.GetUsersTaskCompletionByUserAndTaskId(loggedOnUser.Id, taskId);

            if (completedTask == null)
            {
                return(NotFound());
            }

            var model = _mapper.Map <TaskViewModel>(completedTask);

            return(Ok(model));
        }
コード例 #13
0
        public async Task <IActionResult> UpdateProfile(
            [FromRoute] string userName,
            [FromBody] UpdateProfileParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            _mapper.Map(parameters, loggedOnUser);

            IdentityResult result = await _identityBusiness.UpdateAsync(loggedOnUser);

            if (!result.Succeeded)
            {
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }

                return(UnprocessableEntity(ModelState));
            }

            User updatedUser = await _identityBusiness.GetUserAsync(User);

            var profile = _mapper.Map <ProfileViewModel>(updatedUser);

            return(Ok(profile));
        }
コード例 #14
0
        public async Task <IActionResult> GetAllMemberships(Guid groupId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId);

            if (!loggedOnUserIsGroupOwner)
            {
                return(NotFound());
            }

            UserGroup[] memberships = await _taskBookBusiness.GetGroupMemberships(groupId);

            var viewModels = _mapper.Map <GroupMembershipViewModel[]>(memberships);

            return(Ok(viewModels));
        }
コード例 #15
0
        public async Task <IActionResult> GetAllGroupTaks(Guid groupId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            bool isUserRelatedWithGroup = await _taskBookBusiness.IsUserRelatedWithGroup(loggedOnUser.Id, groupId);

            if (!isUserRelatedWithGroup)
            {
                return(NotFound());
            }

            Task[] tasks = await _taskBookBusiness.GetTasksByGroupId(groupId);

            var models = _mapper.Map <TaskViewModel[]>(tasks);

            return(Ok(models));
        }
コード例 #16
0
        public async Task <IActionResult> ChangePassword(
            [FromRoute] string userName,
            [FromBody] ChangePasswordParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            IdentityResult result = await _identityBusiness.ChangePasswordAsync(loggedOnUser, parameters.CurrentPassword, parameters.NewPassword);

            if (result.Succeeded)
            {
                return(NoContent());
            }

            foreach (IdentityError error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }

            return(UnprocessableEntity(ModelState));
        }
コード例 #17
0
        public async Task <IActionResult> DeleteUserGroup(Guid groupId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            UserGroup groupToDelete = await _taskBookBusiness.GetUserGroupByGroupId(loggedOnUser.Id, groupId);

            if (groupToDelete == null)
            {
                return(NotFound());
            }

            if (groupToDelete.RelationType != UserGroupRelationType.Owner)
            {
                return(Forbid());
            }

            await _taskBookBusiness.DeleteGroup(groupId);

            return(NoContent());
        }
コード例 #18
0
        public async Task <IActionResult> GetProfile(string userName)
        {
            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            var profile = _mapper.Map <ProfileViewModel>(loggedOnUser);

            return(Ok(profile));
        }
コード例 #19
0
        public async Task <IActionResult> CreateUserGroup([FromBody] CreateUserGroupParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            UserGroup createdGroup = await _taskBookBusiness.CreateUserGroup(loggedOnUser.Id, parameters.GroupName, parameters.IsActive);

            var viewModel = _mapper.Map <UserGroupViewModel>(createdGroup);

            return(CreatedAtRoute(nameof(GetUserGroupById), new { viewModel.GroupId }, viewModel));
        }
コード例 #20
0
        public async Task <IActionResult> CreateTaskAssignment(Guid?taskId)
        {
            if (!taskId.HasValue)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            Task userTask = await _taskBookBusiness.GetUsersTaskByUserAndTaskId(loggedOnUser.Id, taskId.Value);

            if (userTask == null)
            {
                return(NotFound());
            }

            if (userTask.AssignedToUserId.HasValue)
            {
                if (userTask.AssignedToUserId != loggedOnUser.Id)
                {
                    return(Conflict("Task has already been assigned to a different assignee."));
                }

                if (userTask.DateTimeCompleted.HasValue)
                {
                    return(Conflict("Cannot create task assignment for a completed task."));
                }

                Task existing = await _taskBookBusiness.GetUsersTaskAssignmentByUserAndTaskId(loggedOnUser.Id, taskId.Value);

                var existingModel = _mapper.Map <TaskViewModel>(existing);

                return(Ok(existingModel));
            }

            Task assignedTask = await _taskBookBusiness.CreateTaskAssignmen(loggedOnUser.Id, taskId.Value);

            var model = _mapper.Map <TaskViewModel>(assignedTask);

            return(CreatedAtRoute(nameof(GetUserTaskAssignmentByTaskId), new { TaskId = assignedTask.Id }, model));
        }
コード例 #21
0
        public async Task <IActionResult> CreateMembership(Guid groupId, [FromBody] CreateGroupMembershipParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId);

            if (!loggedOnUserIsGroupOwner)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            User memberUser = await _identityBusiness.FindByNameAsync(parameters.UserName);

            if (memberUser == null)
            {
                return(NotFound());
            }

            UserGroup existingMembership = await _taskBookBusiness.GetGroupMembership(memberUser.Id, groupId);

            if (existingMembership != null)
            {
                return(Conflict($"'{memberUser.UserName}' is already member of '{existingMembership.Group.Name}'."));
            }

            UserGroup membership = await _taskBookBusiness.CrateGroupMembership(memberUser.Id, groupId);

            var viewModel = _mapper.Map <GroupMembershipViewModel>(membership);

            return(CreatedAtRoute(nameof(GetMembershipByMemberUserName), new { memberUserName = memberUser.UserName }, viewModel));
        }
コード例 #22
0
        public async Task <IActionResult> CreateTaskCompletion(Guid?taskId)
        {
            if (!taskId.HasValue)
            {
                return(BadRequest());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            Task userTask = await _taskBookBusiness.GetUsersTaskByUserAndTaskId(loggedOnUser.Id, taskId.Value);

            if (userTask == null)
            {
                return(NotFound());
            }

            if (!userTask.AssignedToUserId.HasValue)
            {
                return(Conflict("There should be a task assignment before task completion."));
            }

            if (userTask.AssignedToUserId != loggedOnUser.Id)
            {
                return(Conflict("Assignment to another assignee exists."));
            }

            if (userTask.DateTimeCompleted.HasValue)
            {
                Task existing = await _taskBookBusiness.GetUsersTaskCompletionByUserAndTaskId(loggedOnUser.Id, taskId.Value);

                var existingModel = _mapper.Map <TaskViewModel>(existing);
                return(Ok(existingModel));
            }

            Task completedTask = await _taskBookBusiness.CreateTaskCompletion(loggedOnUser.Id, taskId.Value);

            var model = _mapper.Map <TaskViewModel>(completedTask);

            return(CreatedAtRoute(nameof(GetUserTaskCompletionByTaskId), new { TaskId = completedTask.Id }, model));
        }
コード例 #23
0
        public async Task <IActionResult> GetTaskByTaskId(Guid groupId, Guid taskId)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            bool isUserRelatedWithGroup = await _taskBookBusiness.IsUserRelatedWithGroup(loggedOnUser.Id, groupId);

            if (!isUserRelatedWithGroup)
            {
                return(NotFound());
            }

            Task task = await _taskBookBusiness.GetTaskByTaskId(taskId);

            if (task == null)
            {
                return(NotFound());
            }

            var models = _mapper.Map <TaskViewModel>(task);

            return(Ok(models));
        }
コード例 #24
0
        public async Task <IActionResult> CreateAccount([FromBody] CreateAccountParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            var user = new User
            {
                UserName = parameters.UserName,
                Email    = parameters.Email
            };

            IdentityResult result = await _identityBusiness.CreateAsync(user, parameters.Password);

            if (result.Succeeded)
            {
                var account = new AccountViewModel
                {
                    UserName = user.UserName,
                    Email    = user.Email
                };

                return(CreatedAtRoute(nameof(GetAccount), new { parameters.UserName }, account));
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }

            return(UnprocessableEntity(ModelState));
        }
コード例 #25
0
        public async Task <IActionResult> GetAllUserTasks(GetUserTasksParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            UserTaskPage taskPage = await _taskBookBusiness.GetUsersTaskByUserId(loggedOnUser.Id, parameters);

            string previousPageLink = CreatePageLink(taskPage, parameters, -1);
            string nextPageLink     = CreatePageLink(taskPage, parameters, 1);

            var metaData = new
            {
                taskPage.TotalCount,
                taskPage.PageSize,
                taskPage.TotalPages,
                taskPage.CurrentPage,
                previousPageLink,
                nextPageLink
            };

            string pagingMetaDataJson = JsonConvert.SerializeObject(metaData);

            Response.Headers.Add("X-PagingMetadata", pagingMetaDataJson);

            var models = _mapper.Map <TaskViewModel[]>(taskPage.Tasks);

            return(Ok(models));
        }