Skip to content

Instantly share code, notes, and snippets.

@AbhinavPradeep
Created February 10, 2020 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhinavPradeep/4f58d4f5dc6980bc7601be15cef55386 to your computer and use it in GitHub Desktop.
Save AbhinavPradeep/4f58d4f5dc6980bc7601be15cef55386 to your computer and use it in GitHub Desktop.
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using Theta.Customers.Models;
namespace Theta.Customers.Repository
{
public class CustomerRepos
{
private string ConnectionString { get; set; }
public CustomerRepos(string ConnectionString)
{
this.ConnectionString = ConnectionString;
}
private IMongoCollection<Customer> Initialize()
{
var client = new MongoClient(ConnectionString);
var database = client.GetDatabase("CustomersDB");
var collection = database.GetCollection<Customer>("Customers");
return collection;
}
public List<Customer> GetCustomer(int customerID)
{
IMongoCollection<Customer> collection = Initialize();
var Dbcustomer = collection.Find<Customer>(x => x._id == customerID);
return Dbcustomer.ToList();
}
public List<Customer> GetAllCustomers()
{
IMongoCollection<Customer> collection = Initialize();
var Dbcustomer = collection.Find(x => true);
return Dbcustomer.ToList();
}
public long DeleteCustomer(int customerID)
{
IMongoCollection<Customer> collection = Initialize();
var result = collection.DeleteOne<Customer>(x => x._id == customerID);
return result.DeletedCount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment