Skip to content

Commit 76ce209

Browse files
committed
discount grpc implemented and run successfully
1 parent 6ee20b9 commit 76ce209

File tree

10 files changed

+94
-12
lines changed

10 files changed

+94
-12
lines changed

Src/Services/Basket/Basket.API/Basket.API.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1818
</ItemGroup>
1919

20-
<ItemGroup>
21-
<Folder Include="Data\" />
22-
</ItemGroup>
23-
2420
<ItemGroup>
2521
<Protobuf Include="..\..\Discount\Discount.Grpc\Protos\discount.proto" GrpcServices="Client">
2622
<Link>Protos\discount.proto</Link>

Src/Services/Basket/Basket.API/Controllers/BasketController.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Basket.API.Models;
1+
using Basket.API.GrpcServices;
2+
using Basket.API.Models;
23
using Basket.API.Repository;
34
using Microsoft.AspNetCore.Http;
45
using Microsoft.AspNetCore.Mvc;
@@ -11,10 +12,12 @@ namespace Basket.API.Controllers
1112
public class BasketController : ControllerBase
1213
{
1314
private readonly IBasketRepository repository;
15+
private readonly DiscountGrpcService discountGrpcService;
1416

15-
public BasketController(IBasketRepository repository)
17+
public BasketController(IBasketRepository repository, DiscountGrpcService discountGrpcService)
1618
{
1719
this.repository = repository;
20+
this.discountGrpcService = discountGrpcService;
1821
}
1922
[HttpGet("{userName}")]
2023
[ProducesResponseType(typeof(ShoppingCart), (int)HttpStatusCode.OK)]
@@ -26,6 +29,11 @@ public async Task<ActionResult<ShoppingCart>> GetBasket(string userName)
2629
[HttpPut]
2730
public async Task<ActionResult<ShoppingCart>> UpdateBasket(ShoppingCart basket)
2831
{
32+
foreach (var item in basket.ShoppingCartItems)
33+
{
34+
var coupon = await discountGrpcService.GetDiscount(item.ProductName);
35+
item.Price -= coupon.Amount;
36+
}//Discount grpc
2937
return Ok(await repository.UpdateBasket(basket));
3038
}
3139
[HttpDelete("{userName}")]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Discount.Grpc.Protos;
2+
3+
namespace Basket.API.GrpcServices
4+
{
5+
public class DiscountGrpcService
6+
{
7+
private readonly DiscountProto.DiscountProtoClient _discountProtoClient;
8+
9+
public DiscountGrpcService(DiscountProto.DiscountProtoClient discountProtoClient)
10+
{
11+
_discountProtoClient = discountProtoClient;
12+
}
13+
public async Task<CouponModel> GetDiscount(string productName)
14+
{
15+
var protoReq = new DiscountGetRequest { ProductName=productName };
16+
return await _discountProtoClient.GetDiscountAsync(protoReq);
17+
}
18+
}
19+
}

Src/Services/Basket/Basket.API/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using Basket.API.GrpcServices;
12
using Basket.API.Repository;
3+
using Discount.Grpc.Protos;
24

35
var builder = WebApplication.CreateBuilder(args);
46

@@ -9,6 +11,11 @@
911

1012
});
1113
builder.Services.AddScoped<IBasketRepository, BasketRepository>();
14+
builder.Services.AddGrpcClient<DiscountProto.DiscountProtoClient>(options =>
15+
{
16+
options.Address = new Uri(builder.Configuration.GetValue<string>("GrpcSettings:DicountUrl"));
17+
});
18+
builder.Services.AddScoped(typeof(DiscountGrpcService));
1219

1320
builder.Services.AddControllers();
1421
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

