Skip to content

Commit

Permalink
Merge pull request #115 from ilabutin/issue-105-import-via-api
Browse files Browse the repository at this point in the history
Added API import functionality to Importer
  • Loading branch information
ilabutin authored Nov 16, 2019
2 parents 00027ff + a2817d2 commit b2f127d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 25 deletions.
43 changes: 22 additions & 21 deletions DotNetRuServer.Importer/DotNetRuServer.Importer.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetRuServer.Comon.BL\DotNetRuServer.Comon.BL.csproj" />
<ProjectReference Include="..\DotNetRuServer.Meetups.BL\DotNetRuServer.Meetups.BL.csproj" />
<ProjectReference Include="..\DotNetRuServer.Meetups.DAL\DotNetRuServer.Meetups.DAL.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.1" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0006" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetRuServer.Comon.BL\DotNetRuServer.Comon.BL.csproj" />
<ProjectReference Include="..\DotNetRuServer.Meetups.BL\DotNetRuServer.Meetups.BL.csproj" />
<ProjectReference Include="..\DotNetRuServer.Meetups.DAL\DotNetRuServer.Meetups.DAL.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.1" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0006" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions DotNetRuServer.Importer/ImportViaApiOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommandLine;

namespace DotNetRuServer.Importer
{
/// <summary>
/// Set of options required to import data from github Audit repo to
/// Server via API
/// </summary>
[Verb("importViaApi", HelpText = "Imports Audit data from Github repo to the Server using API.")]
public class ImportViaApiOptions
{
/// <summary>
/// Server URL
/// </summary>
[Option("serverUrl", Required = true, HelpText = "Server URL. Example: https://server-dotnetru.azurewebsites.net/.")]
public string ServerUrl { get; set; }

/// <summary>
/// Github token
/// </summary>
[Option("githubToken", Required = true, HelpText = "Github token.")]
public string GithubToken { get; set; }
}
}
24 changes: 24 additions & 0 deletions DotNetRuServer.Importer/ImportViaDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommandLine;

namespace DotNetRuServer.Importer
{
/// <summary>
/// Set of options required to import data from github Audit repo to
/// Server via API
/// </summary>
[Verb("importViaDatabase", HelpText = "Imports Audit data from Github repo to the Server using direct database connection.")]
public class ImportViaDatabaseOptions
{
/// <summary>
/// Server URL
/// </summary>
[Option("dbConnection", Required = true, HelpText = "Database connection string.")]
public string DatabaseConnection { get; set; }

/// <summary>
/// Github token
/// </summary>
[Option("githubToken", Required = true, HelpText = "Github token.")]
public string GithubToken { get; set; }
}
}
42 changes: 38 additions & 4 deletions DotNetRuServer.Importer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;
using CommandLine;
using DotNetRuServer.Comon.BL.Enums;
using DotNetRuServer.Meetups.BL.Entities;
using DotNetRuServer.Meetups.DAL.Database;
Expand All @@ -19,20 +20,52 @@ namespace DotNetRuServer.Importer
{
internal class Program
{
private static async Task Main(string[] args)
private static async Task<int> Main(string[] args)
{
var options = Parser.Default.ParseArguments<ImportViaApiOptions, ImportViaDatabaseOptions>(args);

return await options.MapResult(
async (ImportViaApiOptions o) => await ImportViaApi(o),
async (ImportViaDatabaseOptions o) => await ImportViaDatabase(o),
_ => Task.FromResult(-1));
}

private static async Task<int> ImportViaApi(ImportViaApiOptions options)
{
Console.WriteLine("Importing data to Server at {0} from Github.", options.ServerUrl);

var query = $"{options.ServerUrl}api/import?githubToken={options.GithubToken}";

var client = new HttpClient();
var result = await client.PostAsync(query, new StringContent(string.Empty));

if (result.IsSuccessStatusCode)
{
Console.WriteLine("Completed successfully!");
return 0;
}
else
{
Console.WriteLine("Import failed.");
Console.WriteLine(await result.Content.ReadAsStringAsync());
return -1;
}
}

private static async Task<int> ImportViaDatabase(ImportViaDatabaseOptions options)
{
Console.WriteLine("It's time to start");

var connectionString = args[0];
var githubToken = args[1];
var connectionString = options.DatabaseConnection;
var githubToken = options.GithubToken;

Console.WriteLine($"Github token - {githubToken}");
Console.WriteLine($"Connection string - {connectionString}");

var optionsBuilder = new DbContextOptionsBuilder<DotNetRuServerContext>();
optionsBuilder.UseSqlServer(connectionString);
var context = new DotNetRuServerContext(optionsBuilder.Options);

var github = new GitHubClient(new ProductHeaderValue("DotNetRuServer"));
var tokenAuth = new Credentials(githubToken);
github.Credentials = tokenAuth;
Expand All @@ -51,6 +84,7 @@ private static async Task Main(string[] args)
Console.WriteLine("Start to import Meetups");
await importer.ImportMeetups();
Console.WriteLine("All data is imported");
return 0;
}
}

Expand Down

0 comments on commit b2f127d

Please sign in to comment.