public GetMembersModel GetMembers(MemberFilterModel filters) { if (!string.IsNullOrEmpty(filters.searchType) && filters.searchType.Equals("updated")) { var param = HttpContext.Current.Request.Params; var fromDate = param.Get("searchText[from]").Replace(" GMT+0700 (Indochina Time)", ""); var toDate = param.Get("searchText[to]").Replace(" GMT+0700 (Indochina Time)", ""); filters.searchText = $"{fromDate},{toDate}"; } var members = this.Members.Select(m => new GetMember { id = m.id, email = m.email, firstname = m.firstname, lastname = m.lastname, position = m.position, role = m.role, updated = m.updated }).OrderByDescending(m => m.updated); var memberFilter = new GetMembersModel { items = members.Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(), totalItems = members.Count() }; if (!string.IsNullOrEmpty(filters.searchType) && !string.IsNullOrEmpty(filters.searchText)) { string searchText = filters.searchText; string searchType = filters.searchType; IEnumerable <GetMember> searchMembers = new GetMember[] { }; switch (searchType) { case "updated": var dates = searchText.Split(','); DateTime fromDate = DateTime.Parse(dates[0]); DateTime toDate = DateTime.Parse(dates[1]); searchMembers = from m in members where m.updated >= fromDate && m.updated <= toDate select m; break; case "role": searchText = Enum.Parse(typeof(RoleAccount), searchText).ToString(); searchMembers = from m in members where m.GetType(). GetProperty(filters.searchType). GetValue(m). ToString(). Contains(searchText) select m; break; default: searchMembers = from m in members where m.GetType(). GetProperty(filters.searchType). GetValue(m). ToString(). ToLower(). Contains(searchText.ToLower()) select m; break; } //if (filters.searchType.Equals("role")) //{ // searchText = Enum.Parse(typeof(RoleAccount), searchText).ToString().ToLower(); //} memberFilter.items = searchMembers.Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(); memberFilter.totalItems = searchMembers.Count(); } return(memberFilter); }
public GetMemberModel GetMembers(MemberFilterOptions filters) { var items = this.MemberItem.Select(m => new GetMember { id = m.id, email = m.email, firstname = m.firstname, lastname = m.lastname, position = m.position, role = m.role, updated = m.updated }).OrderByDescending(m => m.updated); var memberItems = new GetMemberModel { items = items .Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(), totalItems = items.Count() }; if (!string.IsNullOrEmpty(filters.searchType) && filters.searchType.Equals("updated")) { var paramItem = HttpContext.Current.Request.Params; var fromDate = paramItem.Get("searchText[from]").Replace(" GMT+0700 (Indochina Time)", ""); var toDate = paramItem.Get("searchText[To]").Replace(" GMT+0700 (Indochina Time)", ""); filters.searchText = $"{fromDate},{toDate}"; } if (!string.IsNullOrEmpty(filters.searchType) && !string.IsNullOrEmpty(filters.searchText)) { string searchText = filters.searchText; string searchType = filters.searchType; IEnumerable <GetMember> searchItem = new GetMember[] { }; switch (searchType) { case "updated": var searchTexts = searchText.Split(','); DateTime FromDate = DateTime.Parse(searchTexts[0]); DateTime ToDate = DateTime.Parse(searchTexts[1]); searchItem = from m in items where FromDate <= m.updated && ToDate >= m.updated select m; break; case "role": searchItem = from m in items where Convert.ToInt16(m.GetType() .GetProperty(filters.searchType) .GetValue(m)) == Convert.ToInt16(searchText) select m; break; default: searchItem = from m in items where m.GetType() .GetProperty(filters.searchType) .GetValue(m) .ToString() .ToLower() .Contains(searchText.ToLower()) select m; break; } memberItems.items = searchItem .Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(); memberItems.totalItems = searchItem.Count(); } return(memberItems); }
// แสดงรายการสมาชิก Pagination และ Filter public GetMemberModel GetMembers(MemberFilterOptions filters) { // ดึงข้อมูล และ คัดข้อมูลที่ต้องการแสดง var items = this.MemberItems.Select(m => new GetMember { id = m.id, firstname = m.firstname, lastname = m.lastname, email = m.email, position = m.position, role = m.role, updated = m.updated }) .OrderByDescending(m => m.updated); // เรียบเรียงข้อมูลให้อยู่ในรูปแบบที่ Frontend ต้องการ var memberItems = new GetMemberModel { items = items .Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(), totalItems = items.Count() }; // ตรวจสอบการ ค้นหาข้อมูลจากวันที่ หากมีข้อมูลมาก็ทำการ เรียบเรียงข้อมูลใหม่ if (!string.IsNullOrEmpty(filters.searchType) && filters.searchType.Equals("updated")) { var paramItem = HttpContext.Current.Request.Params; var fromDate = paramItem.Get("searchText[from]").Replace(" GMT+0700 (Indochina Time)", ""); var toDate = paramItem.Get("searchText[to]").Replace(" GMT+0700 (Indochina Time)", ""); filters.searchText = $"{fromDate},{toDate}"; } // หากว่ามีการค้นหาข้อมูลเข้ามาในระบบ if (!string.IsNullOrEmpty(filters.searchType) && !string.IsNullOrEmpty(filters.searchText)) { string searchText = filters.searchText; string searchType = filters.searchType; IEnumerable <GetMember> searchItem = new GetMember[] { }; switch (searchType) { // ค้นหาจากวันที่ case "updated": var searchTexts = searchText.Split(','); DateTime fromDate = DateTime.Parse(searchTexts[0]); DateTime toDate = DateTime.Parse(searchTexts[1]); searchItem = from m in items where m.updated >= fromDate && m.updated <= toDate select m; break; // ค้นหาจากสิทธิ์ผู้ใช้งาน case "role": searchItem = from m in items where Convert.ToInt16(m.GetType() .GetProperty(filters.searchType) .GetValue(m)) == Convert.ToInt16(searchText) select m; break; // ค้นหาทั่วไป default: searchItem = from m in items where m.GetType() .GetProperty(filters.searchType) .GetValue(m) .ToString() .ToUpper() .Contains(searchText.ToUpper()) select m; break; } memberItems.items = searchItem .Skip((filters.startPage - 1) * filters.limitPage) .Take(filters.limitPage) .ToArray(); memberItems.totalItems = searchItem.Count(); } return(memberItems); }