Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MongoDB.Driver;
- using MongoDB.Bson;
- using MongoDB.Driver.Linq;
- using SurffingSite.Models;
- namespace SurffingSite.DB
- {
- public class DBManager
- {
- static MongoClient client;
- static IMongoDatabase DB;
- // Constractor
- static DBManager()
- {
- client = new MongoClient("mongodb://localhost:27017");
- DB = client.GetDatabase("HELLOSTACKOVERFLOW");
- }
- // Method to get list of all users//
- public static IMongoCollection<User> GetUsersCollection()
- {
- var collection = DB.GetCollection<User>("User");
- return collection;
- }
- internal static object GetCollection<T>(string v)
- {
- throw new NotImplementedException();
- }
- //User Method //
- // Method for adding new user
- public static void AddNewUser(User user)
- {
- var collection = DB.GetCollection<User>("User");
- collection.InsertOne(user);
- }
- //Get user email.
- public static User GetUserEmail(User input)
- {
- User email = DBManager.GetUsersCollection().Find(user => user.Email == input.Email).FirstOrDefault();
- return email;
- }
- //Get user passsword.
- public static User GetUserPassword(User input)
- {
- User password = DBManager.GetUsersCollection().Find(user => user.Password == input.Password).FirstOrDefault();
- return password;
- }
- //User login verification
- public static User UserVerification(User input)
- {
- // try get user by email
- // if user exist than check if the password is correct if not than return null
- User UseVar = DBManager.GetUserEmail(input);
- if (UseVar != null && UseVar.Password == input.Password)
- {
- return UseVar;
- }
- return null;
- }
- //Update User details
- public static void UpdateUserDetails(FilterDefinition<User> filter, UpdateDefinition<User> update)
- {
- var collection = DB.GetCollection<User>("User");
- collection.UpdateOne(filter, update);
- }
- //Products Method's//
- // Adding product into DB
- public static void AddProduct(Products product)
- {
- var collection = DB.GetCollection<Products>("Products");
- collection.InsertOne(product);
- }
- public static IMongoCollection<Products> GetProductsCollection()
- {
- var collection = DB.GetCollection<Products>("Products");
- return collection;
- }
- // Find product in DB by ID
- public static Products FindProductsById(Products input)
- {
- Products searchedProd = DB.GetCollection<Products>("Products").Find(product => product.Id == input.Id).FirstOrDefault();
- return searchedProd;
- }
- //Update product
- public static void UpdateDetails(FilterDefinition<Products> filter, UpdateDefinition<Products> update)
- {
- var collection = DB.GetCollection<Products>("Products");
- collection.UpdateOne(filter, update);
- }
- //Orders//
- // This method will return all exist orders
- public static IMongoCollection<Orders> GetOrdersCollection()
- {
- var collection = DB.GetCollection<Orders>("Orders");
- return collection;
- }
- //Place an new order
- public static void AddOrder(Orders order)
- {
- var collection = DB.GetCollection<Orders>("Orders");
- collection.InsertOne(order);
- }
- // Search for all user orders
- public static List<Orders> FindUserOrders(User user)
- {
- List<Orders> searchedorders = DB.GetCollection<Orders>("Orders")
- .Find(order => order.User.ID == user.ID).ToList<Orders>();
- return searchedorders;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement