Skip to content
Snippets Groups Projects
Commit a6f3c700 authored by Sebastian Barbosa's avatar Sebastian Barbosa
Browse files

Merge branch 'master' into servicelayer

parents 2c371063 ec9a302e
Branches servicelayer
No related tags found
No related merge requests found
Pipeline #
...@@ -51,9 +51,7 @@ namespace DataAccessLayer ...@@ -51,9 +51,7 @@ namespace DataAccessLayer
} }
} }
//throw new NotImplementedException();
} }
public void UpdateEmployee(Employee emp) public void UpdateEmployee(Employee emp)
......
...@@ -6,38 +6,50 @@ using MongoDB.Bson; ...@@ -6,38 +6,50 @@ using MongoDB.Bson;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shared;
namespace DataAccessLayer namespace DataAccessLayer
{ {
public class DALEmployeesMongo : IDALEmployees public class DALEmployeesMongo : IDALEmployees
{ {
// Documentacion sobre MongoDB y C#
// https://docs.mongodb.org/getting-started/csharp/update/
private IMongoClient _client;
private IMongoDatabase _database;
//TODO: Ver si esta bien tener el cliente y la base estatica en la tabla, me imagino que abre una sesion y no expira pero anda a saber public DALEmployeesMongo()
// No veo nada de pool y de keep alive, no debe de ser muy prolijo tenerlo aca
protected static IMongoClient _client;
protected static IMongoDatabase _database;
static DALEmployeesMongo()
{ {
_client = new MongoClient(); _client = new MongoClient();
_database = _client.GetDatabase("tsi_pr1"); _database = _client.GetDatabase("tsi_pr1");
} }
public void AddEmployee(Employee emp) public void AddEmployee(Employee emp)
{ {
throw new NotImplementedException(); var collection = _database.GetCollection<BsonDocument>("EmployeesTPH");
BsonDocument result = collection.Find(new BsonDocument()).Sort(new BsonDocument("ID", -1)).FirstOrDefault();
var document = new BsonDocument
{
{ "ID", result == null ? 1 : result["ID"].ToInt32() + 1},
{ "NAME", emp.Name},
{ "TYPE_EMP", emp is FullTimeEmployee ? 1 : 0},
{ emp is FullTimeEmployee ? "SALARY" : "RATE", emp is FullTimeEmployee ? ((FullTimeEmployee)emp).Salary : ((PartTimeEmployee)emp).HourlyRate},
{ "START_DATE", emp.StartDate}
};
collection.InsertOne(document);
} }
public void DeleteEmployee(int id) public void DeleteEmployee(int id)
{ {
throw new NotImplementedException(); var collection = _database.GetCollection<BsonDocument>("EmployeesTPH");
var filter = Builders<BsonDocument>.Filter.Eq("ID", id);
collection.DeleteOne(filter);
} }
//Bruno
public void UpdateEmployee(Employee emp) public void UpdateEmployee(Employee emp)
{ {
// https://docs.mongodb.org/getting-started/csharp/update/
var collection = _database.GetCollection<BsonDocument>("EmployeesTPH"); var collection = _database.GetCollection<BsonDocument>("EmployeesTPH");
var filter = Builders<BsonDocument>.Filter.Eq("ID", emp.Id); var filter = Builders<BsonDocument>.Filter.Eq("ID", emp.Id);
var update = Builders<BsonDocument>.Update var update = Builders<BsonDocument>.Update
...@@ -96,23 +108,20 @@ namespace DataAccessLayer ...@@ -96,23 +108,20 @@ namespace DataAccessLayer
return employee; return employee;
} }
private Employee fromDocument(BsonDocument document)
private Shared.Entities.Employee fromDocument(BsonDocument document)
{ {
Employee result = null; Employee result = null;
if (document != null) if (document != null)
{ {
if (document["TYPE_EMP"].ToInt32() == 1) if (document["TYPE_EMP"].ToInt32() == 1)
{ {
Shared.Entities.FullTimeEmployee tmp = new FullTimeEmployee(); FullTimeEmployee tmp = new FullTimeEmployee();
tmp.Salary = document["SALARY"].ToInt32(); tmp.Salary = document["SALARY"].ToInt32();
result = tmp; result = tmp;
} }
else else
{ {
Shared.Entities.PartTimeEmployee tmp = new PartTimeEmployee(); PartTimeEmployee tmp = new PartTimeEmployee();
tmp.HourlyRate = document["RATE"].ToDouble(); tmp.HourlyRate = document["RATE"].ToDouble();
result = tmp; result = tmp;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment