ApiClient
This commit is contained in:
71
src/ApiClient/Grpc/GrpcSmsClient.cs
Normal file
71
src/ApiClient/Grpc/GrpcSmsClient.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Contracts;
|
||||
using Contracts.Menu;
|
||||
using Contracts.Responses;
|
||||
using Domain.Entities;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Net.Client;
|
||||
using Sms.Test;
|
||||
|
||||
namespace ApiClient.Grpc;
|
||||
|
||||
public sealed class GrpcSmsClient : ISmsClient, IDisposable
|
||||
{
|
||||
private readonly GrpcChannel _channel;
|
||||
private readonly SmsTestService.SmsTestServiceClient _client;
|
||||
|
||||
public GrpcSmsClient(GrpcSmsClientOptions options)
|
||||
{
|
||||
_channel = GrpcChannel.ForAddress(options.Address);
|
||||
_client = new SmsTestService.SmsTestServiceClient(_channel);
|
||||
}
|
||||
|
||||
public async Task<GetMenuApiResponse> GetMenuAsync(bool withPrice = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await _client.GetMenuAsync(new BoolValue { Value = withPrice }, cancellationToken: cancellationToken);
|
||||
|
||||
return new GetMenuApiResponse
|
||||
{
|
||||
Command = Commands.GetMenu,
|
||||
Success = response.Success,
|
||||
ErrorMessage = response.ErrorMessage,
|
||||
Data = response.Success
|
||||
? new GetMenuData { MenuItems = response.MenuItems.Select(ToDish).ToList() }
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<SendOrderApiResponse> SendOrderAsync(Domain.Entities.Order order, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var grpcOrder = new Sms.Test.Order { Id = order.Id.ToString() };
|
||||
grpcOrder.OrderItems.AddRange(order.Items.Select(item => new Sms.Test.OrderItem
|
||||
{
|
||||
Id = item.Id,
|
||||
Quantity = (double)item.Quantity,
|
||||
}));
|
||||
|
||||
var response = await _client.SendOrderAsync(grpcOrder, cancellationToken: cancellationToken);
|
||||
|
||||
return new SendOrderApiResponse
|
||||
{
|
||||
Command = Commands.SendOrder,
|
||||
Success = response.Success,
|
||||
ErrorMessage = response.ErrorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_channel.Dispose();
|
||||
}
|
||||
|
||||
private static Dish ToDish(MenuItem item) => new()
|
||||
{
|
||||
Id = item.Id,
|
||||
Article = item.Article,
|
||||
Name = item.Name,
|
||||
Price = (decimal)item.Price,
|
||||
IsWeighted = item.IsWeighted,
|
||||
FullPath = item.FullPath,
|
||||
Barcodes = item.Barcodes.ToList(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user