Skip to content

Commit df40978

Browse files
dotnet inventorylocation query (#25)
* dotnet inventorylocation query * dotnet inventorylocation query
1 parent 98c99cc commit df40978

File tree

2 files changed

+139
-71
lines changed

2 files changed

+139
-71
lines changed

grabdish/inventory-dotnet/Startup.cs

Lines changed: 132 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Common;
5+
using System.Data.OracleClient;
36
using System.Linq;
7+
using System.Text;
48
using System.Threading.Tasks;
9+
using System.Transactions;
510
using Microsoft.AspNetCore.Builder;
611
using Microsoft.AspNetCore.Hosting;
712
using Microsoft.AspNetCore.HttpsPolicy;
@@ -11,17 +16,9 @@
1116
using Microsoft.Extensions.Hosting;
1217
using Microsoft.Extensions.Logging;
1318
using Microsoft.OpenApi.Models;
14-
15-
16-
using System.Text;
17-
using System.Data.OracleClient;
18-
using System.Data;
1919
using Newtonsoft.Json;
2020
using Oracle.ManagedDataAccess.Client;
2121

22-
using System.Data.Common;
23-
using System.Transactions;
24-
2522
namespace 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
}

grabdish/inventory-dotnet/dequeueenqueue.sql

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ END;
4343
show errors
4444

4545

46-
47-
-- CREATE OR REPLACE PROCEDURE enqueueInventoryMessage(p_action IN VARCHAR2, p_orderid IN NUMBER)
48-
CREATE OR REPLACE PROCEDURE checkInventoryReturnLocation(p_inventoryId IN VARCHAR2)
46+
CREATE OR REPLACE PROCEDURE checkInventoryReturnLocation(p_inventoryId IN VARCHAR2, p_inventorylocation OUT varchar2)
4947
IS
5048
enqueue_options DBMS_AQ.enqueue_options_t;
5149
message_properties DBMS_AQ.message_properties_t;
@@ -64,17 +62,13 @@ BEGIN
6462
DBMS_OUTPUT.put_line('ID=' || l_id);
6563
END;
6664

67-
message := SYS.AQ$_JMS_TEXT_MESSAGE.construct;
68-
-- message.text_vc := p_inventoryInfo;
69-
message.set_text(p_inventoryInfo);
70-
-- message.set_string_property('action', p_action);
71-
-- message.set_int_property('orderid', p_orderid);
65+
-- message := SYS.AQ$_JMS_TEXT_MESSAGE.construct;
7266

73-
DBMS_AQ.ENQUEUE(queue_name => 'INVENTORYQUEUE',
74-
enqueue_options => enqueue_options,
75-
message_properties => message_properties,
76-
payload => message,
77-
msgid => message_handle);
67+
-- DBMS_AQ.ENQUEUE(queue_name => 'INVENTORYQUEUE',
68+
-- enqueue_options => enqueue_options,
69+
-- message_properties => message_properties,
70+
-- payload => message,
71+
-- msgid => message_handle);
7872

7973
update inventory set inventorycount = inventorycount - 1 where inventoryid = ? and inventorycount > 0 returning inventorylocation into ?
8074

0 commit comments

Comments
 (0)