Skip to content

Commit 8843229

Browse files
authored
Merge pull request #22 from granstel/refactoring-mapping
Refactoring mapping
2 parents 2082349 + 88eea32 commit 8843229

32 files changed

+1104
-772
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using FillInTheTextBot.Services.Configuration;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace FillInTheTextBot.Api.DI
6+
{
7+
internal static class ConfigurationRegistration
8+
{
9+
internal static void AddAppConfiguration(this IServiceCollection services, IConfiguration appConfiguration)
10+
{
11+
var configuration = appConfiguration.GetSection($"{nameof(AppConfiguration)}").Get<AppConfiguration>();
12+
13+
services.AddSingleton(configuration);
14+
services.AddSingleton(configuration.HttpLog);
15+
services.AddSingleton(configuration.Redis);
16+
services.AddSingleton(configuration.DialogflowScopes);
17+
services.AddSingleton(configuration.Tracing);
18+
}
19+
}
20+
}

src/FillInTheTextBot.Api/DI/ExternalServicesRegistration.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using FillInTheTextBot.Services.Configuration;
77
using Google.Apis.Auth.OAuth2;
88
using Google.Cloud.Dialogflow.V2;
9+
using GranSteL.Helpers.Redis;
910
using GranSteL.Tools.ScopeSelector;
1011
using Grpc.Auth;
1112
using Jaeger;
@@ -28,6 +29,7 @@ internal static void AddExternalServices(this IServiceCollection services)
2829
services.AddSingleton(RegisterContextsClientScopes);
2930
services.AddSingleton(RegisterRedisClient);
3031
services.AddSingleton(RegisterTracer);
32+
services.AddSingleton(RegisterCacheService);
3133

3234
services.AddSingleton<IScopeBindingStorage, ScopeBindingStorage>();
3335
}
@@ -138,5 +140,16 @@ private static ITracer RegisterTracer(IServiceProvider provider)
138140
GlobalTracer.Register(tracer);
139141
return tracer;
140142
}
143+
144+
private static IRedisCacheService RegisterCacheService(IServiceProvider provider)
145+
{
146+
var configuration = provider.GetService<RedisConfiguration>();
147+
148+
var db = provider.GetService<IDatabase>();
149+
150+
var service = new RedisCacheService(db, configuration?.KeyPrefix);
151+
152+
return service;
153+
}
141154
}
142155
}
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using FillInTheTextBot.Services;
3-
using FillInTheTextBot.Services.Configuration;
4-
using GranSteL.Helpers.Redis;
1+
using FillInTheTextBot.Services;
52
using Microsoft.Extensions.DependencyInjection;
6-
using StackExchange.Redis;
73

84
namespace FillInTheTextBot.Api.DI
95
{
@@ -13,19 +9,6 @@ internal static void AddInternalServices(this IServiceCollection services)
139
{
1410
services.AddTransient<IConversationService, ConversationService>();
1511
services.AddScoped<IDialogflowService, DialogflowService>();
16-
17-
services.AddSingleton(RegisterCacheService);
18-
}
19-
20-
private static IRedisCacheService RegisterCacheService(IServiceProvider provider)
21-
{
22-
var configuration = provider.GetService<RedisConfiguration>();
23-
24-
var db = provider.GetService<IDatabase>();
25-
26-
var service = new RedisCacheService(db, configuration.KeyPrefix);
27-
28-
return service;
2912
}
3013
}
3114
}

src/FillInTheTextBot.Api/DI/MappingRegistration.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/FillInTheTextBot.Api/DependencyConfiguration.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/FillInTheTextBot.Api/FillInTheTextBot.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5-
<Version>1.15.1</Version>
5+
<Version>1.16.0</Version>
66
<PackageReleaseNotes>net6.0</PackageReleaseNotes>
77
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
88
</PropertyGroup>

src/FillInTheTextBot.Api/Program.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using Microsoft.AspNetCore;
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.AspNetCore;
24
using Microsoft.AspNetCore.Hosting;
35
using NLog.Web;
46
using System.Linq;
7+
using System.Reflection;
8+
using Microsoft.AspNetCore.Mvc.ApplicationParts;
59

610
namespace FillInTheTextBot.Api
711
{
@@ -19,7 +23,7 @@ public static IWebHost BuildWebHost(string[] args)
1923
var hostingStartupAssemblies = builder.GetSetting(WebHostDefaults.HostingStartupAssembliesKey) ?? string.Empty;
2024
var hostingStartupAssembliesList = hostingStartupAssemblies.Split(';');
2125

22-
var names = DependencyConfiguration.GetAssembliesNames();
26+
var names = GetAssembliesNames();
2327
var fullList = hostingStartupAssembliesList.Concat(names).Distinct().ToList();
2428
var concatenatedNames = string.Join(';', fullList);
2529

@@ -32,5 +36,15 @@ public static IWebHost BuildWebHost(string[] args)
3236
return host;
3337
}
3438

