0 Replies - 11791 Views - Last Post: 18 September 2011 - 09:34 AM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Suggestions on how to search for users via username?

Posted 18 September 2011 - 09:34 AM

I'm trying to make my DreamInCode.Net lib let devs search for a user via their name. Below is the code:

using System;
using System.Net;
using DreamInCode.Net.Abstract;
using DreamInCode.Net.Models;
using DreamInCode.Net.Parsers;

namespace DreamInCode.Net.Concrete
{
    public class UserRepository : IUserRepository
    {
        public UserProfile FindUserById(int userId)
        {
            if (userId <= 0)
                throw new ArgumentOutOfRangeException("userId", userId, "The user ID must be greater than 0.");

            var xmlResponse = FetchXmlResponse(userId);
            return UserParser.ParseUser(xmlResponse);
        }

        public UserProfile FindUserByName(string name)
        {
            if (String.IsNullOrWhiteSpace(name))
                throw new ArgumentException("The user name cannot be null or empty.", "name");

            int userId = FindIdForUser(name);

            var xmlResponse = FetchXmlResponse(userId);
            return UserParser.ParseUser(xmlResponse);
        }

        /// <summary>
        /// Fetches the XML response from the DreamInCode API endpoint.
        /// </summary>
        /// <param name="userId">The user's ID.</param>
        /// <returns>The XML response string.</returns>
        private static string FetchXmlResponse(int userId)
        {
            string xmlEndPoint = string.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userId.ToString());
            string xmlResponse;

            using (WebClient client = new WebClient())
            {
                xmlResponse = client.DownloadString(xmlEndPoint);
            }

            if (String.IsNullOrEmpty(xmlResponse))
                throw new Exception("Error: The XML endpoint did not respond.");

            return xmlResponse;
        }

        private static int FindIdForUser(string name)
        {
            //To do: Mechanism for finding the user's ID number goes here. 
            return 335389;
        }
    }
}



Any suggestions or ideas on how to tackle this?

This post has been edited by Sergio Tapia: 18 September 2011 - 09:34 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1