File tree Expand file tree Collapse file tree 10 files changed +94
-12
lines changed Expand file tree Collapse file tree 10 files changed +94
-12
lines changed Original file line number Diff line number Diff line change 17
17
<PackageReference Include =" Swashbuckle.AspNetCore" Version =" 6.5.0" />
18
18
</ItemGroup >
19
19
20
- <ItemGroup >
21
- <Folder Include =" Data\" />
22
- </ItemGroup >
23
-
24
20
<ItemGroup >
25
21
<Protobuf Include =" ..\..\Discount\Discount.Grpc\Protos\discount.proto" GrpcServices =" Client" >
26
22
<Link >Protos\discount.proto</Link >
Original file line number Diff line number Diff line change 1
- using Basket . API . Models ;
1
+ using Basket . API . GrpcServices ;
2
+ using Basket . API . Models ;
2
3
using Basket . API . Repository ;
3
4
using Microsoft . AspNetCore . Http ;
4
5
using Microsoft . AspNetCore . Mvc ;
@@ -11,10 +12,12 @@ namespace Basket.API.Controllers
11
12
public class BasketController : ControllerBase
12
13
{
13
14
private readonly IBasketRepository repository ;
15
+ private readonly DiscountGrpcService discountGrpcService ;
14
16
15
- public BasketController ( IBasketRepository repository )
17
+ public BasketController ( IBasketRepository repository , DiscountGrpcService discountGrpcService )
16
18
{
17
19
this . repository = repository ;
20
+ this . discountGrpcService = discountGrpcService ;
18
21
}
19
22
[ HttpGet ( "{userName}" ) ]
20
23
[ ProducesResponseType ( typeof ( ShoppingCart ) , ( int ) HttpStatusCode . OK ) ]
@@ -26,6 +29,11 @@ public async Task<ActionResult<ShoppingCart>> GetBasket(string userName)
26
29
[ HttpPut ]
27
30
public async Task < ActionResult < ShoppingCart > > UpdateBasket ( ShoppingCart basket )
28
31
{
32
+ foreach ( var item in basket . ShoppingCartItems )
33
+ {
34
+ var coupon = await discountGrpcService . GetDiscount ( item . ProductName ) ;
35
+ item . Price -= coupon . Amount ;
36
+ } //Discount grpc
29
37
return Ok ( await repository . UpdateBasket ( basket ) ) ;
30
38
}
31
39
[ HttpDelete ( "{userName}" ) ]
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ using Basket . API . GrpcServices ;
1
2
using Basket . API . Repository ;
3
+ using Discount . Grpc . Protos ;
2
4
3
5
var builder = WebApplication . CreateBuilder ( args ) ;
4
6
9
11
10
12
} ) ;
11
13
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 ) ) ;
12
19
13
20
builder . Services . AddControllers ( ) ;
14
21
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
Original file line number Diff line number Diff line change 2
2
"CacheSettings" : {
3
3
"ConnectionString" : " localhost:6379"
4
4
},
5
+ "GrpcSettings" : {
6
+ "DicountUrl" : " http://localhost:5003"
7
+ },
5
8
"Logging" : {
6
9
"LogLevel" : {
7
10
"Default" : " Information" ,
Original file line number Diff line number Diff line change 4
4
<TargetFramework >net6.0</TargetFramework >
5
5
<Nullable >enable</Nullable >
6
6
<ImplicitUsings >enable</ImplicitUsings >
7
+ <DockerDefaultTargetOS >Linux</DockerDefaultTargetOS >
8
+ <DockerfileContext >..\..\..</DockerfileContext >
9
+ <DockerComposeProjectPath >..\..\..\docker-compose.dcproj</DockerComposeProjectPath >
7
10
</PropertyGroup >
8
11
9
12
<ItemGroup >
17
20
18
21
<ItemGroup >
19
22
<PackageReference Include =" Grpc.AspNetCore" Version =" 2.52.0" />
23
+ <PackageReference Include =" Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version =" 1.17.0" />
20
24
<PackageReference Include =" npgsql" Version =" 7.0.2" />
21
25
<PackageReference Include =" dapper" Version =" 2.0.123" />
22
26
</ItemGroup >
Original file line number Diff line number Diff line change
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" ]
Original file line number Diff line number Diff line change 7
7
},
8
8
"dotnetRunMessages" : true ,
9
9
"applicationUrl" : " http://localhost:5003"
10
+ },
11
+ "Docker" : {
12
+ "commandName" : " Docker" ,
13
+ "launchUrl" : " {Scheme}://{ServiceHost}:{ServicePort}" ,
14
+ "publishAllPorts" : true
10
15
}
11
16
}
12
17
}
Original file line number Diff line number Diff line change @@ -53,18 +53,30 @@ services:
53
53
environment :
54
54
- ASPNETCORE_ENVIRONMENT=Development
55
55
- " CacheSettings:ConnectionString=basketDb:6379"
56
+ - " GrpcSettings:DicountUrl=http://discount.grpc:80"
56
57
depends_on :
57
58
- basketDb
58
59
ports :
59
60
- " 8001:80"
60
61
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
63
75
environment :
64
76
- ASPNETCORE_ENVIRONMENT=Development
65
77
- " PostgreDbSetting:ConnectionString=Server=discountDb;Port=5432;Database=DiscountDb;User Id=admin;Password=admin123;"
66
78
depends_on :
67
79
- discountDb
68
80
ports :
69
- - " 8002 :80"
81
+ - " 8003 :80"
70
82
Original file line number Diff line number Diff line change @@ -27,13 +27,20 @@ services:
27
27
context : .
28
28
dockerfile : Services/Basket/Basket.API/Dockerfile
29
29
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
32
38
build :
33
39
context : .
34
- dockerfile : Services/Discount/Discount.API /Dockerfile
40
+ dockerfile : Services/Discount/Discount.Grpc /Dockerfile
35
41
36
42
volumes :
37
43
mongo_data :
38
44
pg_data :
39
45
pgAdmin_data :
46
+
You can’t perform that action at this time.
0 commit comments