Пример #1
0
        /**
         * The function search a page of data and return client with specific format
         * { code, error, data: { result, total } }
         */
        public static ResponseContacts search(string fileName, string term, int pageNumber, int pageSize)
        {
            ResponseContacts responseContacts = new ResponseContacts();

            // File does not exist
            if (!File.Exists(fileName))
            {
                responseContacts.code  = 100;
                responseContacts.error = String.Concat(fileName, " Not existed");
                return(responseContacts);
            }

            List <Contact> contacts = Helpers.readFile(fileName);

            // search not Ok
            if (contacts == null)
            {
                responseContacts.code  = 101;
                responseContacts.error = String.Concat("Error occured while reading ", fileName);
                return(responseContacts);
            }

            // filter data with specific page number
            term = term.ToLower();
            var query = contacts.Where(contact => (
                                           ((contact.firstName != null && contact.firstName.ToLower().IndexOf(term) >= 0) || term == String.Empty) ||
                                           ((contact.lastName != null && contact.lastName.ToLower().IndexOf(term) >= 0) || term == String.Empty) ||
                                           ((contact.phone != null && contact.phone.ToLower().IndexOf(term) >= 0) || term == String.Empty) ||
                                           ((contact.email != null && contact.email.ToLower().IndexOf(term) >= 0) || term == String.Empty)
                                           ));

            contacts = query.ToList();
            int total = contacts.Count;

            responseContacts.data.total = total;

            // extract data with specific page
            int startIndex = (pageNumber - 1) * pageSize;

            if (total > startIndex)
            {
                // canculate the range we will respond client based on pageSize and pageNumber
                int count = total > (startIndex + pageSize) ? pageSize : total - ((pageNumber - 1) * pageSize);
                contacts = contacts.GetRange(startIndex, count);
                responseContacts.data.result = contacts;
            }

            return(responseContacts);
        }
Пример #2
0
        public ResponseContacts GetAllContacts()
        {
            ResponseContacts responseContacts = new ResponseContacts();

            if (carsRepository.GetAllMessages().FirstOrDefault() == null)
            {
                responseContacts.Success = false;
                responseContacts.Message = "ERROR: no contacts found";
            }
            else
            {
                responseContacts.Contacts = carsRepository.GetAllMessages();
                responseContacts.Success  = true;
                responseContacts.Message  = "Contacts found";
            }
            return(responseContacts);
        }