Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Merge branch 'develop' of https://github.com/noordwind/Coolector into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
spetz committed Sep 30, 2016
2 parents 6f2b903 + bd96524 commit 0ae7128
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Coolector.Services.Extensions;
using Coolector.Services.Mongo;
using Coolector.Services.Nancy;
using Coolector.Services.Storage.Framework.IoC;
using Coolector.Services.Storage.Providers;
using Coolector.Services.Storage.Repositories;
using Coolector.Services.Storage.Settings;
Expand Down Expand Up @@ -40,6 +41,7 @@ protected override void ConfigureApplicationContainer(ILifetimeScope container)
builder.RegisterType<ServiceClient>().As<IServiceClient>();
builder.RegisterType<ProviderClient>().As<IProviderClient>();
builder.RegisterType<UserProvider>().As<IUserProvider>();
builder.RegisterModule<MapperModule>();
});
LifeTimeScope = container;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Autofac;
using Coolector.Common.DTO.Users;
using Coolector.Services.Storage.Mappers;

namespace Coolector.Services.Storage.Framework.IoC
{
public class MapperModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<UserMapper>().As<IMapper<UserDto>>();
}
}
}
7 changes: 7 additions & 0 deletions src/Services/Coolector.Services.Storage/Mappers/IMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Coolector.Services.Storage.Mappers
{
public interface IMapper<out T>
{
T Map(dynamic source);
}
}
21 changes: 21 additions & 0 deletions src/Services/Coolector.Services.Storage/Mappers/UserMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Coolector.Common.DTO.Users;

