ExchangeRateService and api

This commit is contained in:
Adam 2024-07-27 18:19:56 +02:00
parent 4b7692c0e6
commit 6e345430cb
14 changed files with 223 additions and 11 deletions

View File

@ -69,6 +69,7 @@ namespace TIAMMobileApp
builder.Services.AddSingleton<SignalRService>();
builder.Services.AddSingleton<SumupService>();
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
builder.Services.AddScoped<ExchangeRateService>();
builder.Services.AddAuthorizationCore();
builder.Services.Configure<AnimationOptions>(Guid.NewGuid().ToString(), c => { });
return builder.Build();

View File

@ -38,7 +38,7 @@
NullText="New password"
BindValueMode="BindValueMode.OnDelayedInput"
InputDelay="300"
Password="true"
CssClass="form-field"
/>
<DxTextBox @bind-Text="@ConfirmNewPassword"
@ -46,7 +46,7 @@
NullText="Confirm new password"
BindValueMode="BindValueMode.OnDelayedInput"
InputDelay="300"
Password="true"
CssClass="form-field" />
</div>
<div class="col-3 col-md-2">

View File

@ -68,7 +68,7 @@
<Animation Effect="@Effect.FadeIn" Speed="@Speed.Fast" Delay="@TimeSpan.FromMilliseconds(250)">
<div class="card">
<DxTabs>+-
<DxTabs>
<DxTabPage Text="DataGrid">
<div class="d-flex flex-column mb-4 pb-2">

View File

@ -58,7 +58,14 @@
<NavLink href="@($"mytransfers/{Guid.Parse("108E5A63-AA9E-47BE-ACFA-00306FFC5215")}")">
My trasnfers
My transfers
</NavLink>
</li>
<li>
<NavLink href="exchange-rate">
Exchange Rate
</NavLink>
</li>
<li></li>

View File

@ -0,0 +1,32 @@
<!-- Pages/ExchangeRate.razor -->
@page "/exchange-rate"
@using TIAMWebApp.Shared.Application.Models
@using TIAMWebApp.Shared.Application.Services
@inject ExchangeRateService ExchangeRateService
<h3>Exchange Rate</h3>
@if (exchangeRate == null)
{
<p><em>Loading...</em></p>
}
else
{
<p>Current EUR to HUF exchange rate: @exchangeRate.EURtoHUF</p>
<input type="number" @bind="exchangeRate.EURtoHUF" step="0.01" />
<button @onclick="SaveExchangeRate">Save</button>
}
@code {
private ExchangeRate exchangeRate;
protected override async Task OnInitializedAsync()
{
exchangeRate = await ExchangeRateService.GetExchangeRateAsync();
}
private async Task SaveExchangeRate()
{
await ExchangeRateService.SetExchangeRateAsync(exchangeRate);
}
}

View File

@ -46,6 +46,7 @@ builder.Services.AddSingleton<SignalRService>();
builder.Services.AddSingleton<SumupService>();
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<ExchangeRateService>();
//builder.Services.AddScoped<BrowserConsoleLogWriter>();
//WebSpecific end

View File

@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;
using AyCode.Core.Enums;
using AyCode.Core.Extensions;
using TIAM.Database.DataLayers.Admins;
using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Users;
using TIAMWebApp.Shared.Application.Models;
using Product = TIAM.Entities.Products.Product;
using TIAM.Entities.Addresses;
using TIAM.Entities.Profiles;
using AyCode.Core.Loggers;
using AyCode.Entities;
using AyCode.Services.SignalRs;
using AyCode.Utils.Extensions;
using TIAM.Entities.Drivers;
using TIAM.Services;
using TIAMWebApp.Server.Services;
using GoogleApi.Entities.Search.Video.Common;
namespace TIAMWebApp.Server.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
public class ExchangeRateAPIController(ExchangeRateService _service,IEnumerable<IAcLogWriterBase> logWriters) : ControllerBase
{
private readonly TIAM.Core.Loggers.Logger<ExchangeRateAPIController> _logger = new(logWriters.ToArray());
[AllowAnonymous]
[HttpGet]
[Route(APIUrls.GetExchangeRateRouteName)]
//[SignalR(SignalRTags.GetProfileById)]
public async Task<IActionResult> GetExchangeRate()
{
_logger.Info($@"GetExchangeRate called");
var rate = await _service.GetExchangeRateAsync();
return Ok(rate);
}
[AllowAnonymous]
[HttpPost]
[Route(APIUrls.UpdateExchangeRateRouteName)]
//[SignalR(SignalRTags.GetProfileById)]
public async Task<IActionResult> Set([FromBody] ExchangeRate rate)
{
_logger.Info($@"GetServiceProviderById called with rate: {rate.EURtoHUF}");
await _service.SetExchangeRateAsync(rate);
return Ok();
}
}
}

View File

