/// <summary> /// Adds the staff performance objective to the repository. /// </summary> /// <param name="newObjective">The new objective.</param> /// <param name="pfNumber">The pf number of staff.</param> /// <returns> /// Flag indicating whether the objective was successfully a /// </returns> public bool AddObjective(AddObjective newObjective, int pfNumber) { // Transaction scope to be used to add new objective. TransactionScope scope = new TransactionScope(); try { if (this.objRules.IsPfNumberValid(pfNumber)) { if (this.objRules.IsObjectiveAddPostValid(pfNumber, newObjective.CycleId, newObjective.Weight)) { EmployeeDirectory employee = new EmployeeDirectory(); int empId = employee.GetEmployeeId(pfNumber.ToString()); newObjective.EmployeeId = empId; if (empId != -1) { // Setup transaction scope. using (scope) { // // Add the Objective record to the database. Objective newEmployeeObjective = new Objective { CycleId = newObjective.CycleId, Title = newObjective.Objective }; this.db.Objectives.Add(newEmployeeObjective); this.db.SaveChanges(); // // Add the EmployeeObjective record to the database. newObjective.ObjectiveId = newEmployeeObjective.Id; // Inserted objective record Id EmployeeObjective empObjectiveRecord = new EmployeeObjective { EmployeeId = newObjective.EmployeeId, ObjectiveId = newObjective.ObjectiveId, Weight = newObjective.Weight }; this.db.EmployeeObjectives.Add(empObjectiveRecord); this.db.SaveChanges(); // // Complete the transaction scope and submit changes. scope.Complete(); } return true; } else { return false; } } else { return false; } } else { return false; } } catch (DataException) { // // Handle exception. scope.Dispose(); return false; } catch (Exception) { // // Handle exception. scope.Dispose(); return false; } }
public bool Edit(int? cycle, int empPfNumber, EditObjective objectiveUpdate) { // // Scope that will be used for the edit transaction. TransactionScope scope = new TransactionScope(); try { using (scope) { if (this.objRules.IsPfNumberValid(empPfNumber)) { if (this.objRules.IsObjectiveEditPostValid(empPfNumber, objectiveUpdate, cycle)) { Objective performanceObjective = new Objective { Id = objectiveUpdate.ObjectiveId, CycleId = objectiveUpdate.CycleId, Title = objectiveUpdate.Objective }; this.db.Entry(performanceObjective).State = System.Data.Entity.EntityState.Modified; EmployeeObjective empObjective = new EmployeeObjective { Id = objectiveUpdate.EmployeeObjectiveId, EmployeeId = objectiveUpdate.EmployeeId, ObjectiveId = objectiveUpdate.ObjectiveId, Weight = objectiveUpdate.Weight }; this.db.Entry(empObjective).State = System.Data.Entity.EntityState.Modified; this.db.SaveChanges(); // // Complete the scope. scope.Complete(); //return the operation flag. return true; } else { // Objective edit is invalid. scope.Dispose(); return false; } } else { // user making edit is invalid. scope.Dispose(); return false; } } } catch (DataException) { // Handle exception. scope.Dispose(); return false; } catch (TransactionException) { // Handle exception. scope.Dispose(); return false; } catch (Exception) { // Handle exception. scope.Dispose(); return false; } }