Skip to content

Instantly share code, notes, and snippets.

@mortezadalil
Created March 24, 2022 15:45
Show Gist options
  • Save mortezadalil/d3d55092e4784bbc778cac68f103d12a to your computer and use it in GitHub Desktop.
Save mortezadalil/d3d55092e4784bbc778cac68f103d12a to your computer and use it in GitHub Desktop.
using Dapper;
using Discount.Api.Grpc;
using Grpc.Core;
using Npgsql;
namespace Discount.Api.GrpcService.Server
{
public class GetPriceService : Price.PriceBase
{
private readonly IConfiguration _configuration;
public GetPriceService(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public override async Task<PriceReply> GetPrice(PriceRequest request, ServerCallContext context)
{
using var connection = new NpgsqlConnection
(_configuration.GetValue<string>("DatabaseSettings:ConnectionString"));
var prices = await connection.QueryAsync<Discount.Api.Models.Price>
("SELECT * FROM \"Price\" WHERE \"ProductId\" = @ProductId Order By \"CreatedDate\" Desc ", new { ProductId = request.Id });
if (prices.Count() == 0)
{
return await Task.FromResult(new PriceReply
{
Id = request.Id,
ErrorMessage = "Price Not Found"
});
}
return await Task.FromResult(new PriceReply
{
Id = request.Id,
Amount = prices.FirstOrDefault().Amount,
CurrentAmount = prices.FirstOrDefault().CurrentAmount,
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment