Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10. using System.Xml.Serialization;
  11.  
  12. namespace ServiceFinal
  13. {
  14. class ProductService : IProductService
  15. {
  16.  
  17. public Product GetProduct(int id)
  18. {
  19. using (var context = new AdventureWorksLT2008R2Entities())
  20. {
  21. var productEntity = (from p in context.Products
  22. where p.ProductID == id
  23. select p).FirstOrDefault();
  24. if (productEntity == null)
  25. {
  26. throw new Exception("Invalid product ID");
  27. }
  28. else
  29. return productEntity;
  30. }
  31. }
  32.  
  33. public List<Product> GetProducts()
  34. {
  35. using (var context = new AdventureWorksLT2008R2Entities())
  36. {
  37. var list = (from p in context.Products select p).ToList();
  38. return list;
  39. }
  40. }
  41.  
  42. public void SetProduct(string Name, string Color, string ProductNumber, decimal StandardCost, decimal ListPrice, int ProductCategoryID, int ProductModelID)
  43. {
  44. try
  45. {
  46. AdventureWorksLT2008R2Entities context = new AdventureWorksLT2008R2Entities();
  47. Product p = new Product()
  48. {
  49. Color = Color,
  50. Name = Name,
  51. ProductNumber = ProductNumber,
  52. StandardCost = StandardCost,
  53. ListPrice = ListPrice,
  54. ProductCategoryID = ProductCategoryID,
  55. ProductModelID = ProductModelID
  56. };
  57. context.Products.Add(p);
  58. context.SaveChanges();
  59. }
  60. catch (Exception e)
  61. {
  62. Console.WriteLine(e.InnerException);
  63. }
  64. }
  65.  
  66. public bool IsValid(string NameText, string ProductNumberText)
  67. {
  68. using (var context = new AdventureWorksLT2008R2Entities())
  69. {
  70. var list = (from p in context.Products select p).ToList();
  71. foreach (var a in list)
  72. {
  73. if (a.Name.Equals(NameText) || a.ProductNumber.Equals(ProductNumberText))
  74. {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. }
  81.  
  82. public void DeleteProduct(int id)
  83. {
  84. using (var context = new AdventureWorksLT2008R2Entities())
  85. {
  86. var productToRemove = context.Products.FirstOrDefault(a => a.ProductID == id);
  87.  
  88. if (productToRemove != null)
  89. {
  90. context.Products.Remove(productToRemove);
  91. }
  92. context.SaveChanges();
  93. }
  94. }
  95.  
  96. public void UpdateProduct(Product prod)
  97. {
  98. using (var context = new AdventureWorksLT2008R2Entities())
  99. {
  100. var product = context.Products.Where(p => p.ProductID == prod.ProductID).FirstOrDefault();
  101. if (product != null)
  102. {
  103. product.Name = prod.Name;
  104. product.ProductNumber = prod.ProductNumber;
  105. product.Color = prod.Color;
  106. product.ListPrice = prod.ListPrice;
  107. product.StandardCost = prod.StandardCost;
  108. product.ProductCategoryID = prod.ProductCategoryID;
  109. product.ProductModelID = prod.ProductModelID;
  110. }
  111. context.SaveChanges();
  112. }
  113.  
  114. }
  115.  
  116. public void DeleteProducts(List<int> ids)
  117. {
  118. using (var context = new AdventureWorksLT2008R2Entities())
  119. {
  120. context.Products.RemoveRange(context.Products.Where(p => ids.Contains(p.ProductID)));
  121. context.SaveChanges();
  122. }
  123. }
  124.  
  125. public void InsertProducts(List<Product> list)
  126. {
  127. using (var context = new AdventureWorksLT2008R2Entities())
  128. {
  129. foreach (var item in list)
  130. {
  131. context.Products.Add(item);
  132. }
  133. context.SaveChanges();
  134. }
  135. }
  136.  
  137. public HashSet<string> SelectCategoryName()
  138. {
  139. using (var context = new AdventureWorksLT2008R2Entities())
  140. {
  141. var categoryNameAll = (from p in context.Products
  142. join c in context.ProductCategories on p.ProductCategoryID equals c.ProductCategoryID
  143. select c.Name).ToList();
  144. HashSet<string> categoryName = new HashSet<string>();
  145. foreach (var name in categoryNameAll)
  146. {
  147. categoryName.Add(name);
  148. }
  149. return categoryName;
  150. }
  151.  
  152. }
  153.  
  154. public HashSet<string> SelectModel()
  155. {
  156. using (var context = new AdventureWorksLT2008R2Entities())
  157. {
  158. var categoryNameAll = (from p in context.Products
  159. join m in context.ProductModels on p.ProductModelID equals m.ProductModelID
  160. select m.Name).ToList();
  161. HashSet<string> categoryName = new HashSet<string>();
  162. foreach (var name in categoryNameAll)
  163. {
  164. categoryName.Add(name);
  165. }
  166. return categoryName;
  167. }
  168. }
  169.  
  170. public string GetCategoryName(int? id)
  171. {
  172. using (var context = new AdventureWorksLT2008R2Entities())
  173. {
  174. string categName = String.Empty;
  175. try
  176. {
  177. var categoryName = (from c in context.ProductCategories
  178. join p in context.Products on c.ProductCategoryID equals p.ProductCategoryID
  179. where c.ProductCategoryID == id
  180. select c.Name).First().ToString();
  181. categName = categoryName;
  182. }
  183. catch (Exception ex)
  184. {
  185. return "None";
  186. }
  187. return categName;
  188. }
  189. }
  190.  
  191. public string GetModelName(int? id)
  192. {
  193. using (var context = new AdventureWorksLT2008R2Entities())
  194. {
  195. string prodName = String.Empty;
  196. try
  197. {
  198. var productName = (from p in context.Products
  199. join m in context.ProductModels on p.ProductModelID equals m.ProductModelID
  200. where m.ProductModelID == id
  201. select m.Name).First().ToString();
  202. prodName = productName;
  203. }
  204. catch (Exception ex)
  205. {
  206. return "None";
  207. }
  208. return prodName;
  209. }
  210. }
  211.  
  212. public int GetCategoryID(string category)
  213. {
  214. using (var context = new AdventureWorksLT2008R2Entities())
  215. {
  216. int ID = 0;
  217. try
  218. {
  219. var categoryID = (from c in context.ProductCategories
  220. join p in context.Products on c.ProductCategoryID equals p.ProductCategoryID
  221. where c.Name == category
  222. select c.ProductCategoryID).First();
  223. ID = categoryID;
  224. }
  225. catch (Exception ex)
  226. {
  227. Console.WriteLine(ex.Message);
  228. }
  229. return ID;
  230. }
  231.  
  232. }
  233.  
  234. public int GetModelID(string model)
  235. {
  236. using (var context = new AdventureWorksLT2008R2Entities())
  237. {
  238. var modelID = (from m in context.ProductModels
  239. join p in context.Products on m.ProductModelID equals p.ProductModelID
  240. where m.Name == model
  241. select m.ProductModelID).First();
  242. return modelID;
  243. }
  244.  
  245. }
  246.  
  247. public List<SalesOrderHeader> GetSalesOrder()
  248. {
  249. using (var context = new AdventureWorksLT2008R2Entities())
  250. {
  251. var list = context.SalesOrderHeaders.ToList();
  252. return list;
  253. }
  254. }
  255.  
  256. public SalesOrderHeader GetOrder(int id)
  257. {
  258. using (var context = new AdventureWorksLT2008R2Entities())
  259. {
  260. var productEntity = (from order in context.SalesOrderHeaders
  261. where order.SalesOrderID == id
  262. select order).FirstOrDefault();
  263. if (productEntity == null)
  264. {
  265. throw new Exception("Invalid order ID");
  266. }
  267. else
  268. return productEntity;
  269. }
  270. }
  271.  
  272. public List<Product> GetTheWantedProduct(int id)
  273. {
  274. using (var context = new AdventureWorksLT2008R2Entities())
  275. {
  276.  
  277. var productToDisplay = (from oh in context.SalesOrderHeaders
  278. join od in context.SalesOrderDetails
  279. on oh.SalesOrderID equals od.SalesOrderID
  280. where oh.SalesOrderID == id
  281. join p in context.Products
  282. on od.ProductID equals p.ProductID
  283. select p).ToList();
  284. return productToDisplay;
  285. }
  286.  
  287. }
  288. public Customer AddCustomer(string Title, string FirstName, string LastName)
  289. {
  290.  
  291. using (var context = new AdventureWorksLT2008R2Entities())
  292. {
  293. Customer customer = new Customer()
  294. {
  295. Title = Title,
  296. FirstName = FirstName,
  297. LastName = LastName
  298. };
  299. context.Customers.Add(customer);
  300. context.SaveChanges();
  301. return customer;
  302. }
  303. }
  304.  
  305. public Address AddAddress(string Street, string City, string StateProvince, string Country, string PostalCode)
  306. {
  307. using (var context = new AdventureWorksLT2008R2Entities())
  308. {
  309. Address address = new Address()
  310. {
  311. AddressLine1 = Street,
  312. City = City,
  313. StateProvince = StateProvince,
  314. CountryRegion = Country,
  315. PostalCode = PostalCode
  316. };
  317. context.Addresses.Add(address);
  318. context.SaveChanges();
  319. return address;
  320. }
  321. }
  322.  
  323. public SalesOrderHeader AddOrderHeader(int customerID, int addressID)
  324. {
  325. using (var context = new AdventureWorksLT2008R2Entities())
  326. {
  327. SalesOrderHeader soh = new SalesOrderHeader()
  328. {
  329. CustomerID = customerID,
  330. ShipToAddressID = addressID,
  331. BillToAddressID = addressID
  332. };
  333. context.SalesOrderHeaders.Add(soh);
  334. context.SaveChanges();
  335. return soh;
  336. }
  337. }
  338.  
  339. public void AddOrderDetail(List<string> products, int salesOrderID)
  340. {
  341. using (var context = new AdventureWorksLT2008R2Entities())
  342. {
  343. foreach (var product in products)
  344. {
  345. SalesOrderDetail sod = new SalesOrderDetail()
  346. {
  347. SalesOrderID = salesOrderID,
  348. ProductID = context.Products.First(a => a.Name == product).ProductID,
  349. UnitPrice = context.Products.First(a => a.Name == product).ListPrice,
  350. UnitPriceDiscount = 0
  351. };
  352. context.SalesOrderDetails.Add(sod);
  353. context.SaveChanges();
  354. }
  355. }
  356. }
  357.  
  358. public SalesOrderHeader GetOrderHeader(int id)
  359. {
  360. using (var context = new AdventureWorksLT2008R2Entities())
  361. {
  362. var orderEntity = (from o in context.SalesOrderHeaders
  363. where o.SalesOrderID == id
  364. select o).FirstOrDefault();
  365. if (orderEntity == null)
  366. {
  367. throw new Exception("Invalid order ID");
  368. }
  369. else
  370. return orderEntity;
  371. }
  372. }
  373.  
  374. public Customer GetCustomer(int id)
  375. {
  376. using (var context = new AdventureWorksLT2008R2Entities())
  377. {
  378. var customer = (from order in context.SalesOrderHeaders
  379. where order.SalesOrderID == id
  380. join cust in context.Customers
  381. on order.CustomerID equals cust.CustomerID
  382. select cust).First();
  383. return customer;
  384. }
  385. }
  386.  
  387. public Address GetTheAddress(int id)
  388. {
  389. using (var context = new AdventureWorksLT2008R2Entities())
  390. {
  391. var address = (from order in context.SalesOrderHeaders
  392. where order.SalesOrderID == id
  393. join add in context.Addresses
  394. on order.ShipToAddressID equals add.AddressID
  395. select add).First();
  396. return address;
  397. }
  398. }
  399.  
  400. public List<string> GetTheOrderedProducts(int id)
  401. {
  402. using (var context = new AdventureWorksLT2008R2Entities())
  403. {
  404. var orderedProducts = (from order in context.SalesOrderHeaders
  405. where order.SalesOrderID == id
  406. join details in context.SalesOrderDetails
  407. on order.SalesOrderID equals details.SalesOrderID
  408. join products in context.Products
  409. on details.ProductID equals products.ProductID
  410. select products.Name).ToList();
  411. return orderedProducts;
  412. }
  413. }
  414. public void DeleteOrders(List<int> ids)
  415. {
  416. using (var context = new AdventureWorksLT2008R2Entities())
  417. {
  418. context.SalesOrderHeaders.RemoveRange(context.SalesOrderHeaders.Where(order => ids.Contains(order.SalesOrderID)));
  419. context.SaveChanges();
  420. }
  421. }
  422.  
  423. public void ModifyCustomer(int id, string firstName, string lastName)
  424. {
  425. using (var context = new AdventureWorksLT2008R2Entities())
  426. {
  427. var customer = (from order in context.SalesOrderHeaders
  428. where order.SalesOrderID == id
  429. join cust in context.Customers
  430. on order.CustomerID equals cust.CustomerID
  431. select cust).First();
  432. customer.FirstName = firstName;
  433. customer.LastName = lastName;
  434. context.SaveChanges();
  435. }
  436.  
  437. }
  438.  
  439. public void ModifyAddress(int id, string Street, string City, string StateProvince, string Country, string PostalCode)
  440. {
  441. using (var context = new AdventureWorksLT2008R2Entities())
  442. {
  443. var address = (from order in context.SalesOrderHeaders
  444. where order.SalesOrderID == id
  445. join addr in context.Addresses
  446. on order.ShipToAddressID equals addr.AddressID
  447. select addr).First();
  448. address.AddressLine1 = Street;
  449. address.City = City;
  450. address.StateProvince = StateProvince;
  451. address.CountryRegion = Country;
  452. address.PostalCode = PostalCode;
  453. context.SaveChanges();
  454. }
  455. }
  456. public void ModifyTheOrderedProducts(int salesOrderID, List<string> newProducts, List<string> checkedProducts)
  457. {
  458. using (var context = new AdventureWorksLT2008R2Entities())
  459. {
  460. var productsToAdd = AddOrderedProducts(newProducts, checkedProducts);
  461. var productsToDelete = DeleteTheUnwantedProducts(newProducts, checkedProducts);
  462.  
  463. foreach (var product in productsToAdd)
  464. {
  465. SalesOrderDetail sod = sod = new SalesOrderDetail();
  466. sod.SalesOrderID = salesOrderID;
  467. sod.ProductID = context.Products.First(a => a.Name == product).ProductID;
  468. sod.UnitPrice = context.Products.First(a => a.Name == product).ListPrice;
  469. sod.UnitPriceDiscount = 0;
  470. context.SalesOrderDetails.Add(sod);
  471. context.SaveChanges();
  472.  
  473. }
  474. foreach (var product in productsToDelete)
  475. {
  476.  
  477. var productID = context.Products.First(p => p.Name == product).ProductID;
  478.  
  479. var salesDetailIDToDelete = context.SalesOrderDetails.First(detail => detail.ProductID == productID
  480. && detail.SalesOrderID == salesOrderID).SalesOrderDetailID;
  481.  
  482. var salesOrder = context.SalesOrderDetails.Where(detail => detail.SalesOrderDetailID == salesDetailIDToDelete)
  483. .FirstOrDefault();
  484. if (salesOrder != null)
  485. {
  486. context.SalesOrderDetails.Remove(salesOrder);
  487. context.SaveChanges();
  488. }
  489. }
  490.  
  491. }
  492. }
  493.  
  494. private List<string> AddOrderedProducts(List<string> newProducts, List<string> checkedProducts)
  495. {
  496. var productsToAdd = new List<string>();
  497. foreach (var product in newProducts)
  498. {
  499. if (checkedProducts.Contains(product) == false)
  500. {
  501. productsToAdd.Add(product);
  502. }
  503. }
  504. return productsToAdd;
  505. }
  506.  
  507. private List<string> DeleteTheUnwantedProducts(List<string> newProducts, List<string> checkedProducts)
  508. {
  509. var productsToDelete = new List<string>();
  510. foreach (var product in checkedProducts)
  511. {
  512. if (newProducts.Contains(product) == false)
  513. {
  514. productsToDelete.Add(product);
  515. }
  516. }
  517. return productsToDelete;
  518. }
  519.  
  520. public List<User> GetUsers()
  521. {
  522. using (var context = new AdventureWorksLT2008R2Entities())
  523. {
  524. var list = (from user in context.Users select user).ToList();
  525. return list;
  526. }
  527. }
  528.  
  529. public User AddUser(string username, string password)
  530. {
  531. using (var context = new AdventureWorksLT2008R2Entities())
  532. {
  533. User user = new User()
  534. {
  535. Username = username,
  536. Password = Hash(password)
  537. };
  538. context.Users.Add(user);
  539. context.SaveChanges();
  540. return user;
  541. }
  542. }
  543.  
  544. private string Hash(string password)
  545. {
  546. var bytes = new UTF8Encoding().GetBytes(password);
  547. var hashBytes = System.Security.Cryptography.MD5.Create().ComputeHash(bytes);
  548. return Convert.ToBase64String(hashBytes);
  549. }
  550.  
  551. public string GetUserPassword(string username)
  552. {
  553. using (var context = new AdventureWorksLT2008R2Entities())
  554. {
  555. var pass = context.Users.First(u => u.Username == username).Password;
  556. return pass;
  557. }
  558. }
  559. }
  560. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement