11using System ;
22using System . Collections . Generic ;
3+ using System . Data ;
4+ using System . Data . Common ;
5+ using System . Data . OracleClient ;
36using System . Linq ;
7+ using System . Text ;
48using System . Threading . Tasks ;
9+ using System . Transactions ;
510using Microsoft . AspNetCore . Builder ;
611using Microsoft . AspNetCore . Hosting ;
712using Microsoft . AspNetCore . HttpsPolicy ;
1116using Microsoft . Extensions . Hosting ;
1217using Microsoft . Extensions . Logging ;
1318using Microsoft . OpenApi . Models ;
14-
15-
16- using System . Text ;
17- using System . Data . OracleClient ;
18- using System . Data ;
1919using Newtonsoft . Json ;
2020using Oracle . ManagedDataAccess . Client ;
2121
22- using System . Data . Common ;
23- using System . Transactions ;
24-
2522namespace inventory_dotnet
2623{
2724 public class Startup
@@ -36,12 +33,17 @@ public Startup(IConfiguration configuration)
3633 // This method gets called by the runtime. Use this method to add services to the container.
3734 public void ConfigureServices ( IServiceCollection services )
3835 {
39-
4036 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+ } ) ;
4547 ListenForMessages ( ) ;
4648 }
4749
@@ -52,7 +54,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5254 {
5355 app . UseDeveloperExceptionPage ( ) ;
5456 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" ) ) ;
5662 }
5763
5864 app . UseHttpsRedirection ( ) ;
@@ -61,66 +67,135 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6167
6268 app . UseAuthorization ( ) ;
6369
64- app . UseEndpoints ( endpoints =>
65- {
66- endpoints . MapControllers ( ) ;
67- } ) ;
70+ app
71+ . UseEndpoints ( endpoints =>
72+ {
73+ endpoints . MapControllers ( ) ;
74+ } ) ;
6875 }
6976
7077 public String ListenForMessages ( )
7178 {
7279 //Other options include...
7380 // using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
7481 // 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" ) ;
7683 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
82100 {
83101 connection . Open ( ) ;
84- while ( true ) {
102+ while ( true )
103+ {
85104 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 ) ;
91114 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)
108169 Inventory inventory = new Inventory ( ) ;
109- inventory . inventorylocation = "Philly" ;
170+ var inventoryLocation = "" + checkInventoryReturnLocationCommand . Parameters [ "p_inventorylocation" ] . Value ;
171+ inventory . inventorylocation = inventoryLocation . Equals ( "null" ) ? "inventorydoesnotexist" : inventoryLocation ;
110172 inventory . itemid = order . itemid ;
111173 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 ( ) ;
117184 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 ;
122195 p_inventoryInfoParam . Value = inventoryJSON ;
123- inventorySendMessageCommand . Parameters . Add ( p_inventoryInfoParam ) ;
196+ inventorySendMessageCommand . Parameters . Add (
197+ p_inventoryInfoParam
198+ ) ;
124199 inventorySendMessageCommand . ExecuteNonQuery ( ) ;
125200 }
126201 }
@@ -133,6 +208,5 @@ public String ListenForMessages()
133208 }
134209 return "complete" ;
135210 }
136-
137211 }
138212}
0 commit comments