Skip to content

Commit

Permalink
#1 TextAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinacupac committed Jun 13, 2020
1 parent ad67c1a commit 249ad07
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 53 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace TextAnalyzer.Web.ConsoleClient
{
public class AppOptions
{
public const string Key = "App";

public string RestApi { get; set; }

public string ReaderFactory { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TextAnalyzer.Core.Application.Commands.Documents;
using TextAnalyzer.Web.ConsoleClient.Interface;
using TextAnalyzer.Web.RestClient.Interface;

namespace TextAnalyzer.Web.ConsoleClient
{
public class ConsoleExecutor : IExecutor
{
private readonly IApiClient _apiClient;
private readonly IReaderFactory _readerFactory;

public ConsoleExecutor(IApiClient apiClient, IReaderFactory readerFactory)
{
_apiClient = apiClient;
_readerFactory = readerFactory;
}

public async Task ExecuteAsync()
{
var reader = _readerFactory.Create();

var document = reader.Read();

var request = new CreateDocumentCommand
{
Name = document.Name,
Text = document.Text,
};

var response = await _apiClient.Documents.CreateDocumentAsync(request);

Console.WriteLine($"Word count is: {response.Data.WordCount}");

Console.ReadKey();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace TextAnalyzer.Web.ConsoleClient
{
public class HttpClientFactory : IHttpClientFactory
{
private readonly IOptions<ApiOptions> _options;
private readonly IOptions<AppOptions> _options;

public HttpClientFactory(IOptions<ApiOptions> options)
public HttpClientFactory(IOptions<AppOptions> options)
{
_options = options;
}

public HttpClient CreateClient(string name)
{
var baseAddressUrl = _options.Value.Connection;
var baseAddressUrl = _options.Value.RestApi;

var baseAddress = new Uri(baseAddressUrl);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace TextAnalyzer.Web.ConsoleClient.Interface
{
public enum ReaderType
public interface IExecutor
{
None = 0,
Console = 1,
Database = 2,
File = 3
Task ExecuteAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using TextAnalyzer.Core.Application.Commands.Documents;
using TextAnalyzer.Core.Application.Queries.Customers;
using TextAnalyzer.Web.ConsoleClient.Interface;
using TextAnalyzer.Web.RestClient;
using TextAnalyzer.Web.RestClient.Interface;

Expand All @@ -20,34 +21,19 @@ static async Task Main(string[] args)
{
try
{
Console.WriteLine("Hello World!");
Console.WriteLine("Starting...");

await ExecuteAsync(serviceProvider);
var executor = serviceProvider.GetRequiredService<IExecutor>();

Console.WriteLine("Finished");
await executor.ExecuteAsync();

Console.WriteLine("Finished.");
}
catch (Exception ex)
{
Console.WriteLine($"Error {ex.Message}");
Console.WriteLine($"Error has occurred. Details: {ex.Message}");
}
}
}

private static async Task ExecuteAsync(ServiceProvider serviceProvider)
{
var api = serviceProvider.GetRequiredService<IApiClient>();

var request = new CreateDocumentCommand
{
Name = $"My doc {DateTime.Now.ToString()}",
Text = "This is some text",
};

var response = await api.Documents.CreateDocumentAsync(request);

Console.WriteLine($"Response is: {response.Data.WordCount}");

Console.ReadKey();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace TextAnalyzer.Web.ConsoleClient.Readers
Expand Down Expand Up @@ -35,9 +38,34 @@ public class DatabaseReader : IReader

public Document Read()
{
// TODO: Create DB connection read, close DB connection
var parameters = new
{
Name = DocumentName,
};

throw new NotImplementedException();
using(var connection = new SqlConnection(ConnectionString))
{
var query = $"SELECT {DocumentTextField} FROM {DocumentTableName}" +
$"WHERE {DocumentNameField} = @DocumentName";

var documentRecords = connection
.Query<DocumentRecord>(query, parameters)
.ToList();

return documentRecords.Select(GetDocument).FirstOrDefault();
}
}

private Document GetDocument(DocumentRecord record)
{
return new Document(record.Name, record.Text);
}

private class DocumentRecord
{
public string Name { get; set; }

public string Text { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Optivem.Atomiv.Core.Common.Serialization;
using Optivem.Atomiv.Infrastructure.NewtonsoftJson;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Text;
using TextAnalyzer.Web.ConsoleClient.Interface;
using TextAnalyzer.Web.RestClient;
using TextAnalyzer.Web.RestClient.Interface;

Expand All @@ -15,6 +18,8 @@ public class ServiceProviderFactory
{
private const string DefaultEnvironment = "Production";

private const string ReaderFactoryKey = "ReaderFactory";

public static ServiceProvider Create(string environment = null)
{
if(environment == null)
Expand All @@ -38,7 +43,9 @@ public static ServiceProvider Create(string environment = null)

var services = new ServiceCollection();

services.Configure<ApiOptions>(configuration.GetSection(ApiOptions.Api));
var appOptions = configuration.GetSection(AppOptions.Key);

services.Configure<AppOptions>(appOptions);

services.AddSingleton<IJsonSerializer, JsonSerializer>();

Expand All @@ -49,6 +56,21 @@ public static ServiceProvider Create(string environment = null)
services.AddHttpClient<IProductControllerClient, ProductControllerClient>();
services.AddTransient<IApiClient, ApiClient>();

services.AddTransient<IExecutor, ConsoleExecutor>();

var readerFactoryName = appOptions.GetValue<string>(ReaderFactoryKey);

var assembly = Assembly.GetAssembly(typeof(ServiceProviderFactory));

var type = assembly.GetType($"TextAnalyzer.Web.ConsoleClient.Factories.{readerFactoryName}");

if(type == null)
{
throw new Exception($"Reader factory {readerFactoryName} is not recognized");
}

services.AddTransient(e => (IReaderFactory)Activator.CreateInstance(type));

var serviceProvider = services.BuildServiceProvider();

return serviceProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.4" />
<PackageReference Include="Optivem.Atomiv.Core.Common" Version="1.0.31" />
<PackageReference Include="Optivem.Atomiv.Infrastructure.NewtonsoftJson" Version="1.0.31" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
<PackageReference Include="System.IO" Version="4.3.0" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Api": {
"Connection": "https://localhost:44349"
"App": {
"RestApi": "https://localhost:44349",
"ReaderFactory": "ConsoleInputReaderFactory"
},

"Logging": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Api": {
"Connection": "https://localhost:44349"
"App": {
"RestApi": "https://localhost:44349",
"ReaderFactory": "ConsoleInputReaderFactory"
},

"Logging": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Api": {
"Connection": "https://localhost:44349"
"App": {
"RestApi": "https://localhost:44349",
"ReaderFactory": "ConsoleInputReaderFactory"
},

"Logging": {
Expand Down

0 comments on commit 249ad07

Please sign in to comment.