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

New Topic/Question
Reply




MultiQuote


|