public async Task UpdateProjectUser(ObjectId projectId, ObjectId userId, PropertyUserDTO projectUserDTO) { var user = await _userRepository.GetAsync(userId); if (user == null) { throw new HttpStatusException(400, $"The user {userId} does not exist"); } if (userId != projectUserDTO.User.Id) { throw new HttpStatusException(400, "User id does not match"); } var roleIds = from role in projectUserDTO.Roles select role.Id; var newProjectUser = new PropertyUser() { UserId = userId, RoleIds = roleIds.ToList(), }; var existingProject = await _projectRepository.GetAsync(projectId); if (existingProject == null) { throw new HttpStatusException(404, "Invalid projectId"); } Company company = await _companyRepository.GetAsync(existingProject.CompanyId); if (company is null) { throw new HttpStatusException(StatusCodes.Status400BadRequest, "Company not found."); } await AuthorizeUpdateAsync(company); var projectLeaderRole = Role.ProjectLeaderRole; if (projectLeaderRole != null && roleIds.Contains(projectLeaderRole.Id)) { // Nutzer soll die Projektleiterrolle bekommen, in jedem Projekt // darf es aber nur einen ProjektLeiter geben var existingProjectLeader = from projectUser in existingProject.Users where projectUser.RoleIds.Contains(projectLeaderRole.Id) select projectUser; if (existingProjectLeader.Any()) { throw new HttpStatusException(403, "Cannot make two users a project Leader"); } } existingProject.Users.ReplaceOrInsert(x => x.UserId == userId, newProjectUser); await _projectRepository.UpdateAsync(existingProject); }
public async Task <ActionResult> UpdateProjectUser([FromBody] PropertyUserDTO projectUserDTO, [FromRoute] ObjectId projectId, ObjectId userId) { await _projectUserService.UpdateProjectUser(projectId, userId, projectUserDTO); return(NoContent()); }