Example #1
0
        public void MonthlyEmployeeFactory_Create_CreatesEmployee()
        {
            // arrange
            var basicEmployee = new BasicEmployee
            {
                Id               = 50,
                Name             = "Employee 2",
                ContractTypeName = "MonthlySalaryEmployee",
                RoleId           = 200,
                RoleName         = "Role Name",
                RoleDescription  = "Role Description",
                HourlySalary     = 160,
                MonthlySalary    = 220
            };
            var expectedSalary = basicEmployee.MonthlySalary * 12;
            var factory        = new MonthlyEmployeeFactory();

            // act
            var hourlyEmployee = factory.Create(basicEmployee);

            // assert
            Assert.IsNotNull(hourlyEmployee);
            Assert.IsInstanceOfType(hourlyEmployee, typeof(MonthlyEmployee));
            Assert.AreEqual(basicEmployee.Id, hourlyEmployee.Id);
            Assert.AreEqual(basicEmployee.RoleId, hourlyEmployee.Role.Id);
            Assert.AreEqual(expectedSalary, hourlyEmployee.AnnualSalary);
        }
Example #2
0
 public HubEmployee CreateEmployee(BasicEmployee employeeData)
 {
     // Select the factory according to the Contract Type
     if (!_factories.ContainsKey(employeeData.ContractTypeName))
     {
         return(null);
     }
     else
     {
         return(_factories[employeeData.ContractTypeName].Create(employeeData));
     }
 }
        public void HubEmployeeCreator_CreateEmployee_ReturnsNullWithRandomContract()
        {
            // arrange
            var basicEmployee = new BasicEmployee
            {
                Id = 56,
                ContractTypeName = Guid.NewGuid().ToString()
            };
            var creator = new HubEmployeeCreator();

            // act
            var hubEmployee = creator.CreateEmployee(basicEmployee);

            // assert
            Assert.IsNull(hubEmployee);
        }
        public async Task <EmployeeDetailsModel> GetEmplyeeDetails(int employeeId)
        {
            // you can implement a dictionary here with the requests that have come in and their data
            // and perhaps a time stamp. So, when a request comes along, you can check if it was alrady
            // cached and if it was recently enough - skip calling the database and return from memory
            // the data caching and retrieval is entirely up to the application and its practices/needs
            BasicEmployee        employee = data.Where(empl => empl.Id == employeeId).FirstOrDefault();
            EmployeeDetailsModel details  = new EmployeeDetailsModel(employee);

            details.Salary         = rnd.Next(1000, 5000);
            details.ActiveProjects = rnd.Next(2, 10);
            details.HireDate       = DateTime.Now.AddYears(-rnd.Next(1, 10)).AddMonths(-rnd.Next(0, 10)).AddDays(-rnd.Next(0, 10));

            await Task.Delay(800); // simulate real life delay. Remove this in a real app

            return(details);
        }
        public void HubEmployeeCreator_CreateEmployee_CanCreateMonthlyEmployee()
        {
            // arrange
            var basicEmployee = new BasicEmployee
            {
                Id = 34,
                ContractTypeName = "MonthlySalaryEmployee"
            };
            var creator = new HubEmployeeCreator();

            // act
            var hubEmployee = creator.CreateEmployee(basicEmployee);

            // assert
            Assert.IsNotNull(hubEmployee);
            Assert.IsInstanceOfType(hubEmployee, typeof(MonthlyEmployee));
            Assert.AreEqual(basicEmployee.Id, hubEmployee.Id);
        }
        /// <summary>
        /// Creates a HourlyEmployee calculating annual salary as:
        /// 120 * HourlySalary * 12
        /// </summary>
        /// <param name="data">Basic employee information</param>
        /// <returns>Converted user with annual salary</returns>
        public override HubEmployee Create(BasicEmployee data)
        {
            var role = new HubRole
            {
                Id          = data.RoleId,
                Name        = data.Name,
                Description = data.RoleDescription
            };

            var employee = new HourlyEmployee
            {
                Id           = data.Id,
                Name         = data.Name,
                HourlySalary = data.HourlySalary,
                AnnualSalary = data.HourlySalary * MonthsInYear * HoursInMonth,
                Role         = role
            };

            return(employee);
        }
        public ActionResult EditTraining(int id, BasicEmployee employee)
        {
            try
            {
                DeleteAllUpcomingTrianing(id);
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        foreach (TrainingSelect training in employee.TrainingList)
                        {
                            if (training.isSelected)
                            {
                                cmd.CommandText = @"INSERT INTO EmployeeTraining (TrainingProgramId, EmployeeId)
                                            OUTPUT INSERTED.id
                                            VALUES (@trainigProgramId, @employeeId)";

                                cmd.Parameters.Add(new SqlParameter("@trainigProgramId", training.Id));
                                cmd.Parameters.Add(new SqlParameter("@employeeId", employee.Id));



                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                        }
                    }
                }

                // RedirectToAction("Index");
                return(RedirectToAction(nameof(Details), new { id = id }));
            }
            catch (Exception ex)
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
 /// <summary>
 /// Creates an employee from the provided data
 /// </summary>
 /// <param name="data">Basic employee information</param>
 /// <returns>Converted user with annual salary</returns>
 public abstract HubEmployee Create(BasicEmployee data);
        // GET: Employees/Details/5
        public ActionResult Details(int id)
        {
            var trainings = GetAllTrainingPrograms().Select(p => new TrainingSelect
            {
                Name       = p.Name,
                Id         = p.Id,
                isSelected = false
            }).ToList();

            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                        SELECT e.Id, e.FirstName, e.LastName, d.Name AS DepartmentName,
                                c.Make, c.Model, tp.Name AS TrainingName
                        FROM Employee e
                        LEFT JOIN Computer c ON ComputerId = c.Id
                        LEFT JOIN Department d ON e.DepartmentId = d.Id
                        LEFT JOIN EmployeeTraining et ON et.EmployeeId = e.id
                        LEFT JOIN TrainingProgram tp ON et.TrainingProgramId = tp.Id
                        
                        WHERE e.Id = @id";

                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    SqlDataReader reader = cmd.ExecuteReader();

                    BasicEmployee employee = null;

                    while (reader.Read())
                    {
                        if (employee == null)
                        {
                            employee = new BasicEmployee
                            {
                                Id             = reader.GetInt32(reader.GetOrdinal("Id")),
                                FirstName      = reader.GetString(reader.GetOrdinal("FirstName")),
                                LastName       = reader.GetString(reader.GetOrdinal("LastName")),
                                DepartmentName = reader.GetString(reader.GetOrdinal("DepartmentName")),
                                Computer       = reader.GetString(reader.GetOrdinal("Make")) + " " + reader.GetString(reader.GetOrdinal("Model")),
                            };

                            if (!reader.IsDBNull(reader.GetOrdinal("TrainingName")))
                            {
                                employee.EmployeeTrainings.Add(reader.GetString(reader.GetOrdinal("TrainingName")));
                            }
                        }
                        else if (!reader.IsDBNull(reader.GetOrdinal("TrainingName")))
                        {
                            employee.EmployeeTrainings.Add(reader.GetString(reader.GetOrdinal("TrainingName")));
                        }
                    }
                    reader.Close();

                    if (employee == null)
                    {
                        return(NotFound($"No Employee found with the ID of {id}"));
                    }
                    foreach (TrainingSelect training in trainings)
                    {
                        if (employee.EmployeeTrainings.Any(e => e == training.Name))
                        {
                            training.isSelected = true;
                        }
                    }

                    employee.TrainingList = trainings;


                    return(View(employee));
                }
            }
        }
        // GET: TrainingPrograms/Details/5
        public ActionResult Details(int id)
        {
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                        SELECT tp.Id AS TrainingId, tp.[Name] AS TrainingName, tp.StartDate, tp.EndDate, tp.MaxAttendees, e.FirstName, e.LastName, e.Id AS EmployeeId
                        FROM TrainingProgram tp
                        LEFT JOIN EmployeeTraining et ON et.TrainingProgramId = tp.Id
                        LEFT JOIN Employee e ON et.EmployeeId = e.Id
                        WHERE tp.Id = @id";

                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    SqlDataReader reader = cmd.ExecuteReader();

                    TrainingWithEmployees trainingProgram = null;

                    while (reader.Read())
                    {
                        if (trainingProgram == null)
                        {
                            trainingProgram = new TrainingWithEmployees
                            {
                                Id = reader.GetInt32(reader.GetOrdinal("TrainingId")),
                                Name = reader.GetString(reader.GetOrdinal("TrainingName")),
                                StartDate = reader.GetDateTime(reader.GetOrdinal("StartDate")),
                                EndDate = reader.GetDateTime(reader.GetOrdinal("EndDate")),
                                MaxAttendees = reader.GetInt32(reader.GetOrdinal("MaxAttendees"))
                            };
                            if (!reader.IsDBNull(reader.GetOrdinal("FirstName")))
                            {
                                var employee = new BasicEmployee
                                {
                                    Id = reader.GetInt32(reader.GetOrdinal("EmployeeId")),
                                    FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                                    LastName = reader.GetString(reader.GetOrdinal("LastName")),
                                };

                                trainingProgram.EmployeesAttending.Add(employee);

                            }
                        } else if (!reader.IsDBNull(reader.GetOrdinal("FirstName")))
                        {
                            var employee = new BasicEmployee
                            {
                                Id = reader.GetInt32(reader.GetOrdinal("EmployeeId")),
                                FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                                LastName = reader.GetString(reader.GetOrdinal("LastName")),
                            };

                            trainingProgram.EmployeesAttending.Add(employee);
                        }
                    }
                    reader.Close();
                    

                    if (trainingProgram == null)
                    {
                        return NotFound($"No Training Program found with the ID of {id}");
                    }

                    return View(trainingProgram);
                }
            }
            
        }