Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Abstraction</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DataAccessLayer.Models\DataAccessLayer.Models.csproj"/>
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions BusinessLogicLayer.Abstraction/Interfaces/ICurrencyProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DataAccessLayer.Models.Entities;

namespace Abstraction.Interfaces
{
public interface ICurrencyProvider
{
Task<IEnumerable<CurrencyExchangeRate>> GetExchangeRateAsync();
}
}
6 changes: 6 additions & 0 deletions BusinessLogicLayer.Abstraction/Interfaces/IOfficialSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Abstraction.Interfaces
{
public interface IOfficialSource : ICurrencyProvider
{
}
}
10 changes: 10 additions & 0 deletions BusinessLogicLayer.Abstraction/Interfaces/IResponseClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using DataAccessLayer.Models.Entities;

namespace Abstraction.Interfaces
{
public interface IResponseClient
{
Task<string> SendResponse(Updates updates);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Abstraction.Interfaces
{
public interface IUnofficialSource : ICurrencyProvider
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Flurl.Http" Version="2.4.2"/>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2"/>
<PackageReference Include="VkNet" Version="1.44.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BusinessLogicLayer.Abstraction\BusinessLogicLayer.Abstraction.csproj"/>
<ProjectReference Include="..\BusinessLogicLayer.Objects\BusinessLogicLayer.Objects.csproj"/>
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Abstraction.Interfaces;
using AutoMapper;
using BusinessLogicLayer.Objects.Dtos;
using BusinessLogicLayer.Objects.Dtos.Cbr;
using DataAccessLayer.Models.Entities;
using Flurl.Http;
using Microsoft.Extensions.Configuration;

namespace BusinessLogicLayer.Implementation.Services
{
public class CbrExchangeRateProvider : IOfficialSource
{
private readonly IMapper _mapper;

private readonly IConfiguration _configuration;

public CbrExchangeRateProvider(IMapper mapper, IConfiguration configuration)
{
_mapper = mapper;

_configuration = configuration;
}


public async Task<IEnumerable<CurrencyExchangeRate>> GetExchangeRateAsync()
{
CbrResponse cbrResponse = await _configuration["Config:LinqCbr"]
.GetJsonAsync<CbrResponse>();

Currency[] currencies =
{
cbrResponse.Currencies.EUR,
cbrResponse.Currencies.UAH,
cbrResponse.Currencies.USD
};
//
// return currencies.Select(currency => new CurrencyDataResponse
// {
// DataSource = SourceType.Official,
// Date = cbrResponse.Date,
// Code = currency.CharCode,
// Name = currency.Name,
// Value = currency.Value / currency.Nominal
// })
// .ToList();

return _mapper.Map<IEnumerable<CurrencyExchangeRate>>(currencies,
options => options.Items[CbrConstants.Date] = cbrResponse.Date);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abstraction.Interfaces;
using BusinessLogicLayer.Objects.Dtos.Currate;
using DataAccessLayer.Models.Entities;
using DataAccessLayer.Models.Enums;
using Flurl.Http;
using Microsoft.Extensions.Configuration;

namespace BusinessLogicLayer.Implementation.Services
{
public class CurrateCurrencyProviderService : IUnofficialSource
{
private readonly IConfiguration _configuration;

public CurrateCurrencyProviderService(IConfiguration configuration)
{
_configuration = configuration;
}

public async Task<IEnumerable<CurrencyExchangeRate>> GetExchangeRateAsync()
{
CurrateResponse currateResponse =
await _configuration["Config:LinqCurrate"]
.GetJsonAsync<CurrateResponse>();

return new[]
{
new CurrencyExchangeRate
{
DataSource = SourceType.Exchange,

Code = "EUR",

Date = DateTime.UtcNow,

Name = "Евро",

Value = currateResponse.Data.EURRUB
},

new CurrencyExchangeRate
{
DataSource = SourceType.Exchange,

Code = "USD",

Date = DateTime.UtcNow,

Name = "Доллар США",

Value = currateResponse.Data.USDRUB
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abstraction.Interfaces;
using BusinessLogicLayer.Objects.Dtos;
using DataAccessLayer.Models.Entities;
using Microsoft.Extensions.Configuration;
using VkNet.Abstractions;
using VkNet.Model;
using VkNet.Model.RequestParams;
using VkNet.Utils;

namespace BusinessLogicLayer.Implementation.Services
{
public class VkResponseClientService : IResponseClient
{
private readonly IConfiguration _configuration;

private readonly IVkApi _vkApi;

private readonly ICurrencyProvider _currencyProviderService;

public VkResponseClientService(IVkApi vkApi, IConfiguration configuration,
ICurrencyProvider currencyProviderService)
{
_vkApi = vkApi;
_configuration = configuration;
_currencyProviderService = currencyProviderService;
}

public async Task<string> SendResponse(Updates updates)
{
if (updates == null)
{
return string.Empty;
}
// Проверяем, что находится в поле "type"

switch (updates.Type)
{
// Если это уведомление для подтверждения адреса
case VkMessagesTypes.Confirmation:
// Отправляем строку для подтверждения
return _configuration["Config:Confirmation"];

case VkMessagesTypes.MessageNew:
{
// Десериализация
var vkMessage = Message.FromJson(new VkResponse(updates.Object));

// Отправим в ответ полученный от пользователя текст
IEnumerable<CurrencyExchangeRate> currencies = await _currencyProviderService
.GetExchangeRateAsync();

string responseMessage = currencies.Aggregate(string.Empty,
(previous, current) => previous + $"{current.Name}: {current.Value} ₽\n");

if (vkMessage.PeerId != null)
{
_vkApi.Messages.Send(new MessagesSendParams
{
RandomId = new DateTime().Millisecond,

PeerId = vkMessage.PeerId.Value,

Message = responseMessage
});
}

return responseMessage;
}
}

return string.Empty;
}
}
}
11 changes: 11 additions & 0 deletions BusinessLogicLayer.Objects/BusinessLogicLayer.Objects.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2"/>
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions BusinessLogicLayer.Objects/Constants/CbrConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BusinessLogicLayer.Objects.Dtos
{
public class CbrConstants
{
public const string Date = "Date";
}
}
9 changes: 9 additions & 0 deletions BusinessLogicLayer.Objects/Constants/VkMessagesTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BusinessLogicLayer.Objects.Dtos
{
public class VkMessagesTypes
{
public const string Confirmation = "confirmation";

public const string MessageNew = "message_new";
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleAppCourseCurrency
namespace BusinessLogicLayer.Objects.Dtos.Cbr
{
public class Valute
public class CbrCurrencies
{
public Currency USD { get; set; }

public Currency UAH { get; set; }

public Currency EUR { get; set; }
}
}
}
18 changes: 18 additions & 0 deletions BusinessLogicLayer.Objects/Dtos/Cbr/CbrResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Newtonsoft.Json;

namespace BusinessLogicLayer.Objects.Dtos.Cbr
{
public class CbrResponse
{
public DateTime Date { get; set; }

public DateTime PreviousDate { get; set; }

/// <summary>
/// Валюты
/// </summary>
[JsonProperty("Valute")]
public CbrCurrencies Currencies { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleAppCourseCurrency
namespace BusinessLogicLayer.Objects.Dtos.Cbr
{
public class Currency
{
Expand All @@ -19,8 +15,5 @@ public class Currency
public double Value { get; set; }

public double Previous { get; set; }

public override string ToString() => $"Курс {Name}: {Value / Nominal} ₽";
}

}
}
15 changes: 15 additions & 0 deletions BusinessLogicLayer.Objects/Dtos/Currate/CurrateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BusinessLogicLayer.Objects.Dtos.Currate
{
public class CurrateResponse
{
public Currencies Data { get; set; }
// {
// "status": "200",
// "message": "rates",
// "data": {
// "EURRUB": "71.3846",
// "USDRUB": "58.059"
// }
// }
}
}
9 changes: 9 additions & 0 deletions BusinessLogicLayer.Objects/Dtos/Currate/Currencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BusinessLogicLayer.Objects.Dtos.Currate
{
public class Currencies
{
public double EURRUB { get; set; }

public double USDRUB { get; set; }
}
}
Loading