/// <summary> /// Get a dependent /// </summary> /// <param name="empid">The employee id</param> /// <param name="id">The unique identifier</param> /// <returns>Returns the dependent data for the given id </returns> public Dependent Get(string empid, string id) { var response = new Dependent(); try { response = Data.Dependent(empid, id); } catch (Exception e) { logger.Error(e); } return response; }
public static async Task<Employee> AddDependent(string empid, Dependent dependent) { using (var context = new DbModel<Employee>()) { var emp = context.Get(empid); if (emp == null) return null; if (emp.Dependents == null) emp.Dependents = new List<Dependent>(); emp.Dependents.Add(dependent); return await context.Update(emp); } }
/// <summary> /// Add Demo data to the database (10 random employee names will be added) /// </summary> /// <param name="count">The count of employees to add (default = 10)</param> /// <returns>Returns a list of names for those employees added</returns> public async Task<IHttpActionResult> Get(int count = 10) { try { var employees = new List<string>(); using (var context = new DbModel<Employee>()) { for (int i = 0; i < count; i++) { string name = RandomName; int age = rnd.Next(28, 45); employees.Add(name); var newEmployee = new Employee { Name = name, Email = name.Replace(' ', '.') + "@test.us", Age = age, PaycheckAmount = 2000.00, PaychecksPerYear = 26, HireDate = DateTime.Now.AddDays(-rnd.Next(500, (age - 22) * 300)), Dependents = new List<Dependent>() }; // Add some dependents for (int j = 0; j < rnd.Next(0, 4); j++) { var dependent = new Dependent { Name = RandomName, Age = rnd.Next(age++, age + 10) }; newEmployee.Dependents.Add(dependent); } await context.Add(newEmployee); } } return Ok(employees); } catch (Exception e) { logger.Error(e); return InternalServerError(e); } }