Beispiel #1
0
        public IActionResult Add(EmployeeRequest employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Employee manager = null;

            if (!string.IsNullOrEmpty(employee.Manager))
            {
                manager = _employeesRepo.GetByName(employee.Manager);
                if (manager == null)
                {
                    ModelState.AddModelError(nameof(employee.Manager), "Manager is not be found");
                    return(BadRequest(ModelState));
                }
            }

            Employee emp = new Employee();

            emp.Id         = employee.Id;
            emp.Name       = employee.Name;
            emp.Department = employee.Department;
            emp.Position   = employee.Position;
            emp.ManagerId  = manager?.Id;
            emp.StartDate  = employee.StartDate;

            _employeesRepo.Add(emp);
            return(Ok());
        }
Beispiel #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            string employeeName = value as string;

            if (employeeName == null || employeeName.Length == 0)
            {
                return(ValidationResult.Success);
            }

            IEmployeesRepo employeesRepo = (IEmployeesRepo)validationContext.GetService(typeof(IEmployeesRepo));
            Employee       employee      = employeesRepo.GetByName(employeeName);

            if (employee == null)
            {
                return(new ValidationResult("Not found"));
            }
            return(ValidationResult.Success);
        }