using MongoDB.Driver;
|
using MongoDB.Driver.Builders;
|
using MongoDBMVC.Properties;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.Linq;
|
using System.Runtime.CompilerServices;
|
using System.Web;
|
using System.Web.Mvc;
|
using Microsoft.Ajax.Utilities;
|
using MongoDB.Bson;
|
using MongoDB.Driver.Linq;
|
|
namespace MongoDBMVC.Employee
|
{
|
public class EmployeeRepository : IEmployeeRepository
|
{
|
public MongoDatabase _MongoDB;
|
public MongoCollection _MongoEmpCollection;
|
public bool ServerIsDown = false;
|
|
public EmployeeRepository()
|
{
|
var mongoClient = new MongoClient(Settings.Default.EmployeeCS);
|
var server = mongoClient.GetServer();
|
_MongoDB = server.GetDatabase("Employees");
|
_MongoEmpCollection = _MongoDB.GetCollection("Employees");
|
|
//test if server is up and running
|
try
|
{
|
_MongoDB.Server.Ping();
|
// Ping() method throws exception if not able to connect
|
}
|
catch (Exception ex)
|
{
|
ServerIsDown = true;
|
}
|
|
}
|
|
|
|
private Employee[] _testEmployeeData = new Employee[]
|
{
|
new Employee()
|
{
|
Id = Guid.NewGuid().ToString(),
|
EmployeeId = 50010,
|
FirstName = "Surya",
|
LastName = "Shankar",
|
ContactNo = "99999999",
|
Email = "Suryashankar.something.com",
|
Gender = "Male",
|
Department = new List<Department.Department>()
|
{
|
new Department.Department()
|
{
|
Id = Guid.NewGuid().ToString(),
|
DepartmentId = 01,
|
DepartmentName = "CEO",
|
Description = "Head of the Office",
|
Status = "Active",
|
CreateDateTime = DateTime.UtcNow,
|
ModifyDateTime = DateTime.UtcNow
|
},
|
|
new Department.Department()
|
{
|
Id = Guid.NewGuid().ToString(),
|
DepartmentId = 03,
|
DepartmentName = "SAP Analyst",
|
Description = "SAP Head of the office",
|
Status = "In-Active",
|
CreateDateTime = DateTime.UtcNow,
|
ModifyDateTime = DateTime.UtcNow
|
},
|
new Department.Department()
|
{
|
Id = Guid.NewGuid().ToString(),
|
DepartmentId = 02,
|
DepartmentName = "Sr.Director",
|
Description = "CEO of the Company",
|
Status = "Active",
|
CreateDateTime = DateTime.UtcNow,
|
ModifyDateTime = DateTime.UtcNow
|
},
|
},
|
|
Status = "Active"
|
},
|
new Employee()
|
{
|
Id = Guid.NewGuid().ToString(),
|
EmployeeId = 50011,
|
FirstName = "Vishnu",
|
LastName = "Vallapu",
|
ContactNo = "99999999",
|
Email = "Vishnuvallapu.something.com",
|
Gender = "Male",
|
Department = new List<Department.Department>()
|
{
|
new Department.Department()
|
{
|
Id = Guid.NewGuid().ToString(),
|
DepartmentId = 01,
|
DepartmentName = "CEO",
|
Description = "Head of the Office",
|
Status = "Active",
|
CreateDateTime = DateTime.UtcNow,
|
ModifyDateTime = DateTime.UtcNow
|
},
|
|
new Department.Department()
|
{
|
Id = Guid.NewGuid().ToString(),
|
DepartmentId = 04,
|
DepartmentName = "Monitoring",
|
Description = "Checks all the work done by the employees",
|
Status = "In-Active",
|
CreateDateTime = DateTime.UtcNow,
|
ModifyDateTime = DateTime.UtcNow
|
}
|
},
|
Status = "Active"
|
},
|
|
};
|
|
private List<Employee> _employeesList = new List<Employee>();
|
|
public IEnumerable<Employee> GetAllEmployees()
|
{
|
if (ServerIsDown) return null;
|
|
if (Convert.ToInt32(_MongoEmpCollection.Count()) > 0)
|
{
|
_employeesList.Clear();
|
var employees = _MongoEmpCollection.FindAs(typeof(Employee), Query.NE("FirstName", "null"));
|
if (employees.Count() > 0)
|
{
|
foreach (Employee employee in employees)
|
{
|
_employeesList.Add(employee);
|
}
|
}
|
}
|
else
|
{
|
#region add test data if DB is empty
|
|
_MongoEmpCollection.RemoveAll();
|
foreach (var employee in _testEmployeeData)
|
{
|
_employeesList.Add(employee);
|
|
Add(employee); // add data to mongo db also
|
}
|
|
#endregion
|
}
|
|
var result = _employeesList.AsQueryable();
|
return result;
|
|
}
|
|
public Employee Add(Employee employee)
|
{
|
if (string.IsNullOrEmpty(employee.Id))
|
{
|
employee.Id = Guid.NewGuid().ToString();
|
}
|
_MongoEmpCollection.Save(employee);
|
return employee;
|
}
|
|
public bool Delete(int ObjectId)
|
{
|
_MongoEmpCollection.Remove(Query.EQ("EmployeeId", ObjectId));
|
return true;
|
}
|
|
public Employee GetEmployeeById(int id = 0)
|
{
|
if (id == 0)
|
{
|
throw new ArgumentNullException("id", "Employee Id is empty!");
|
}
|
var employee = (Employee) _MongoEmpCollection.FindOneAs(typeof(Employee), Query.EQ("EmployeeId", id));
|
return employee;
|
}
|
|
public bool Update(int objectId, Employee employee)
|
{
|
UpdateBuilder updateBuilder = MongoDB.Driver.Builders.Update
|
.Set("EmployeeId", employee.EmployeeId)
|
.Set("FirstName", employee.FirstName)
|
.Set("LastName", employee.LastName)
|
.Set("Email", employee.Email)
|
.Set("ContactNo", employee.ContactNo)
|
.Set("Gender", employee.Gender)
|
.Set("Department..DepartmentName", employee.Department.DepartmentName)
|
.Set("Department.Description", employee.Department.Description)
|
.Set("Department.Status", employee.Department.Status)
|
.Set("Department.CreateDateTime", employee.Department.CreateDateTime)
|
.Set("Department.ModifyDateTime", employee.Department.ModifyDateTime)
|
.Set("Status", employee.Status);
|
// _MongoEmpCollection.Update()
|
|
return true;
|
}
|
|
}
|
}
|