public async Task <PagedList <MdaDeviceAssignee> > GetAssignees(MdaAssigneeQuery queryObj)
        {
            var query = _context.MdaDeviceAssignee
                        .Include(d => d.Department)
                        .Include(a => a.MdaDeviceAssignments).ThenInclude(d => d.Device)
                        .ThenInclude(s => s.Sim)
                        .IgnoreQueryFilters()
                        .AsQueryable();

            // if (queryObj.PageSize == 0)
            //     queryObj.PageSize = 10;

            if (!String.IsNullOrEmpty(queryObj.FirstName))
            {
                //query = query.Where(f => f.FirstName == queryObj.FirstName);
                query = query.Where(f => f.FirstName.Contains(queryObj.FirstName));
            }

            if (!String.IsNullOrEmpty(queryObj.LastName))
            {
                // query = query.Where(l => l.LastName == queryObj.LastName);
                query = query.Where(l => l.LastName.Contains(queryObj.LastName));
            }

            if (queryObj.DepartmentId.HasValue)
            {
                query = query.Where(d => d.DepartmentId == queryObj.DepartmentId);
            }

            if (queryObj.Active == 0)
            {
                query = query.Where(d => d.Active == 0);
            }

            if (queryObj.Active == 1)
            {
                query = query.Where(d => d.Active == 1);
            }

            queryObj.SortBy          = "lastname";
            queryObj.IsSortAscending = true;

            var columnsMap = new Dictionary <string, Expression <Func <MdaDeviceAssignee, object> > >
            {
                ["firstname"]  = a => a.FirstName,
                ["lastname"]   = a => a.LastName,
                ["department"] = a => a.Department
            };

            query = query.ApplyOrdering(queryObj, columnsMap);

            // query = query.ApplyPaging(queryObj);

            // return await query.ToListAsync();
            return(await PagedList <MdaDeviceAssignee> .CreateAsync(query, queryObj.Page, queryObj.PageSize));
        }
        public async Task <IEnumerable <MdaDeviceAssignee> > GetAllAssignees(MdaAssigneeQuery queryObj)
        {
            var query = _context.MdaDeviceAssignee
                        .AsQueryable();

            if (!String.IsNullOrEmpty(queryObj.FirstName))
            {
                //query = query.Where(f => f.FirstName == queryObj.FirstName);
                query = query.Where(f => f.FirstName.Contains(queryObj.FirstName));
            }

            if (!String.IsNullOrEmpty(queryObj.LastName))
            {
                // query = query.Where(l => l.LastName == queryObj.LastName);
                query = query.Where(l => l.LastName.Contains(queryObj.LastName));
            }

            if (queryObj.DepartmentId.HasValue)
            {
                query = query.Where(d => d.DepartmentId == queryObj.DepartmentId);
            }

            if (queryObj.Active == 0)
            {
                query = query.Where(d => d.Active == 0);
            }

            if (queryObj.Active == 1)
            {
                query = query.Where(d => d.Active == 1);
            }

            queryObj.SortBy          = "lastname";
            queryObj.IsSortAscending = true;

            var columnsMap = new Dictionary <string, Expression <Func <MdaDeviceAssignee, object> > >
            {
                ["firstname"]  = a => a.FirstName,
                ["lastname"]   = a => a.LastName,
                ["department"] = a => a.Department
            };

            query = query.ApplyOrdering(queryObj, columnsMap);

            return(await query.ToListAsync());
        }
        public async Task <IActionResult> UpdateAssignee(int id, AssigneeSaveResource assigneeUpdateResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var assigneeFromRepo = await _repo.GetAssignee(id);

            if (assigneeFromRepo == null)
            {
                return(BadRequest($"AssigneeId {id} could not be found"));
            }

            if (assigneeUpdateResource.DepartmentId == 0)
            {
                assigneeUpdateResource.DepartmentId = null;
            }


            var filter = new MdaAssigneeQuery()
            {
                FirstName = assigneeUpdateResource.FirstName,
                LastName  = assigneeUpdateResource.LastName,
                Active    = 2
            };

            var assigneeFromRepoExisting = await _repo.GetAssignees(filter);

            if (assigneeFromRepoExisting.Any())
            {
                var existingAssignee = assigneeFromRepoExisting.FirstOrDefault();
                if (existingAssignee.Id != id)
                {
                    return(BadRequest($"Assignee {assigneeUpdateResource.FirstName + ' ' + assigneeUpdateResource.LastName} already exists."));
                }
                else
                {
                    if (existingAssignee.FirstName == assigneeUpdateResource.FirstName &&
                        existingAssignee.LastName == assigneeUpdateResource.LastName &&
                        existingAssignee.DepartmentId == assigneeUpdateResource.DepartmentId)
                    {
                        if (existingAssignee.Active == Convert.ToByte(assigneeUpdateResource.Active == true ? 1 : 0))
                        {
                            return(BadRequest("Nothing was changed"));
                        }
                    }
                }
            }

            _mapper.Map(assigneeUpdateResource, assigneeFromRepo);
            assigneeFromRepo.ModifiedBy   = User.Identity.Name;
            assigneeFromRepo.ModifiedDate = DateTime.Now;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update assignee"));
        }