Advertisement
Guest User

yes indeed

a guest
Jun 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MongoDB.Driver;
  6. using MongoDB.Bson;
  7. using MongoDB.Driver.Linq;
  8. using SurffingSite.Models;
  9.  
  10.  
  11.  
  12. namespace SurffingSite.DB
  13. {
  14. public class DBManager
  15. {
  16. static MongoClient client;
  17. static IMongoDatabase DB;
  18. // Constractor
  19. static DBManager()
  20. {
  21. client = new MongoClient("mongodb://localhost:27017");
  22. DB = client.GetDatabase("HELLOSTACKOVERFLOW");
  23. }
  24. // Method to get list of all users//
  25. public static IMongoCollection<User> GetUsersCollection()
  26. {
  27. var collection = DB.GetCollection<User>("User");
  28. return collection;
  29. }
  30.  
  31. internal static object GetCollection<T>(string v)
  32. {
  33. throw new NotImplementedException();
  34. }
  35.  
  36. //User Method //
  37.  
  38. // Method for adding new user
  39. public static void AddNewUser(User user)
  40. {
  41. var collection = DB.GetCollection<User>("User");
  42. collection.InsertOne(user);
  43. }
  44. //Get user email.
  45. public static User GetUserEmail(User input)
  46. {
  47. User email = DBManager.GetUsersCollection().Find(user => user.Email == input.Email).FirstOrDefault();
  48. return email;
  49. }
  50.  
  51. //Get user passsword.
  52. public static User GetUserPassword(User input)
  53. {
  54. User password = DBManager.GetUsersCollection().Find(user => user.Password == input.Password).FirstOrDefault();
  55. return password;
  56. }
  57. //User login verification
  58. public static User UserVerification(User input)
  59. {
  60. // try get user by email
  61. // if user exist than check if the password is correct if not than return null
  62. User UseVar = DBManager.GetUserEmail(input);
  63. if (UseVar != null && UseVar.Password == input.Password)
  64. {
  65. return UseVar;
  66. }
  67. return null;
  68. }
  69. //Update User details
  70. public static void UpdateUserDetails(FilterDefinition<User> filter, UpdateDefinition<User> update)
  71. {
  72. var collection = DB.GetCollection<User>("User");
  73. collection.UpdateOne(filter, update);
  74. }
  75.  
  76. //Products Method's//
  77.  
  78. // Adding product into DB
  79. public static void AddProduct(Products product)
  80. {
  81. var collection = DB.GetCollection<Products>("Products");
  82. collection.InsertOne(product);
  83. }
  84. public static IMongoCollection<Products> GetProductsCollection()
  85. {
  86. var collection = DB.GetCollection<Products>("Products");
  87. return collection;
  88. }
  89. // Find product in DB by ID
  90. public static Products FindProductsById(Products input)
  91. {
  92. Products searchedProd = DB.GetCollection<Products>("Products").Find(product => product.Id == input.Id).FirstOrDefault();
  93. return searchedProd;
  94. }
  95. //Update product
  96. public static void UpdateDetails(FilterDefinition<Products> filter, UpdateDefinition<Products> update)
  97. {
  98. var collection = DB.GetCollection<Products>("Products");
  99. collection.UpdateOne(filter, update);
  100.  
  101. }
  102.  
  103. //Orders//
  104.  
  105. // This method will return all exist orders
  106. public static IMongoCollection<Orders> GetOrdersCollection()
  107. {
  108. var collection = DB.GetCollection<Orders>("Orders");
  109. return collection;
  110. }
  111. //Place an new order
  112. public static void AddOrder(Orders order)
  113. {
  114. var collection = DB.GetCollection<Orders>("Orders");
  115. collection.InsertOne(order);
  116. }
  117. // Search for all user orders
  118. public static List<Orders> FindUserOrders(User user)
  119. {
  120. List<Orders> searchedorders = DB.GetCollection<Orders>("Orders")
  121. .Find(order => order.User.ID == user.ID).ToList<Orders>();
  122. return searchedorders;
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement