/// <summary> /// Deletes an employement position from an already existing employee. /// </summary> /// <param name="position"></param> /// <param name="employeeId"></param> /// <returns>True, if position was deleted. False if the operation failed or no update was performed</returns> public bool RemovePositionFromEmployee(EmployementPosition position, ObjectId employeeId) { var collection = db.GetCollection <Employee>("employees"); var update = Builders <Employee> .Update.PullFilter(p => p.Positions, f => f.StartDate == position.StartDate && f.FullTimeProcent == position.FullTimeProcent && f.Position == position.Position && f.WorkLocationId == position.WorkLocationId); var result = collection .UpdateOne(p => p.Id == employeeId, update); return(result.IsAcknowledged && result.ModifiedCount >= 1); }
/// <summary> /// Associates a new employement position with a already existing employee. /// </summary> /// <param name="position"></param> /// <param name="employeeId"></param> /// <returns>True, if a successful update took place. False if update failed or no update was performed</returns> public bool AddPositionToEmployee(EmployementPosition position, ObjectId employeeId) { if (position.Id == null || position.Id.Equals(ObjectId.Empty)) { position.Id = ObjectId.GenerateNewId(); } var collection = db.GetCollection <Employee>("employees"); var filter = Builders <Employee> .Filter.Eq("Id", employeeId); var update = Builders <Employee> .Update.Push("Positions", position); var result = collection.UpdateOne(filter, update); return(result.IsAcknowledged && result.ModifiedCount >= 1); }
/// <summary> /// Updates the coffee shop where a particular employee works at in a specific employement position. /// </summary> /// <param name="pos">The EmployementPosition associated with the coffee shop</param> /// <param name="employeeId">The id of the employee holding the employement position to be updated</param> /// <returns>True, if a successful update took place. False if update failed or no update was performed</returns> public bool UpdatePositionShopIdOfEmployee(EmployementPosition pos, ObjectId employeeId) { var collection = db.GetCollection <Employee>("employees"); // var filter = Builders<Employee>.Filter.And(Builders<Employee>.Filter.Where(x => x.Id == employeeId), // Builders<Employee>.Filter.ElemMatch(x => x.Positions, x => x.StartDate == pos.StartDate && x.Position == pos.Position)); var filter = Builders <Employee> .Filter.And(Builders <Employee> .Filter.Where(x => x.Id == employeeId), Builders <Employee> .Filter.ElemMatch(x => x.Positions, x => x.Id == pos.Id)); var update = Builders <Employee> .Update.Set("Positions.$.ShopId", pos.ShopId); var result = collection.UpdateOne(filter, update); return(result.IsAcknowledged && result.ModifiedCount >= 1); }
public void AddEmployees() { var collLocations = db.GetCollection <Location>("locations"); var locationUs = collLocations.AsQueryable().Where(l => l.Currency == Currency.USD).Single(); var locationSwe = collLocations.AsQueryable().Where(l => l.Currency == Currency.SEK).Single(); var collEmployee = db.GetCollection <Employee>("employees"); EmployementPosition shopWorkerUs1pos1 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2016, 4, 29, 00, 00, 00), EndDate = new DateTime(2017, 5, 30, 00, 00, 00), Position = 1, FullTimeProcent = 100, WorkLocationId = locationUs.Id, ShopId = locationUs.Shops.Where(s => s.Address.Street == "Main square 4" && s.Address.City == "SF").Single().Id }; EmployementPosition shopWorkerUs1pos2 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2017, 6, 01, 00, 00, 00), Position = 1, FullTimeProcent = 100, WorkLocationId = locationUs.Id, ShopId = locationUs.Shops.Where(s => s.Address.Street == "Main square 4" && s.Address.City == "SF").Single().Id, }; List <EmployementPosition> positionsUS1 = new List <EmployementPosition>(); positionsUS1.Add(shopWorkerUs1pos1); positionsUS1.Add(shopWorkerUs1pos2); Employee empUS1 = new Employee() { Name = "Tom Smith", PersonalId = "TSSFDR23", HireDate = new DateTime(2016, 4, 29, 00, 00, 00), Address = new Address() { City = "San Francisco", Street = "Test avenue 34", Country = "US", Zip = "23445" }, Positions = positionsUS1, Comments = new List <Comment>(), }; EmployementPosition shopWorkerUs2pos1 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2017, 4, 29, 00, 00, 00), Position = 1, FullTimeProcent = 50, WorkLocationId = locationUs.Id, ShopId = locationUs.Shops.Where(s => s.Address.Street == "Example street 23" && s.Address.City == "SF").Single().Id, }; List <EmployementPosition> positionsUS2 = new List <EmployementPosition>(); positionsUS2.Add(shopWorkerUs2pos1); Employee empUS2 = new Employee() { Name = "Anna L", PersonalId = "ALDTRE75", HireDate = new DateTime(2017, 4, 29, 00, 00, 00), Address = new Address() { City = "Oakland", Street = "Test stret 1", Country = "US", Zip = "45634" }, Positions = positionsUS2, Comments = new List <Comment>(), }; List <Employee> employees = new List <Employee>(); employees.Add(empUS1); employees.Add(empUS2); Shop sweShop = locationSwe.Shops.Where(s => s.Address.Street == "Storgatan 2").Single(); EmployementPosition shopWorkerSwe1pos1 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2015, 4, 29, 00, 00, 00), EndDate = new DateTime(2016, 5, 30, 00, 00, 00), Position = 1, FullTimeProcent = 100, WorkLocationId = locationSwe.Id, ShopId = sweShop.Id, }; EmployementPosition shopWorkerSwe1pos2 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2016, 6, 01, 00, 00, 00), Position = 1, FullTimeProcent = 100, WorkLocationId = locationSwe.Id, ShopId = sweShop.Id, }; List <EmployementPosition> positionSwe1 = new List <EmployementPosition>(); positionSwe1.Add(shopWorkerSwe1pos1); positionSwe1.Add(shopWorkerSwe1pos2); Employee empSwe1 = new Employee() { Name = "Lars Andersson", PersonalId = "8902135300", HireDate = new DateTime(2015, 4, 29, 00, 00, 00), Address = new Address() { City = "Malmoe", Street = "Test gatan 5", Country = "Sweden", Zip = "25463" }, Positions = positionSwe1, Comments = new List <Comment>(), }; employees.Add(empSwe1); List <EmployementPosition> positionSwe2 = new List <EmployementPosition>(); positionSwe2.Add(shopWorkerSwe1pos2); Employee empSwe2 = new Employee() { Name = "Elza M", PersonalId = "9202135300", HireDate = new DateTime(2016, 6, 01, 00, 00, 00), Address = new Address() { City = "Malmoe", Street = "Test torget 15", Country = "Sweden", Zip = "25463" }, Positions = positionSwe2, Comments = new List <Comment>(), }; employees.Add(empSwe2); try { collEmployee.InsertMany(employees); } catch (Exception ex) { Error = "Error adding coffee shop employees to the database: " + ex.Message; throw; } var bossOverEmployeesUS = collEmployee.AsQueryable().Where(e => e.Positions.Any(p => p.WorkLocationId == locationUs.Id && p.EndDate < new DateTime(100, 01, 01, 00, 00, 00))); List <ObjectId> bossOverList = new List <ObjectId>(); foreach (var emp in bossOverEmployeesUS) { bossOverList.Add(emp.Id); } EmployementPosition bosPossUs1 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2017, 1, 01, 00, 00, 00), Position = 2, FullTimeProcent = 100, WorkLocationId = locationUs.Id, BossOver = bossOverList, }; List <EmployementPosition> bossPositionsUS1 = new List <EmployementPosition>(); bossPositionsUS1.Add(bosPossUs1); Employee bossUS1 = new Employee() { Name = "Kate M", PersonalId = "KMDRET28", HireDate = new DateTime(2017, 1, 01, 00, 00, 00), Address = new Address() { City = "San Francisco", Street = "Test street 234", Country = "US", Zip = "98756" }, Positions = bossPositionsUS1, Comments = new List <Comment>(), }; List <Employee> bosses = new List <Employee>(); bosses.Add(bossUS1); var bossOverEmployeesSwe = collEmployee.AsQueryable().Where(e => e.Positions.Any(p => p.WorkLocationId == locationSwe.Id && p.EndDate < new DateTime(100, 01, 01, 00, 00, 00))); List <ObjectId> bossOverListSwe = new List <ObjectId>(); foreach (var emp in bossOverEmployeesSwe) { bossOverListSwe.Add(emp.Id); } EmployementPosition bosPossSwe1 = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2017, 1, 01, 00, 00, 00), Position = 2, FullTimeProcent = 100, WorkLocationId = locationSwe.Id, BossOver = bossOverListSwe, }; List <EmployementPosition> bossPositionsSwe1 = new List <EmployementPosition>(); bossPositionsSwe1.Add(bosPossSwe1); Employee bossSwe1 = new Employee() { Name = "Jon L", PersonalId = "7812065321", HireDate = new DateTime(2017, 1, 01, 00, 00, 00), Address = new Address() { City = "Lund", Street = "Test torget 1", Country = "Sweden", Zip = "25231" }, Positions = bossPositionsSwe1, Comments = new List <Comment>(), }; bosses.Add(bossSwe1); try { collEmployee.InsertMany(bosses); } catch (Exception ex) { Error = "Error adding boss employees to the database: " + ex.Message; throw; } List <ObjectId> bossOverbosses = new List <ObjectId>(); bossOverbosses.Add(bossSwe1.Id); bossOverbosses.Add(bossUS1.Id); EmployementPosition managerPos = new EmployementPosition() { Id = ObjectId.GenerateNewId(), StartDate = new DateTime(2017, 1, 01, 00, 00, 00), Position = 3, FullTimeProcent = 100, WorkLocationId = locationSwe.Id, BossOver = bossOverbosses, }; List <EmployementPosition> managerPositions = new List <EmployementPosition>(); managerPositions.Add(managerPos); Employee manager = new Employee() { Name = "Lars G", PersonalId = "7208065334", HireDate = new DateTime(2017, 1, 01, 00, 00, 00), Address = new Address() { City = "Lund", Street = "Test gatan 34", Country = "Sweden", Zip = "25431" }, Positions = managerPositions, Comments = new List <Comment>(), }; try { collEmployee.InsertOne(manager); } catch (Exception ex) { Error = "Error adding manager employee to the database: " + ex.Message; throw; } Comment commentUS = new Comment() { EmployerId = bossUS1.Id, EmployerName = bossUS1.Name, Date = DateTime.Now, Text = "This is a comment about an employee written by an employer", }; Comment commentSwe = new Comment() { EmployerId = bossSwe1.Id, EmployerName = bossSwe1.Name, Date = DateTime.Now, Text = "Det här är en kommentar...", }; try { var filter = Builders <Employee> .Filter.Eq("Id", empUS1.Id); var update = Builders <Employee> .Update.Push("Comments", commentUS); var result = collEmployee.UpdateOne(filter, update); var filter2 = Builders <Employee> .Filter.Eq("Id", empSwe1.Id); var update2 = Builders <Employee> .Update.Push("Comments", commentSwe); var result2 = collEmployee.UpdateOne(filter2, update2); } catch (Exception ex) { Error = ex.Message; throw; } }