1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Data ;
4
+ using System . Data . Common ;
5
+ using System . Data . OracleClient ;
3
6
using System . Linq ;
7
+ using System . Text ;
4
8
using System . Threading . Tasks ;
9
+ using System . Transactions ;
5
10
using Microsoft . AspNetCore . Builder ;
6
11
using Microsoft . AspNetCore . Hosting ;
7
12
using Microsoft . AspNetCore . HttpsPolicy ;
11
16
using Microsoft . Extensions . Hosting ;
12
17
using Microsoft . Extensions . Logging ;
13
18
using Microsoft . OpenApi . Models ;
14
-
15
-
16
- using System . Text ;
17
- using System . Data . OracleClient ;
18
- using System . Data ;
19
19
using Newtonsoft . Json ;
20
20
using Oracle . ManagedDataAccess . Client ;
21
21
22
- using System . Data . Common ;
23
- using System . Transactions ;
24
-
25
22
namespace inventory_dotnet
26
23
{
27
24
public class Startup
@@ -36,12 +33,17 @@ public Startup(IConfiguration configuration)
36
33
// This method gets called by the runtime. Use this method to add services to the container.
37
34
public void ConfigureServices ( IServiceCollection services )
38
35
{
39
-
40
36
services . AddControllers ( ) ;
41
- services . AddSwaggerGen ( c =>
42
- {
43
- c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "inventory_dotnet" , Version = "v1" } ) ;
44
- } ) ;
37
+ services
38
+ . AddSwaggerGen ( c =>
39
+ {
40
+ c
41
+ . SwaggerDoc ( "v1" ,
42
+ new OpenApiInfo {
43
+ Title = "inventory_dotnet" ,
44
+ Version = "v1"
45
+ } ) ;
46
+ } ) ;
45
47
ListenForMessages ( ) ;
46
48
}
47
49
@@ -52,7 +54,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
52
54
{
53
55
app . UseDeveloperExceptionPage ( ) ;
54
56
app . UseSwagger ( ) ;
55
- app . UseSwaggerUI ( c => c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "inventory_dotnet v1" ) ) ;
57
+ app
58
+ . UseSwaggerUI ( c =>
59
+ c
60
+ . SwaggerEndpoint ( "/swagger/v1/swagger.json" ,
61
+ "inventory_dotnet v1" ) ) ;
56
62
}
57
63
58
64
app . UseHttpsRedirection ( ) ;
@@ -61,66 +67,135 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
61
67
62
68
app . UseAuthorization ( ) ;
63
69
64
- app . UseEndpoints ( endpoints =>
65
- {
66
- endpoints . MapControllers ( ) ;
67
- } ) ;
70
+ app
71
+ . UseEndpoints ( endpoints =>
72
+ {
73
+ endpoints . MapControllers ( ) ;
74
+ } ) ;
68
75
}
69
76
70
77
public String ListenForMessages ( )
71
78
{
72
79
//Other options include...
73
80
// using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
74
81
// DbProviderFactory factory = DbProviderFactories.GetFactory("Oracle.ManagedDataAccess.Client"); DbCommand oracleCommand = factory.CreateCommand();
75
- String tnsAdmin = Environment . GetEnvironmentVariable ( "TNS_ADMIN" ) ;
82
+ String tnsAdmin = Environment . GetEnvironmentVariable ( "TNS_ADMIN" ) ;
76
83
OracleConfiguration . WalletLocation = tnsAdmin ;
77
- string connString = "User Id=INVENTORYUSER;Password=" + Environment . GetEnvironmentVariable ( "dbpassword" ) +
78
- ";Data Source=" + Environment . GetEnvironmentVariable ( "INVENTORY_PDB_NAME" ) + ";" ;
79
- Console . WriteLine ( "tnsAdmin:" + tnsAdmin + " connString:" + connString ) ;
80
- using ( OracleConnection connection = new OracleConnection ( connString ) )
81
- { try
84
+ string connString =
85
+ "User Id=INVENTORYUSER;Password=" +
86
+ Environment . GetEnvironmentVariable ( "dbpassword" ) +
87
+ ";Data Source=" +
88
+ Environment . GetEnvironmentVariable ( "INVENTORY_PDB_NAME" ) +
89
+ ";" ;
90
+ Console
91
+ . WriteLine ( "tnsAdmin:" +
92
+ tnsAdmin +
93
+ " connString:" +
94
+ connString ) ;
95
+ using (
96
+ OracleConnection connection = new OracleConnection ( connString )
97
+ )
98
+ {
99
+ try
82
100
{
83
101
connection . Open ( ) ;
84
- while ( true ) {
102
+ while ( true )
103
+ {
85
104
Console . WriteLine ( "connection:" + connection ) ;
86
- OracleCommand oracleCommand = new OracleCommand ( ) ;
87
- oracleCommand . Connection = connection ;
88
- oracleCommand . CommandText = "dequeueOrderMessage" ;
89
- oracleCommand . CommandType = CommandType . StoredProcedure ;
90
- OracleParameter p_orderInfoParam = new OracleParameter ( "p_orderInfo" , OracleDbType . Varchar2 , 32767 ) ;
105
+ //dequeue from order queues (out param)
106
+ OracleCommand orderReceiveMessageCommand = new OracleCommand ( ) ;
107
+ orderReceiveMessageCommand . Connection = connection ;
108
+ orderReceiveMessageCommand . CommandText = "dequeueOrderMessage" ;
109
+ orderReceiveMessageCommand . CommandType = CommandType . StoredProcedure ;
110
+ OracleParameter p_orderInfoParam =
111
+ new OracleParameter ( "p_orderInfo" ,
112
+ OracleDbType . Varchar2 ,
113
+ 32767 ) ;
91
114
p_orderInfoParam . Direction = ParameterDirection . Output ;
92
- oracleCommand . Parameters . Add ( p_orderInfoParam ) ;
93
- oracleCommand . ExecuteNonQuery ( ) ;
94
- Order order = JsonConvert . DeserializeObject < Order > ( "" + oracleCommand . Parameters [ "p_orderInfo" ] . Value ) ;
95
- System . Console . WriteLine ( "order.itemid inventorychecked sendmessage for {0}" , order . orderid ) ;
96
-
97
- OracleCommand checkInventoryCommand = new OracleCommand ( ) ;
98
- checkInventoryCommand . Connection = connection ;
99
- checkInventoryCommand . CommandText =
100
- @"update inventory set inventorycount = inventorycount - 1 where inventoryid = " + order . itemid +
101
- " and inventorycount > 0 returning inventorylocation into ?" ;
102
- OracleParameter p_inventoryCheckParam = new OracleParameter ( "p_orderInfo" , OracleDbType . Varchar2 , 32767 ) ;
103
- p_inventoryCheckParam . Direction = ParameterDirection . Output ;
104
- oracleCommand . Parameters . Add ( p_orderInfoParam ) ;
105
- int retVal = checkInventoryCommand . ExecuteNonQuery ( ) ;
106
- Console . WriteLine ( "Rows to be affected by checkInventoryCommand: {0}" , retVal ) ;
107
-
115
+ orderReceiveMessageCommand . Parameters . Add ( p_orderInfoParam ) ;
116
+ orderReceiveMessageCommand . ExecuteNonQuery ( ) ;
117
+ Order order =
118
+ JsonConvert
119
+ . DeserializeObject < Order > ( "" +
120
+ orderReceiveMessageCommand . Parameters [ "p_orderInfo" ] . Value ) ;
121
+ System
122
+ . Console
123
+ . WriteLine ( "order.itemid inventorychecked sendmessage for {0}" ,
124
+ order . orderid ) ;
125
+ // check inventory (in and out params)
126
+ OracleCommand checkInventoryReturnLocationCommand =
127
+ new OracleCommand ( ) ;
128
+ checkInventoryReturnLocationCommand . Connection = connection ;
129
+ checkInventoryReturnLocationCommand . CommandText =
130
+ "checkInventoryReturnLocation" ;
131
+ checkInventoryReturnLocationCommand . CommandType =
132
+ CommandType . StoredProcedure ;
133
+ OracleParameter p_itemIdParam =
134
+ new OracleParameter ( "p_inventoryId" ,
135
+ OracleDbType . Varchar2 ,
136
+ 32767 ) ;
137
+ p_itemIdParam . Direction =
138
+ ParameterDirection . Input ;
139
+ p_itemIdParam . Value = order . itemid ;
140
+ checkInventoryReturnLocationCommand . Parameters . Add (
141
+ p_itemIdParam
142
+ ) ;
143
+ OracleParameter p_inventorylocationParam =
144
+ new OracleParameter ( "p_inventorylocation" ,
145
+ OracleDbType . Varchar2 ,
146
+ 32767 ) ;
147
+ p_inventorylocationParam . Direction = ParameterDirection . Output ;
148
+ checkInventoryReturnLocationCommand . Parameters . Add ( p_inventorylocationParam ) ;
149
+ checkInventoryReturnLocationCommand . ExecuteNonQuery ( ) ;
150
+
151
+ // direct query version (ie not using sproc)...
152
+ // checkInventoryCommand.CommandText =
153
+ // @"update inventory set inventorycount = inventorycount - 1 where inventoryid = " +
154
+ // order.itemid +
155
+ // " and inventorycount > 0 returning inventorylocation into ?";
156
+ // OracleParameter p_inventoryCheckParam =
157
+ // new OracleParameter("p_orderInfo",
158
+ // OracleDbType.Varchar2,
159
+ // 32767);
160
+ // p_inventoryCheckParam.Direction =
161
+ // ParameterDirection.Output;
162
+ // oracleCommand.Parameters.Add (p_orderInfoParam);
163
+ // int retVal = checkInventoryCommand.ExecuteNonQuery();
164
+ // Console
165
+ // .WriteLine("Rows to be affected by checkInventoryCommand: {0}",
166
+ // retVal);
167
+
168
+ //inventory status object creation (using inventory location deteremined from query above)
108
169
Inventory inventory = new Inventory ( ) ;
109
- inventory . inventorylocation = "Philly" ;
170
+ var inventoryLocation = "" + checkInventoryReturnLocationCommand . Parameters [ "p_inventorylocation" ] . Value ;
171
+ inventory . inventorylocation = inventoryLocation . Equals ( "null" ) ? "inventorydoesnotexist" : inventoryLocation ;
110
172
inventory . itemid = order . itemid ;
111
173
inventory . orderid = order . orderid ;
112
- inventory . suggestiveSale = "beer" ;
113
- string inventoryJSON = JsonConvert . SerializeObject ( inventory ) ;
114
- System . Console . WriteLine ( "order.itemid inventoryJSON {0}" , inventoryJSON ) ;
115
-
116
- OracleCommand inventorySendMessageCommand = new OracleCommand ( ) ;
174
+ inventory . suggestiveSale = inventoryLocation . Equals ( "null" ) ? "" : "beer" ;
175
+ string inventoryJSON =
176
+ JsonConvert . SerializeObject ( inventory ) ;
177
+ System
178
+ . Console
179
+ . WriteLine ( "order.itemid inventoryJSON {0}" ,
180
+ inventoryJSON ) ;
181
+ //enqueue to inventory queue (in param)
182
+ OracleCommand inventorySendMessageCommand =
183
+ new OracleCommand ( ) ;
117
184
inventorySendMessageCommand . Connection = connection ;
118
- inventorySendMessageCommand . CommandText = "enqueueInventoryMessage" ;
119
- inventorySendMessageCommand . CommandType = CommandType . StoredProcedure ;
120
- OracleParameter p_inventoryInfoParam = new OracleParameter ( "p_inventoryInfo" , OracleDbType . Varchar2 , 32767 ) ;
121
- p_inventoryInfoParam . Direction = ParameterDirection . Input ;
185
+ inventorySendMessageCommand . CommandText =
186
+ "enqueueInventoryMessage" ;
187
+ inventorySendMessageCommand . CommandType =
188
+ CommandType . StoredProcedure ;
189
+ OracleParameter p_inventoryInfoParam =
190
+ new OracleParameter ( "p_inventoryInfo" ,
191
+ OracleDbType . Varchar2 ,
192
+ 32767 ) ;
193
+ p_inventoryInfoParam . Direction =
194
+ ParameterDirection . Input ;
122
195
p_inventoryInfoParam . Value = inventoryJSON ;
123
- inventorySendMessageCommand . Parameters . Add ( p_inventoryInfoParam ) ;
196
+ inventorySendMessageCommand . Parameters . Add (
197
+ p_inventoryInfoParam
198
+ ) ;
124
199
inventorySendMessageCommand . ExecuteNonQuery ( ) ;
125
200
}
126
201
}
@@ -133,6 +208,5 @@ public String ListenForMessages()
133
208
}
134
209
return "complete" ;
135
210
}
136
-
137
211
}
138
212
}
0 commit comments