Commit e3b1cbd0 authored by Sebastian Barbosa's avatar Sebastian Barbosa
Browse files

Quedaron los read y list para EF y Mongo

parent 8a7d26dc
Pipeline #52 skipped
......@@ -34,7 +34,7 @@ namespace BusinessLogicLayer
public List<Employee> GetAllEmployees()
{
throw new NotImplementedException();
return _dal.GetAllEmployees();
}
public Employee GetEmployee(int id)
......
using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer
{
public class DALEmployeesEF : IDALEmployees
{
public void AddEmployee(Employee emp)
{
throw new NotImplementedException();
}
public void DeleteEmployee(int id)
{
throw new NotImplementedException();
}
//Bruno
public void UpdateEmployee(Employee emp)
{
using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer
{
public class DALEmployeesEF : IDALEmployees
{
public void AddEmployee(Employee emp)
{
throw new NotImplementedException();
}
public void DeleteEmployee(int id)
{
throw new NotImplementedException();
}
//Bruno
public void UpdateEmployee(Employee emp)
{
using (var context = new Model.tsi_pr1Entities())
{
var result = from e in context.Employees
......@@ -44,19 +44,31 @@ namespace DataAccessLayer
original.StartDate = emp.StartDate;
context.SaveChanges();
}
}
}
}
public List<Employee> GetAllEmployees()
{
throw new NotImplementedException();
}
public Employee GetEmployee(int id)
}
}
}
public List<Employee> GetAllEmployees()
{
// http://www.dreamincode.net/forums/topic/329803-creating-an-easy-data-access-layer-using-entity-framework/
List<Employee> employees = new List<Employee>();
using (var context = new Model.tsi_pr1Entities())
{
var result = from emp in context.Employees
select emp;
foreach (Model.Employee emp in result)
{
employees.Add(fromModel(emp));
}
}
return employees;
}
public Employee GetEmployee(int id)
{
using (var context = new Model.tsi_pr1Entities())
{
......@@ -64,34 +76,66 @@ namespace DataAccessLayer
where emp.Id == id
select emp;
// TODO: Ver si se quiere utilizar algun plugin externo para realizar el mapeo, por lo que vi esta el AutoMapper, algunos les tiran palos pero porque
// tienen base de datos muy complejas con relaciones entre tabla y ahi no es muy performante, pero con nuestra tabla pedorra no creo que haya drama y te ahorras
// este fardo
Employee employee = null;
if (result.Count() == 1) {
Model.Employee modelEmployee = result.First();
Employee employee = result.Count() == 1 ? fromModel(result.First()) : null;
return employee;
}
}
// Rustico, rustico señores.
if (modelEmployee is Model.FullTimeEmployee)
{
Shared.Entities.FullTimeEmployee tmp = new FullTimeEmployee();
tmp.Salary = ((Model.FullTimeEmployee) modelEmployee).Salary;
employee = tmp;
}
else
{
Shared.Entities.PartTimeEmployee tmp = new PartTimeEmployee();
tmp.HourlyRate = ((Model.PartTimeEmployee)modelEmployee).HourlyRate;
employee = tmp;
}
employee.Id = modelEmployee.Id;
employee.Name = modelEmployee.Name;
employee.StartDate = modelEmployee.StartDate;
private Shared.Entities.Employee fromModel(Model.Employee employee)
{
Employee result = null;
if (employee != null)
{
if (employee is Model.FullTimeEmployee)
{
Shared.Entities.FullTimeEmployee tmp = new Shared.Entities.FullTimeEmployee();
tmp.Salary = ((Model.FullTimeEmployee)employee).Salary;
result = tmp;
}
else
{
Shared.Entities.PartTimeEmployee tmp = new Shared.Entities.PartTimeEmployee();
tmp.HourlyRate = ((Model.PartTimeEmployee)employee).HourlyRate;
result = tmp;
}
return employee;
}
}
}
}
result.Id = employee.Id;
result.Name = employee.Name;
result.StartDate = employee.StartDate;
}
return result;
}
private Model.Employee toModel(Shared.Entities.Employee employee)
{
Model.Employee result = null;
if (employee != null)
{
if (employee is Shared.Entities.FullTimeEmployee)
{
Model.FullTimeEmployee tmp = new Model.FullTimeEmployee();
tmp.Salary = ((Shared.Entities.FullTimeEmployee)employee).Salary;
result = tmp;
}
else
{
Model.PartTimeEmployee tmp = new Model.PartTimeEmployee();
tmp.HourlyRate = ((Shared.Entities.PartTimeEmployee)employee).HourlyRate;
result = tmp;
}
result.Id = employee.Id;
result.Name = employee.Name;
result.StartDate = employee.StartDate;
}
return result;
}
}
}
......@@ -56,12 +56,30 @@ namespace DataAccessLayer
public List<Employee> GetAllEmployees()
{
throw new NotImplementedException();
List<Employee> employees = new List<Employee>();
var collection = _database.GetCollection<BsonDocument>("EmployeesTPH");
var filter = new BsonDocument();
using (var cursor = collection.FindSync(filter))
{
while (cursor.MoveNext())
{
var batch = cursor.Current;
foreach (var document in batch)
{
employees.Add(fromDocument(document));
}
}
}
return employees;
}
public Employee GetEmployee(int id)
{
// https://docs.mongodb.org/getting-started/csharp/query/
var collection = _database.GetCollection<BsonDocument>("EmployeesTPH");
var filter = Builders<BsonDocument>.Filter.Eq("ID", id);
......@@ -72,35 +90,41 @@ namespace DataAccessLayer
BsonDocument document = cursor.SingleOrDefault();
if (document != null)
{
//BsonDocument document = batch.First();
if (document["TYPE_EMP"].ToInt32() == 1)
{
Shared.Entities.FullTimeEmployee tmp = new FullTimeEmployee();
tmp.Salary = document["SALARY"].ToInt32();
employee = tmp;
}
else
{
Shared.Entities.PartTimeEmployee tmp = new PartTimeEmployee();
tmp.HourlyRate = document["RATE"].ToDouble();
employee = tmp;
}
employee = fromDocument(document);
}
return employee;
}
employee.Id = id;
employee.Name = document["NAME"].ToString();
//TODO: Creo que hay que insertar la fecha en un formato que despues podamos convetirla, por ejemplo el formato que espera ToLocalTime
//employee.StartDate = document["START_DATE"].;
private Shared.Entities.Employee fromDocument(BsonDocument document)
{
Employee result = null;
if (document != null)
{
if (document["TYPE_EMP"].ToInt32() == 1)
{
Shared.Entities.FullTimeEmployee tmp = new FullTimeEmployee();
tmp.Salary = document["SALARY"].ToInt32();
result = tmp;
}
else
{
Shared.Entities.PartTimeEmployee tmp = new PartTimeEmployee();
tmp.HourlyRate = document["RATE"].ToDouble();
result = tmp;
}
result.Id = document["ID"].ToInt32();
result.Name = document["NAME"].ToString();
result.StartDate = document["START_DATE"].ToLocalTime();
}
return employee;
return result;
}
}
}
......@@ -42,14 +42,13 @@ namespace Test
{
// TODO: Sacar metodo, es para probar nada mas
IBLEmployees blHandler = container.Resolve<IBLEmployees>();
Employee emp = blHandler.GetEmployee(1);
List<Employee> result = blHandler.GetAllEmployees();
//assert
Assert.IsNotNull(emp);
Assert.AreEqual(1, emp.Id);
Assert.IsNotNull(result);
}
[TestMethod()]
//[TestMethod()]
public void UpdateEmployeeMethod()
{
// Este metodo deberia tener un Crear pero sirvio para la primera vez
......
......@@ -15,11 +15,11 @@ namespace Test
{
// TODO: Lo deje apuntando al EF, antes de entregar habria que mandarlo al Mock (Lo que esta comentado abajo)
//register the concrete implementation for interfaces
container.RegisterType<IDALEmployees, DALEmployeesEF>();
container.RegisterType<IDALEmployees, DALEmployeesMongo>();
container.RegisterType<IBLEmployees, BLEmployees>();
//register a singleton for DAL
DALEmployeesEF dalEmployeesEF = new DALEmployeesEF();
DALEmployeesMongo dalEmployeesEF = new DALEmployeesMongo();
container.RegisterInstance(dalEmployeesEF);
//register a singleton for BL
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment