Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.Diagnostics;
  8. using System.ServiceModel;
  9. using System.Runtime.Serialization;
  10.  
  11. namespace StoreDomain
  12. {
  13. public class Store
  14. {
  15. public Store() { }
  16.  
  17. public Users GetUser(String username)
  18. {
  19. using (mymodelContainer ctx = new mymodelContainer())
  20. {
  21. var users = from u in ctx.UsersSet
  22. where u.Name == username
  23. select u;
  24.  
  25. foreach (Users user in users)
  26. {
  27. return user;
  28. }
  29. return null;
  30. }
  31. }
  32.  
  33. public UsersDTO GetUsersDTO(String username)
  34. {
  35. using (mymodelContainer ctx = new mymodelContainer())
  36. {
  37. var users = from u in ctx.UsersSet
  38. where u.Name == username
  39. select u;
  40.  
  41. UsersDTO user = new UsersDTO();
  42. foreach (Users us in users)
  43. {
  44. user.Id = us.Id;
  45. user.Name = us.Name;
  46. user.Password = us.Password;
  47. user.Saldo = us.Saldo;
  48. return user;
  49. }
  50. return null;
  51. }
  52. }
  53.  
  54. public Product GetProduct(int id)
  55. {
  56. using (mymodelContainer ctx = new mymodelContainer())
  57. {
  58. var products = from p in ctx.ProductSet
  59. where p.Id == id
  60. select p;
  61.  
  62. foreach (Product product in products)
  63. {
  64. return product;
  65. }
  66. return null;
  67. }
  68. }
  69.  
  70. public ProductDTO GetProductDTO(int id)
  71. {
  72. using (mymodelContainer ctx = new mymodelContainer())
  73. {
  74. var products = from p in ctx.ProductSet
  75. where p.Id == id
  76. select p;
  77.  
  78. ProductDTO product = new ProductDTO();
  79. foreach (Product pr in products)
  80. {
  81. product.Id = pr.Id;
  82. product.Name = pr.Name;
  83. product.Price = pr.Price;
  84. return product;
  85. }
  86. return null;
  87. }
  88. }
  89.  
  90. public List<ProductDTO> GetAllProducts()
  91. {
  92. using (mymodelContainer ctx = new mymodelContainer())
  93. {
  94. List<ProductDTO> allProducts = new List<ProductDTO>();
  95.  
  96. var products = from p in ctx.ProductSet
  97. select p;
  98. foreach (Product up in products)
  99. {
  100. ProductDTO product = new ProductDTO();
  101. product.Id = up.Id;
  102. product.Name = up.Name;
  103. product.Price = up.Price;
  104. allProducts.Add(product);
  105. }
  106. return allProducts;
  107. }
  108. }
  109.  
  110. public List<ProductDTO> GetAllProductsDTOByUser(int id)
  111. {
  112. using (mymodelContainer ctx = new mymodelContainer())
  113. {
  114. List<ProductDTO> allProducts = new List<ProductDTO>();
  115.  
  116. var products = from p in ctx.UserProductsSet
  117. where p.UserId == id
  118. select p;
  119. foreach (UserProducts p in products)
  120. {
  121. var userProducts = from up in ctx.ProductSet
  122. where up.Id == p.Id
  123. select up;
  124. foreach (Product up in userProducts)
  125. {
  126. ProductDTO product = new ProductDTO();
  127. product.Id = up.Id;
  128. product.Name = up.Name;
  129. product.Price = up.Price;
  130. allProducts.Add(product);
  131. }
  132. };
  133. return allProducts;
  134. }
  135. }
  136.  
  137. public List<Product> GetAllProductsByUser(int id)
  138. {
  139. using (mymodelContainer ctx = new mymodelContainer())
  140. {
  141. List<Product> allProducts = new List<Product>();
  142.  
  143. var products = from p in ctx.UserProductsSet
  144. where p.UserId == id
  145. select p;
  146. foreach (UserProducts p in products)
  147. {
  148. var userProducts = from up in ctx.ProductSet
  149. where up.Id == p.Id
  150. select up;
  151. foreach (Product up in userProducts)
  152. {
  153. allProducts.Add(up);
  154. }
  155. };
  156. return allProducts;
  157. }
  158. }
  159.  
  160. public Boolean CanSell(ProductDTO p)
  161. {
  162. if (p != null && GetAllProducts().Contains(p))
  163. {
  164. return true;
  165. }
  166. return false;
  167. }
  168.  
  169. public Boolean CanUserBuy(Users user, ProductDTO p)
  170. {
  171. if (p.Price <= user.Saldo)
  172. {
  173. return true;
  174. }
  175. return false;
  176. }
  177.  
  178. public Boolean SellProduct(String username, ProductDTO p)
  179. {
  180. if (CanSell(p) && CanUserBuy(GetUser(username), p))
  181. {
  182. AddProductToUser(username, p.Id);
  183. return true;
  184. }
  185. return false;
  186. }
  187.  
  188. private void AddProductToUser(String username, int productId)
  189. {
  190. using (mymodelContainer ctx = new mymodelContainer())
  191. {
  192. Users user = GetUser(username);
  193. ProductDTO product = GetProductDTO(productId);
  194.  
  195. InventoryItem inventoryItem = ctx.InventoryItemSet.Single(i => i.Id == product.Id);
  196. inventoryItem.Amount -= 1;
  197.  
  198. user.Saldo -= product.Price;
  199.  
  200. List<Product> userProducts = GetAllProductsByUser(user.Id);
  201. if (userProducts.Contains(GetProduct(productId)))
  202. {
  203. UserProducts uProduct = ctx.UserProductsSet.Single(i => i.Id == product.Id);
  204. uProduct.Amount += 1;
  205. }
  206. else
  207. {
  208. UserProducts up = new UserProducts { ProductId = product.Id, UserId = user.Id, Amount = 1 };
  209. ctx.UserProductsSet.Add(up);
  210. }
  211. ctx.SaveChanges();
  212. }
  213. }
  214.  
  215. public List<Users> GetAllUsers()
  216. {
  217. using (mymodelContainer ctx = new mymodelContainer())
  218. {
  219. List<Users> allUsers = new List<Users>();
  220.  
  221. var users = from u in ctx.UsersSet
  222. select u;
  223. foreach (Users u in users)
  224. {
  225. System.Console.WriteLine(u);
  226. Debug.WriteLine(u);
  227. allUsers.Add(u);
  228. }
  229. return allUsers;
  230. }
  231. }
  232.  
  233. public Boolean Register(String username)
  234. {
  235. if (!CheckIfUserExists(username))
  236. {
  237. char[] charArray = username.ToCharArray();
  238. Array.Reverse(charArray);
  239. String password = new string(charArray);
  240.  
  241. AddUserToDatabase(username, password);
  242. return true;
  243. }
  244. return false;
  245. }
  246.  
  247. public Boolean CheckIfUserExists(String username)
  248. {
  249. if(username == String.Empty && GetAllUsers().Count > 0)
  250. {
  251. foreach (Users u in GetAllUsers())
  252. {
  253. if (u.Name.Equals(username))
  254. {
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. }
  261.  
  262. public void AddUserToDatabase(String username, String password)
  263. {
  264. using (mymodelContainer ctx = new mymodelContainer())
  265. {
  266. Users user = new Users { Name = username, Password = password, Saldo = 20.0 };
  267. ctx.UsersSet.Add(user);
  268. ctx.SaveChanges();
  269. }
  270. }
  271.  
  272. public Boolean Login(String username, String password)
  273. {
  274. using (mymodelContainer ctx = new mymodelContainer())
  275. {
  276. foreach(Users u in GetAllUsers())
  277. {
  278. if(u.Name.Equals(username) && u.Password.Equals(password))
  279. {
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. }
  286. }
  287.  
  288. public partial class Users
  289. {
  290. public override Boolean Equals(Object other)
  291. {
  292. Boolean b = other.GetType() == typeof(Users);
  293. if (b)
  294. {
  295. Users u = (Users)other;
  296. b = b && this.Id == u.Id;
  297. b = b && this.Name.Equals(u.Name);
  298. b = b && this.Password.Equals(u.Password);
  299. b = b && this.Saldo == u.Saldo;
  300. }
  301. return b;
  302. }
  303.  
  304. public override int GetHashCode()
  305. {
  306. return Id.GetHashCode() + Name.GetHashCode() + Password.GetHashCode() + Saldo.GetHashCode();
  307. }
  308. }
  309.  
  310. public partial class ProductDTO
  311. {
  312. public override Boolean Equals(Object other)
  313. {
  314. Boolean b = other.GetType() == typeof(ProductDTO);
  315. if (b)
  316. {
  317. ProductDTO p = (ProductDTO)other;
  318. b = b && this.Id == p.Id;
  319. b = b && this.Name.Equals(p.Name);
  320. b = b && this.Price == p.Price;
  321. }
  322. return b;
  323. }
  324.  
  325. public override int GetHashCode()
  326. {
  327. return Id.GetHashCode() + Name.GetHashCode() + Price.GetHashCode();
  328. }
  329. }
  330.  
  331. [DataContract]
  332. public partial class ProductDTO
  333. {
  334. [DataMember]
  335. public int Id { get; set; }
  336. [DataMember]
  337. public String Name { get; set; }
  338. [DataMember]
  339. public double Price { get; set; }
  340. }
  341.  
  342. [DataContract]
  343. public partial class UsersDTO
  344. {
  345. [DataMember]
  346. public int Id { get; set; }
  347. [DataMember]
  348. public String Name { get; set; }
  349. [DataMember]
  350. public String Password { get; set; }
  351. [DataMember]
  352. public double Saldo { get; set; }
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement