public async Task Handle(UserHandlerData <AuthorAddingFormData> notification, CancellationToken cancellationToken) { Project project = await Context.Projects .FirstAsync(project => project.Name == notification.Data.ProjectName && project.Creator.UserName == notification.User.UserName); var unathorizedAuthor = new UnauthorizedAuthor { Email = notification.Data.Email, Project = project }; await AssignService.Assign(unathorizedAuthor); }
/// <summary> /// Assign author to project but if author is not registered /// add his email to database to assign him later. /// </summary> /// <param name="author">Author email and project.</param> /// <returns></returns> public async Task Assign(UnauthorizedAuthor author) { bool isAlreadyInProject = Context.Users .Where(user => user.ProjectRoles .Any(projectRole => projectRole.Project.Id == author.Project.Id)) .Any(user => user.Email == author.Email); if (isAlreadyInProject) { return; } User user = await Context.Users .Include(user => user.ProjectRoles) .FirstOrDefaultAsync(user => user.Email == author.Email); if (user != null) { Role authorRole = await Context.Roles.FirstAsync(role => role.Name == Role.Value.Author.ToString()); var projectRole = new UserProjectRole { Project = author.Project, Role = authorRole }; user.ProjectRoles = user.ProjectRoles.ToList(); ((List <UserProjectRole>)user.ProjectRoles).Add(projectRole); Context.Users.Update(user); await Context.SaveChangesAsync(); } else { bool isAssigned = Context.UnauthorizedAuthors .Any(unathorized => unathorized.Email == author.Email && unathorized.ProjectId == author.Project.Id); if (isAssigned == false) { Context.UnauthorizedAuthors.Add(author); await Context.SaveChangesAsync(); } } }