Src/Services/Basket/Basket.API/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"CacheSettings": {
33
"ConnectionString": "localhost:6379"
44
},
5+
"GrpcSettings": {
6+
"DicountUrl": "http://localhost:5003"
7+
},
58
"Logging": {
69
"LogLevel": {
710
"Default": "Information",

Src/Services/Discount/Discount.Grpc/Discount.Grpc.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
8+
<DockerfileContext>..\..\..</DockerfileContext>
9+
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
710
</PropertyGroup>
811

912
<ItemGroup>
@@ -17,6 +20,7 @@
1720

1821
<ItemGroup>
1922
<PackageReference Include="Grpc.AspNetCore" Version="2.52.0" />
23+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
2024
<PackageReference Include="npgsql" Version="7.0.2" />
2125
<PackageReference Include="dapper" Version="2.0.123" />
2226
</ItemGroup>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
4+
WORKDIR /app
5+
EXPOSE 80
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
8+
WORKDIR /src
9+
COPY ["Services/Discount/Discount.Grpc/Discount.Grpc.csproj", "Services/Discount/Discount.Grpc/"]
10+
RUN dotnet restore "Services/Discount/Discount.Grpc/Discount.Grpc.csproj"
11+
COPY . .
12+
WORKDIR "/src/Services/Discount/Discount.Grpc"
13+
RUN dotnet build "Discount.Grpc.csproj" -c Release -o /app/build
14+
15+
FROM build AS publish
16+
RUN dotnet publish "Discount.Grpc.csproj" -c Release -o /app/publish /p:UseAppHost=false
17+
18+
FROM base AS final
19+
WORKDIR /app
20+
COPY --from=publish /app/publish .
21+
ENTRYPOINT ["dotnet", "Discount.Grpc.dll"]

Src/Services/Discount/Discount.Grpc/Properties/launchSettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
},
88
"dotnetRunMessages": true,
99
"applicationUrl": "http://localhost:5003"
10+
},
11+
"Docker": {
12+
"commandName": "Docker",
13+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
14+
"publishAllPorts": true
1015
}
1116
}
1217
}

Src/docker-compose.override.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,30 @@ services:
5353
environment:
5454
- ASPNETCORE_ENVIRONMENT=Development
5555
- "CacheSettings:ConnectionString=basketDb:6379"
56+
- "GrpcSettings:DicountUrl=http://discount.grpc:80"
5657
depends_on:
5758
- basketDb
5859
ports:
5960
- "8001:80"
6061

61-
discount.api:
62-
container_name: discount.api
62+
#discount.api:
63+
# container_name: discount.api
64+
# environment:
65+
# - ASPNETCORE_ENVIRONMENT=Development
66+
# - "PostgreDbSetting:ConnectionString=Server=discountDb;Port=5432;Database=DiscountDb;User Id=admin;Password=admin123;"
67+
# depends_on:
68+
# - discountDb
69+
# ports:
70+
# - "8002:80"
71+
72+
73+
discount.grpc:
74+
container_name: discount.grpc
6375
environment:
6476
- ASPNETCORE_ENVIRONMENT=Development
6577
- "PostgreDbSetting:ConnectionString=Server=discountDb;Port=5432;Database=DiscountDb;User Id=admin;Password=admin123;"
6678
depends_on:
6779
- discountDb
6880
ports:
69-
- "8002:80"
81+
- "8003:80"
7082

Src/docker-compose.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ services:
2727
context: .
2828
dockerfile: Services/Basket/Basket.API/Dockerfile
2929

30-
discount.api:
31-
image: ${DOCKER_REGISTRY-}discountapi
30+
#discount.api:
31+
# image: ${DOCKER_REGISTRY-}discountapi
32+
# build:
33+
# context: .
34+
# dockerfile: Services/Discount/Discount.API/Dockerfile
35+
36+
discount.grpc:
37+
image: ${DOCKER_REGISTRY-}discountgrpc
3238
build:
3339
context: .
34-
dockerfile: Services/Discount/Discount.API/Dockerfile
40+
dockerfile: Services/Discount/Discount.Grpc/Dockerfile
3541

3642
volumes:
3743
mongo_data:
3844
pg_data:
3945
pgAdmin_data:
46+

0 commit comments

Comments
 (0)