39+
private static ICollection<string> GetAssembliesNames()
40+
{
41+
var callingAssemble = Assembly.GetCallingAssembly();
42+
43+
var names = callingAssemble.GetCustomAttributes<ApplicationPartAttribute>()
44+
.Where(a => a.AssemblyName.Contains("FillInTheTextBot", StringComparison.InvariantCultureIgnoreCase))
45+
.Select(a => a.AssemblyName).ToList();
46+
47+
return names;
48+
}
3549
}
3650
}

src/FillInTheTextBot.Api/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.Logging;
88
using System;
99
using System.Linq;
10+
using FillInTheTextBot.Api.DI;
1011

1112
namespace FillInTheTextBot.Api
1213
{
@@ -34,7 +35,9 @@ public void ConfigureServices(IServiceCollection services)
3435
o.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
3536
});
3637

37-
DependencyConfiguration.Configure(services, _configuration);
38+
services.AddAppConfiguration(_configuration);
39+
services.AddInternalServices();
40+
services.AddExternalServices();
3841
}
3942

4043

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FillInTheTextBot.Models;
4+
using MailRu.Marusia.Models;
5+
using MailRu.Marusia.Models.Buttons;
6+
using MailRu.Marusia.Models.Input;
7+
using MarusiaModels = MailRu.Marusia.Models;
8+
9+
namespace FillInTheTextBot.Messengers.Marusia
10+
{
11+
public static class MarusiaMapping
12+
{
13+
public static Models.Request ToRequest(this InputModel source)
14+
{
15+
if (source == null) return null;
16+
17+
var destinaton = new Models.Request();
18+
19+
destinaton.ChatHash = source.Session?.SkillId;
20+
destinaton.UserHash = source.Session?.UserId;
21+
destinaton.Text = source.Request?.OriginalUtterance;
22+
destinaton.SessionId = source.Session?.SessionId;
23+
destinaton.NewSession = source.Session?.New;
24+
destinaton.Language = source.Meta?.Locale;
25+
destinaton.HasScreen = string.Equals(source?.Session?.Application?.ApplicationType, MarusiaModels.ApplicationTypes.Mobile);
26+
destinaton.ClientId = source?.Meta?.ClientId;
27+
destinaton.Source = Source.Marusia;
28+
destinaton.Appeal = Appeal.NoOfficial;
29+
30+
return destinaton;
31+
}
32+
33+
public static OutputModel FillOutput(this InputModel source, OutputModel destination)
34+
{
35+
if (source == null) return null;
36+
if (destination == null) return null;
37+
38+
destination.Session = source.Session;
39+
destination.Version = source.Version;
40+
41+
return destination;
42+
}
43+
44+
public static OutputModel ToOutput(this Models.Response source)
45+
{
46+
if (source == null) return null;
47+
48+
var destination = new OutputModel();
49+
50+
destination.Response = source.ToResponse();
51+
destination.Session = source.ToSession();
52+
53+
return destination;
54+
}
55+
56+
public static MarusiaModels.Response ToResponse(this Models.Response source)
57+
{
58+
if (source == null) return null;
59+
60+
var destination = new MarusiaModels.Response();
61+
62+
destination.Text = source.Text?.Replace(Environment.NewLine, "\n");
63+
destination.Tts = source.AlternativeText?.Replace(Environment.NewLine, "\n");
64+
destination.EndSession = source.Finished;
65+
destination.Buttons = source.Buttons?.ToResponseButtons();
66+
67+
return destination;
68+
}
69+
70+
public static Session ToSession(this Models.Response source)
71+
{
72+
if (source == null) return null;
73+
74+
var destination = new Session
75+
{
76+
UserId = source.UserHash
77+
};
78+
79+
return destination;
80+
}
81+
82+
public static ResponseButton[] ToResponseButtons(this ICollection<Models.Button> source)
83+
{
84+
if (source == null) return null;
85+
86+
var responseButtons = new List<ResponseButton>();
87+
88+
foreach (var button in source)
89+
{
90+
var responseButton = new ResponseButton();
91+
92+
responseButton.Title = button?.Text;
93+
responseButton.Url = !string.IsNullOrEmpty(button?.Url) ? button?.Url : null;
94+
responseButton.Hide = button.IsQuickReply;
95+
96+
responseButtons.Add(responseButton);
97+
}
98+
99+
return responseButtons.ToArray();
100+
}
101+
}
102+
}

src/FillInTheTextBot.Messengers.Marusia/MarusiaProfile.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)