public async Task <IProjectModel> CreateAsync(Guid userId, IProjectModel project) { var newProject = mapper.Map <Project>(project); if (string.IsNullOrWhiteSpace(newProject.Name)) { throw new AppException("Project name is required"); } if (await projectRepository.CheckIfExistAsync(newProject.Name)) { throw new AppException("Project name \"" + newProject.Name + "\" is already taken"); } newProject.Id = Guid.NewGuid(); newProject.Abrv = project.Name.ToLower(); newProject.DateCreated = DateTime.Now; newProject.DateUpdated = DateTime.Now; newProject.OwnerId = userId; UserProject userProject = new UserProject(); foreach (var item in project.Users) { var user = await userRepository.GetByIdAsync(item.Id); userProject.ProjectId = newProject.Id; userProject.UserId = item.Id; userProject.Project = newProject; await projectRepository.AddUsersToProjectAsync(userProject); } return(mapper.Map <IProjectModel>(newProject)); }