/
EbayFactory.svc.cs
354 lines (291 loc) · 12.5 KB
/
EbayFactory.svc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using eBay.Service.Call;
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;
using eBay.Service.Core;
using eBay.Services.Finding;
using eBay.Services;
using Samples.Helper;
using System.Collections;
using System.Security.Principal;
using System.Web.Security;
namespace EbayFactory
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class EbayClient : IEbayClient
{
private FindingServicePortTypeClient m_client;
private ClientConfig m_config;
private ApiContext m_api;
DataClasses1DataContext db;
tblCategory m_cats;
private List<string> m_log;
// private ApiCredential m_user;
/* Test site
private static readonly string findingServerAddress = "http://svcs.sandbox.ebay.com/services/search/FindingService/v1";
private static readonly string appID = "RichardF-18df-4fec-a53e-fc4110d935dd";
*/
/* Production Site */
private static readonly string findingServerAddress = "http://svcs.ebay.com/services/search/FindingService/v1?";
private static readonly string appID = "RichardF-62b4-46b0-bccd-37e659be2a82";
public EbayClient()
{
m_config = new ClientConfig();
m_config.ApplicationId = appID;
m_config.EndPointAddress = findingServerAddress;
m_client = FindingServiceClientFactory.getServiceClient(m_config);
m_api = new ApiContext();
m_api = AppSettingHelper.GetApiContext();
m_cats = new tblCategory();
db = new DataClasses1DataContext();
m_log = new List<string> { };
}
public List<string> GetLog()
{
return m_log;
}
public void CreateSiteDataBase()
{
DataClasses1DataContext context = new DataClasses1DataContext();
if (false == context.DatabaseExists())
{
context.CreateDatabase();
}
}
public List<string> GetCategories()
{
DataClasses1DataContext context= new DataClasses1DataContext();
var result = from r in context.tblCategories select r.category_id;
return result.ToList();
}
//get categories from item table
//count number of occurances for each
//insert into table vaules category - category count
//return ordered list in decending order
public void SetCategoryCount()
{
DataClasses1DataContext context = new DataClasses1DataContext();
var result = from i in context.tblItems select i.item_category;
var d = result.Distinct();
List<string> dlist = d.ToList<string>();
foreach (string s in d)
{
//update
var query= from update in context.cat_counts
where update.category_id==s
select update;
m_log.Add(query.ToString());
var cnt = context.tblItems.Count(p => p.item_category == s);
foreach(cat_count up in query)
{
dlist.Remove(up.category_id);
up.category_count = cnt;
}//update
try
{
context.SubmitChanges();
m_log.Add(context.Log.ToString());
}
catch (Exception e)
{
string er = e.Message;
}
}
foreach (string s in dlist)
{
var cnt = context.tblItems.Count(p => p.item_category == s);
cat_count catcnt = new cat_count();
catcnt.category_id = s;
catcnt.category_count = cnt;
try
{
context.cat_counts.InsertOnSubmit(catcnt);
context.SubmitChanges();
m_log.Add(context.Log.ToString());
}
catch (Exception e)
{
string er = e.Message;
}
}
}
public List<cat_count> GetCategoryCount()
{
DataClasses1DataContext context = new DataClasses1DataContext();
return context.cat_counts.ToList().OrderByDescending(cat_count => cat_count.category_count).ToList();
}
public List<tblItem> ItemByCategory(string cat)
{
DataClasses1DataContext context = new DataClasses1DataContext();;
var result = (from i in context.tblItems where i.item_category == cat select i);
return result.ToList();
}
public string TestConnection()
{
IIdentity currentUser = ServiceSecurityContext.Current.PrimaryIdentity;
if (Roles.IsUserInRole(currentUser.Name, "Member"))
{
DataClasses1DataContext context = new DataClasses1DataContext();
return string.Format(context.Connection.State + context.Connection.ToString());
}
return string.Format("fail");
}
public void EbayTopLevelCategories()
{
GetCategoriesCall Categories = new GetCategoriesCall(m_api);
Categories.LevelLimit = 2;
Categories.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);
CategoryTypeCollection cats =Categories.GetCategories();
foreach (CategoryType category in cats)
{
m_cats = new tblCategory();
m_cats.category_id= category.CategoryID;
m_cats.category_level = category.CategoryLevel.ToString();
m_cats.category_name = category.CategoryName;
m_cats.category_parent = category.CategoryParentID[0].ToString();
DataClasses1DataContext db = new DataClasses1DataContext();
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
// context.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EbayFactoryConnectionString"].ConnectionString;
context.tblCategories.InsertOnSubmit(m_cats);
context.SubmitChanges();
}
catch(Exception e)
{
string er = e.Message;
}
}
}
public List<tblItem> FindByKeyWord(String items)
{
List<tblItem> prodlist = new List<tblItem> { };
FindItemsAdvancedRequest request = new FindItemsAdvancedRequest();
//request.affiliate.trackingId = null;
//request.affiliate.networkId = "9";
// Set request parameters
request.keywords = items;
ItemFilter filter1 = new ItemFilter();
ItemFilter filter2 = new ItemFilter();
ItemFilter filter3 = new ItemFilter();
filter3.name = ItemFilterType.Condition;
filter3.value = new string[] { "1000" };
ItemFilter[] filters = { filter3/*, filter2, filter3*/ };
request.itemFilter = filters;
//request.categoryId = items;
if (request.keywords == null)
{
request.keywords = "ipod";
}
PaginationInput pi = new PaginationInput();
pi.entriesPerPage = 100;
pi.entriesPerPageSpecified = true;
request.paginationInput = pi;
// Call the service
FindItemsAdvancedResponse response = m_client.findItemsAdvanced(request);
SearchItem[] listing = response.searchResult.item;
if (listing != null)
{
foreach (SearchItem i in listing)
{
tblItem items_tbl = new tblItem();
items_tbl.item_category = i.primaryCategory.categoryId;
items_tbl.item_title = i.title;
items_tbl.item_id = i.itemId;
items_tbl.gallery_url = i.galleryURL;
items_tbl.listing_url = i.viewItemURL;
prodlist.Add(items_tbl);
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
context.tblItems.InsertOnSubmit(items_tbl);
context.SubmitChanges();
}
catch (Exception e)
{
string s = e.Message;
}
}
return prodlist;
}
return prodlist;
}
public void DeleteTablesThenPopulate()
{
DataClasses1DataContext context = new DataClasses1DataContext();
List<tblItem> items= (from i in context.tblItems select i).ToList<tblItem>();
List<tblCategory> categories = (from i in context.tblCategories select i).ToList<tblCategory>();
try
{
context.tblCategories.DeleteAllOnSubmit(categories);
context.tblItems.DeleteAllOnSubmit(items);
context.SubmitChanges();
}
catch (Exception e)
{
string er = e.ToString();
}
this.EbayTopLevelCategories();
List<string> cats = this.GetCategories();
for (int i = 0; i <= 30; i++)
{
this.FindByCategory(new string[] { cats[i] });
}
}
public void FindByCategory(String[] items)
{
FindItemsAdvancedRequest request = new FindItemsAdvancedRequest();
//request.affiliate.trackingId = null;
//request.affiliate.networkId = "9";
// Set request parameters
// request.keywords = items;
ItemFilter filter1 = new ItemFilter();
ItemFilter filter2 = new ItemFilter();
ItemFilter filter3 = new ItemFilter();
filter3.name = ItemFilterType.Condition;
filter3.value = new string[] { "1000" };
ItemFilter[] filters = { filter3/*, filter2, filter3*/ };
request.itemFilter = filters;
request.categoryId = items;
// if (request.keywords == null)
// {
// request.keywords = "ipod";
//}
PaginationInput pi = new PaginationInput();
pi.entriesPerPage = 100;
pi.entriesPerPageSpecified = true;
request.paginationInput = pi;
// Call the service
FindItemsAdvancedResponse response = m_client.findItemsAdvanced(request);
SearchItem[] listing = response.searchResult.item;
if (listing != null)
{
foreach (SearchItem i in listing)
{
tblItem items_tbl = new tblItem();
items_tbl.item_category = i.primaryCategory.categoryId;
items_tbl.item_title = i.title;
items_tbl.item_id = i.itemId;
items_tbl.gallery_url = i.galleryURL;
items_tbl.listing_url = i.viewItemURL;
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
context.tblItems.InsertOnSubmit(items_tbl);
context.SubmitChanges();
}
catch (Exception e)
{
string s = e.Message;
}
}
}
}
}
}