@ -68,6 +68,7 @@ namespace TIAMWebApp.Server.Controllers
//[Authorize]
//[HttpGet]
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
[NonAction]
[SignalR(SignalRTags.GetTransferDestinationById)]
public async Task<TransferDestination?> GetTransferDestinationById(Guid transferDestinationId)
{
@ -139,6 +140,7 @@ namespace TIAMWebApp.Server.Controllers
//[Authorize]
//[HttpGet]
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
[NonAction]
[SignalR(SignalRTags.RemoveTransferDestination)]
public async Task<TransferDestination?> RemoveTransferDestination([FromBody] TransferDestination transferDestination)
{
@ -396,17 +398,36 @@ namespace TIAMWebApp.Server.Controllers
var from = destList.FirstOrDefault(x => x.AddressString == transfer.FromAddress);
var to = destList.FirstOrDefault(x => x.AddressString == transfer.ToAddress);
//TODO
if (!transfer.ProductId.IsNullOrEmpty())
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
////TODO
//if (!transfer.ProductId.IsNullOrEmpty())
// transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
Product? product = null;
//TODO
if (!transfer.ProductId.IsNullOrEmpty())
{
product = await _adminDal.GetProductByIdAsync((Guid)transfer.ProductId);
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
}
await _adminDal.AddTransferAsync(transfer);
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
if (transfer.Price != null && transfer.Price > 0 && product != null)
{
if (product.ServiceProvider.CommissionPercent != null)
{
transfer.Revenue = transfer.Price * product.ServiceProvider.CommissionPercent / 100;
}
else
{
transfer.Revenue = 0;
}
}
await _adminDal.AddTransferAsync(transfer);
_logger.Info($"Created transfer, send emailMessage!!!");
var message = new MessageSenderModel<EmailMessage>();
message.Message = new EmailMessage();
message.Message.Id = Guid.NewGuid();
message.Message.Subject = "[Tour I Am] New transfer in Budapest";
message.Message.ContextId = transfer.Id;
@ -498,7 +519,7 @@ namespace TIAMWebApp.Server.Controllers
{
if(product.ServiceProvider.CommissionPercent!=null)
{
transfer.Revenue = transfer.Price * product.ServiceProvider.CommissionPercent;
transfer.Revenue = transfer.Price * product.ServiceProvider.CommissionPercent / 100;
}
else
{
@ -750,6 +771,7 @@ namespace TIAMWebApp.Server.Controllers
return result;
}
[NonAction]
//[Authorize]
//[HttpGet]
//[Route(APIUrls.GetTransferDriverRouteName)]

View File

@ -47,6 +47,7 @@ builder.Services.AddScoped<TransferDataAPIController>();
builder.Services.AddScoped<MessageAPIController>();
builder.Services.AddScoped<ProfileAPIController>();
builder.Services.AddScoped<LoggerApiController>();
builder.Services.AddSingleton<ExchangeRateService>();
builder.Services.AddSignalR(options => options.MaximumReceiveMessageSize = 102400 * 1024);//.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData));

View File

@ -0,0 +1,34 @@
using System.IO;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Hosting;
using TIAMWebApp.Shared.Application.Models;
namespace TIAMWebApp.Server.Services
{
public class ExchangeRateService
{
private readonly IWebHostEnvironment _env;
private readonly string _filePath;
public ExchangeRateService(IWebHostEnvironment env)
{
_env = env;
_filePath = Path.Combine(_env.WebRootPath, "ExchangeRate.xml");
}
public async Task<ExchangeRate> GetExchangeRateAsync()
{
using var stream = new FileStream(_filePath, FileMode.Open);
var serializer = new XmlSerializer(typeof(ExchangeRate));
return (ExchangeRate)serializer.Deserialize(stream);
}
public async Task SetExchangeRateAsync(ExchangeRate rate)
{
using var stream = new FileStream(_filePath, FileMode.Create);
var serializer = new XmlSerializer(typeof(ExchangeRate));
serializer.Serialize(stream, rate);
}
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<ExchangeRate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EURtoHUF>380</EURtoHUF>
</ExchangeRate>

View File

@ -19,6 +19,7 @@ namespace TIAMWebApp.Shared.Application.Models
public const string FileAPI = BaseUrlWithSlashAndVersion + "FileAPI/";
public const string MessageAPI = BaseUrlWithSlashAndVersion + "MessageAPI/";
public const string PaymentAPI = BaseUrlWithSlashAndVersion + "PaymentAPI/";
public const string ExchangeRateAPI = BaseUrlWithSlashAndVersion + "ExchangeRateAPI/";
public const string AddLogItemRouteName = "AddLogItem";
public const string AddLogItem = LoggerApi + AddLogItemRouteName;
@ -132,7 +133,7 @@ namespace TIAMWebApp.Shared.Application.Models
public const string GetTransferDriverRouteName = "GetTransfersByDriverId";
public const string GetTransferDriver = TransferDataAPI + GetTransferDriverRouteName;
public const string GetTransferDriversByTransferIdRouteName = "GetTransfersByDriverId";
public const string GetTransferDriversByTransferIdRouteName = "GetTransferDriversByTransferId";
public const string GetTransferDriversByTransferId = TransferDataAPI + GetTransferDriversByTransferIdRouteName;
//serviceprovider
@ -268,5 +269,10 @@ namespace TIAMWebApp.Shared.Application.Models
public const string GetPaymentRouteName = "GetPaymentById";
public const string GetPaymentById = PaymentAPI + GetPaymentRouteName;
public const string UpdateExchangeRateRouteName = "UpdateExchangeRate";
public const string UpdateExchangeRate = ExchangeRateAPI + UpdateExchangeRateRouteName;
public const string GetExchangeRateRouteName = "GetExchangeRate";
public const string GetExchangeRate = ExchangeRateAPI + GetExchangeRateRouteName;
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TIAMWebApp.Shared.Application.Models
{
public class ExchangeRate
{
public decimal EURtoHUF { get; set; }
}
}

View File

@ -0,0 +1,35 @@
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.ClientSide;
namespace TIAMWebApp.Shared.Application.Services
{
public class ExchangeRateService
{
private readonly HttpClient _httpClient;
public ExchangeRateService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<ExchangeRate> GetExchangeRateAsync()
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetExchangeRate}";
return await _httpClient.GetFromJsonAsync<ExchangeRate>(url);
}
public async Task SetExchangeRateAsync(ExchangeRate rate)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.UpdateExchangeRate}";
await _httpClient.PostAsJsonAsync(url, rate);
}
}
}