Example #1
0
        public async Task <object> GetAllDepartment(SearchDepartmentModel model)
        {
            var departments = await _unitOfWork.DepartmentRepository.Get(model);

            dynamic result;
            List <Dictionary <string, object> > listModel = new List <Dictionary <string, object> >();

            if (!string.IsNullOrEmpty(model.Fields))
            {
                string[] filter = model.Fields.Split(",");
                foreach (var dep in departments)
                {
                    Dictionary <string, object> dictionnary = new Dictionary <string, object>();
                    for (int i = 0; i < filter.Length; i++)
                    {
                        switch (filter[i].Trim())
                        {
                        case "DepartmentId":
                            dictionnary.Add("DepartmentId", dep.DepartmentId);
                            break;

                        case "DepartmentNm":
                            dictionnary.Add("DepartmentNm", dep.DepartmentNm);
                            break;

                        case "Hotline":
                            dictionnary.Add("Hotline", dep.Hotline);
                            break;

                        case "ManagerId":
                            dictionnary.Add("ManagerId", dep.ManagerId);
                            break;

                        case "Manager":
                            dictionnary.Add("Manager", dep.Manager);
                            break;

                        case "RoomNum":
                            dictionnary.Add("RoomNum", dep.RoomNum);
                            break;
                        }
                    }
                    listModel.Add(dictionnary);
                }
                result = listModel;
            }
            else
            {
                result = departments;
            }

            return(new {
                data = result,
                totalCount = departments.TotalCount,
                totalPages = departments.TotalPages
            });
        }
        public async Task <PagedList <DepartmentModel> > Get(SearchDepartmentModel model)
        {
            var query = _context.Department.Where(d => (d.DelFlg == false) &&
                                                  (model.DepartmentNm == null || d.DepartmentNm.Contains(model.DepartmentNm)))
                        .Select(s => new DepartmentModel
            {
                DepartmentId = s.DepartmentId,
                DepartmentNm = s.DepartmentNm,
                Hotline      = s.Hotline,
                RoomNum      = s.RoomNum,
                Manager      = s.Manager.StaffNavigation.FullName,
                DelFlg       = s.DelFlg,
                InsBy        = s.InsBy,
                InsDatetime  = s.InsDatetime,
                UpdBy        = s.UpdBy,
                UpdDatetime  = s.UpdDatetime
            });


            var totalCount = await query.CountAsync();

            List <DepartmentModel> result = null;

            if (model.SortBy == Constants.SortBy.SORT_NAME_ASC)
            {
                query = query.OrderBy(t => t.DepartmentNm);
            }
            else if (model.SortBy == Constants.SortBy.SORT_NAME_DES)
            {
                query = query.OrderByDescending(t => t.DepartmentNm);
            }

            result = await query.Skip(model.Size *(model.Page - 1))
                     .Take(model.Size)
                     .ToListAsync();

            return(PagedList <DepartmentModel> .ToPagedList(result, totalCount, model.Page, model.Size));
        }
        public async Task <IActionResult> GetAllDepartments([FromQuery] SearchDepartmentModel model)
        {
            var result = await _departmentService.GetAllDepartment(model);

            return(Ok(result));
        }