Beispiel #1
0
        //public async Task<DepartmentViewResult> Get(int id)
        //{

        //    var department = await _dbContext.Departments.FirstOrDefaultAsync(d => d.Id == id);

        //    if (department == null)
        //        return null;
        //    var managerId = department.ManagerId;
        //    var manager = await _dbContext.Employees.FirstOrDefaultAsync(e => e.Id == managerId);
        //    if (manager != null)
        //    {
        //        return new DepartmentViewResult
        //        {
        //            Id = department.Id,
        //            Name = department.Name,
        //            ManagerName = manager.Name
        //        };
        //    }
        //    else
        //    {
        //        return new DepartmentViewResult
        //        {
        //            Id = department.Id,
        //            Name = department.Name,
        //        };
        //    }
        //}


        //auto Mapper
        public async Task <DepartmentViewResult> Get(DepartmentSearchParameters parameters)
        {
            var query = _dbContext.Departments
                        .Include(d => d.Manager)
                        .Include(d => d.Employees)
                        .AsQueryable();

            List <EmployeeViewResult> employees = new List <EmployeeViewResult>();

            if (parameters.Id.HasValue)
            {
                query = query.Where(d => d.Id == parameters.Id);
            }
            if (parameters.ManagerId.HasValue)
            {
                query = query.Where(e => e.ManagerId.Equals(parameters.ManagerId));
            }
            if (!string.IsNullOrWhiteSpace(parameters.Name))
            {
                query = query.Where(e => e.Name.Contains(parameters.Name));
            }

            var department = await query.FirstOrDefaultAsync();


            if (department == null)
            {
                return(null);
            }

            if (department.Employees != null && department.Employees.Any())
            {
                foreach (var item in department.Employees)
                {
                    employees.Add(new EmployeeViewResult
                    {
                        DepartmentName = department.Name,
                        Id             = item.Id,
                        Name           = item.Name,
                        Email          = item.Email,
                        JobTitle       = item.JobTitle,
                        PhoneNumber    = item.PhoneNumber,
                        ManagerName    = department.Manager?.Name,
                    });
                }
            }

            return(new DepartmentViewResult
            {
                Id = department.Id,
                Name = department.Name,
                ManagerName = department.Manager?.Name,
                Employees = employees
            });
        }
        public async Task <IActionResult> Get([FromQuery] DepartmentSearchParameters parametrs)
        {
            var department = await _departmentService.Get(parametrs);

            if (department == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(department));
            }
        }
Beispiel #3
0
        public async Task <IEnumerable <Department> > GetDepartmentsAsync(DepartmentSearchParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var departments = context.Departments.Where(d => d.CompanyId == parameters.CompanyId);

            if (departments != null && !string.IsNullOrWhiteSpace(parameters.DepartmentName))
            {
                departments = departments.Where(d => d.Name.Contains(parameters.DepartmentName));
            }
            return(await departments.OrderBy(d => d.Name).ToListAsync());
        }