namespace Coolector.Services.Storage.Mappers
{
public class UserMapper : IMapper<UserDto>
{
public UserDto Map(dynamic source)
{
return new UserDto
{
Id = source.id,
UserId = source.userId,
Email = source.email,
Name = source.name,
Role = source.role,
State = source.state,
CreatedAt = source.createdAt
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Threading.Tasks;
using Coolector.Common.Types;
using Coolector.Services.Storage.Mappers;

namespace Coolector.Services.Storage.Providers
{
public interface IProviderClient
{
Task<Maybe<T>> GetAsync<T>(string url, string endpoint) where T : class;
Task<Maybe<T>> GetAsync<T>(string url, string endpoint, IMapper<T> mapper) where T : class;
Task<Maybe<T>> GetUsingStorageAsync<T>(string url, string endpoint,
Func<Task<Maybe<T>>> storageFetch, Func<T, Task> storageSave) where T : class;
Func<Task<Maybe<T>>> storageFetch, Func<T, Task> storageSave, IMapper<T> mapper) where T : class;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Threading.Tasks;
using Coolector.Common.Types;
using Coolector.Services.Storage.Mappers;

namespace Coolector.Services.Storage.Providers
{
public interface IServiceClient
{
Task<Maybe<T>> GetAsync<T>(string url, string endpoint) where T : class;
Task<Maybe<T>> GetAsync<T>(string url, string endpoint, IMapper<T> mapper) where T : class;
}
}
31 changes: 0 additions & 31 deletions src/Services/Coolector.Services.Storage/Providers/IUserProvider.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
using System.Threading.Tasks;
using Coolector.Common.DTO.Users;
using Coolector.Common.Types;
using Coolector.Services.Storage.Repositories;
using Coolector.Services.Storage.Settings;

namespace Coolector.Services.Storage.Providers
{
public interface IUserProvider
{
Task<Maybe<UserDto>> GetAsync(string userId);
}

public class UserProvider : IUserProvider
{
private readonly IUserRepository _userRepository;
private readonly IProviderClient _providerClient;
private readonly ProviderSettings _providerSettings;

public UserProvider(IUserRepository userRepository,
IProviderClient providerClient,
ProviderSettings providerSettings)
{
_userRepository = userRepository;
_providerClient = providerClient;
_providerSettings = providerSettings;
}

public async Task<Maybe<UserDto>> GetAsync(string userId)
=> await _providerClient.GetUsingStorageAsync(_providerSettings.UsersApiUrl, $"users/{userId}",
async () =>
{
var user = await _userRepository.GetByIdAsync(userId);

return user.HasValue ? user.Value : new Maybe<UserDto>();
},
async user =>
{
await _userRepository.AddAsync(user);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Coolector.Common.Types;
using Coolector.Services.Storage.Mappers;

namespace Coolector.Services.Storage.Providers
{
Expand All @@ -13,11 +14,11 @@ public ProviderClient(IServiceClient serviceClient)
_serviceClient = serviceClient;
}

public async Task<Maybe<T>> GetAsync<T>(string url, string endpoint) where T : class
=> await _serviceClient.GetAsync<T>(url, endpoint);
public async Task<Maybe<T>> GetAsync<T>(string url, string endpoint, IMapper<T> mapper) where T : class
=> await _serviceClient.GetAsync<T>(url, endpoint, mapper);

public async Task<Maybe<T>> GetUsingStorageAsync<T>(string url, string endpoint,
Func<Task<Maybe<T>>> storageFetch, Func<T, Task> storageSave) where T : class
Func<Task<Maybe<T>>> storageFetch, Func<T, Task> storageSave, IMapper<T> mapper) where T : class
{
if (storageFetch != null)
{
Expand All @@ -26,7 +27,7 @@ public async Task<Maybe<T>> GetUsingStorageAsync<T>(string url, string endpoint,
return data;
}

var response = await GetAsync<T>(url, endpoint);
var response = await GetAsync(url, endpoint, mapper);
if (response.HasNoValue)
return new Maybe<T>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Net.Http;
using System.Threading.Tasks;
using Coolector.Common.Types;
using Coolector.Services.Storage.Mappers;
using Newtonsoft.Json;

namespace Coolector.Services.Storage.Providers
{
public class ServiceClient : IServiceClient
{
public async Task<Maybe<T>> GetAsync<T>(string url, string endpoint) where T : class
public async Task<Maybe<T>> GetAsync<T>(string url, string endpoint, IMapper<T> mapper) where T : class
{
var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseAddress(url)) };
httpClient.DefaultRequestHeaders.Remove("Accept");
Expand All @@ -19,9 +20,10 @@ public async Task<Maybe<T>> GetAsync<T>(string url, string endpoint) where T : c
return new Maybe<T>();

var content = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<T>(content);
var data = JsonConvert.DeserializeObject<dynamic>(content);
var result = mapper.Map(data);

return data;
return result;
}

private string GetBaseAddress(string url) => url.EndsWith("/") ? url : $"{url}/";
Expand Down
42 changes: 42 additions & 0 deletions src/Services/Coolector.Services.Storage/Providers/UserProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using Coolector.Common.DTO.Users;
using Coolector.Common.Types;
using Coolector.Services.Storage.Mappers;
using Coolector.Services.Storage.Repositories;
using Coolector.Services.Storage.Settings;

namespace Coolector.Services.Storage.Providers
{
public class UserProvider : IUserProvider
{
private readonly IUserRepository _userRepository;
private readonly IProviderClient _providerClient;
private readonly ProviderSettings _providerSettings;
private readonly IMapper<UserDto> _mapper;

public UserProvider(IUserRepository userRepository,
IProviderClient providerClient,
ProviderSettings providerSettings,
IMapper<UserDto> mapper)
{
_userRepository = userRepository;
_providerClient = providerClient;
_providerSettings = providerSettings;
_mapper = mapper;
}

public async Task<Maybe<UserDto>> GetAsync(string userId)
=> await _providerClient.GetUsingStorageAsync(_providerSettings.UsersApiUrl, $"users/{userId}",
async () =>
{
var user = await _userRepository.GetByIdAsync(userId);

return user.HasValue ? user.Value : new Maybe<UserDto>();
},
async user =>
{
await _userRepository.AddAsync(user);
},
_mapper);
}
}
3 changes: 2 additions & 1 deletion src/Services/Coolector.Services.Storage/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"RawRabbit": "1.10.0",
"RawRabbit.vNext": "1.10.0"
"RawRabbit.vNext": "1.10.0",
"Coolector.Common": "1.0.0-*"
},
"frameworks": {
"netcoreapp1.0": {
Expand Down

0 comments on commit 0ae7128

Please sign in to comment.