Created
March 24, 2022 15:45
-
-
Save mortezadalil/d3d55092e4784bbc778cac68f103d12a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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