public async Task <int> CreateProject(CreateProjectData project) { Services.Validation.Validate.NotNull(project.Name, "Project name"); var projectId = await ps.CreateProjectAsync(project.Name, project.CategoryId, iis.GetProfileIdFromTokenPayload(GetTokenFromHeader())); return(projectId); }
int OnExecute(IConsole console, IConfig config, IConfiguration appConfiguration, IMicrosoftCustomTranslatorAPIPreview10 sdk, IAccessTokenClient atc) { LanguagePair = LanguagePair.ToLower(); // Get the supported language pairs var languagePairs = CallApi <IList <LanguagePair> >(() => sdk.GetSupportedLanguagePairs(atc.GetToken())); if (languagePairs == null) { return(-1); } var languagePairId = (from lp in languagePairs where lp.SourceLanguage.LanguageCode == LanguagePair.Split(":")[0] && (lp.TargetLanguage.LanguageCode == LanguagePair.Split(":")[1]) select lp.Id).FirstOrDefault(); if (languagePairId == null) { console.WriteLine("Invalid or unsupported LanguagePair."); return(-1); } // Get the categories var categories = CallApi <IList <TranslatorCategory> >(() => sdk.GetCategories(atc.GetToken())); if (categories == null) { return(-1); } var categoryId = (from c in categories where c.Name.ToLower() == Category.ToLower() select c.Id).FirstOrDefault(); if (categoryId == 0) { console.WriteLine("Invalid or unsupported Category."); return(-1); } // Populate the new project data var projectDefinition = new CreateProjectData() { LanguagePairId = (int)languagePairId.Value, CategoryId = (int)categoryId, Name = Name, CategoryDescriptor = CategoryDescriptor, Description = Description, Label = Label }; if (!Json.HasValue) { console.WriteLine("Creating project..."); } CallApi(() => sdk.CreateProject(projectDefinition, atc.GetToken(), WorkspaceId)); if (Label == null) { Label = string.Empty; } var res1 = CallApi <ProjectsResponse>(() => sdk.GetProjects(atc.GetToken(), WorkspaceId, 1, $"name eq {Name}", "createdDate desc")); if (res1 == null) { return(-1); } if (res1.Projects.Count == 0) { throw new Exception("Error: project creation failed."); } else { bool foundIt = false; foreach (var project in res1.Projects) { if (project.Name == Name && (project.Label == Label)) { foundIt = true; if (!Json.HasValue) { console.WriteLine($"{project.Id,30} {project.Name,-25}"); } else { console.WriteLine(SafeJsonConvert.SerializeObject(project, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented })); } break; } } if (!foundIt) { throw new Exception("Error: project creation failed."); } } return(0); }