From fa1a7d882a363cb8ffe921fe416fd58ef97bd2b9 Mon Sep 17 00:00:00 2001 From: Simon Carpentier Date: Sat, 26 Jan 2013 15:14:41 -0500 Subject: [PATCH] Modernized Core project a bit --- RezNetUsage.Core/AppartHelper.cs | 24 +++++++--------- RezNetUsage.Core/{Net.cs => NetHelper.cs} | 34 +++++++++++++---------- RezNetUsage.Core/RezNetUsage.Core.csproj | 2 +- RezNetUsage.Core/UsageFactory.cs | 33 +++++++--------------- RezNetUsage.Web/Default.aspx.cs | 2 +- 5 files changed, 41 insertions(+), 54 deletions(-) rename RezNetUsage.Core/{Net.cs => NetHelper.cs} (53%) diff --git a/RezNetUsage.Core/AppartHelper.cs b/RezNetUsage.Core/AppartHelper.cs index 1209968..0cfdd28 100644 --- a/RezNetUsage.Core/AppartHelper.cs +++ b/RezNetUsage.Core/AppartHelper.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; +using System.Data; +using System.IO; using System.Linq; +using System.Reflection; using System.Text; +using System.Xml.Linq; namespace RezNetUsage.Core { - using System.Data; - using System.IO; - using System.Reflection; - public static class AppartHelper { public static bool IsAppartExist(int phase, int appart) @@ -21,8 +21,8 @@ public static bool IsAppartExist(int phase, int appart) DataRow[] foundrows = chambres.Tables[0].Select("Phase = " + phase + " AND Appartement = " + appart); // Retourner le résultat - return (foundrows.Count() == 1); - } catch (Exception ex) + return (foundrows.Any()); + } catch { return false; } @@ -47,6 +47,7 @@ public static Chambres GetChambres() var chambres = new Chambres(); // Loader le data dans le dataset + // TODO: Trouver un meilleur plan que d'utiliser la réflexion pour aller chercher un XML à chaque fois que quelqu'un demande une chambre! var _assembly = Assembly.GetExecutingAssembly(); chambres.ReadXml(new StreamReader(_assembly.GetManifestResourceStream("RezNetUsage.Core.Chambres.xml"))); @@ -55,7 +56,7 @@ public static Chambres GetChambres() public static Usage GetUsage(Chambres chambres, int mois) { - Usage usage = new Usage(); + var usage = new Usage(); foreach (DataRow row in chambres.Tables[0].Rows) { @@ -64,8 +65,8 @@ public static Usage GetUsage(Chambres chambres, int mois) (string)row["Phase"]); try { - usage = UsageFactory.GetUsage( - usage, int.Parse((string)row["Phase"]), int.Parse((string)row["Appartement"]), mois); + usage = new Usage().GetUsage( + int.Parse((string)row["Phase"]), int.Parse((string)row["Appartement"]), mois); } catch (Exception ex) { @@ -75,10 +76,5 @@ public static Usage GetUsage(Chambres chambres, int mois) return usage; } - - public static Usage GetUsage(int phase, int appart, int mois) - { - return UsageFactory.GetUsage(phase, appart, mois); - } } } diff --git a/RezNetUsage.Core/Net.cs b/RezNetUsage.Core/NetHelper.cs similarity index 53% rename from RezNetUsage.Core/Net.cs rename to RezNetUsage.Core/NetHelper.cs index 871a2e8..8ac8c59 100644 --- a/RezNetUsage.Core/Net.cs +++ b/RezNetUsage.Core/NetHelper.cs @@ -2,16 +2,16 @@ using System.Net; using System.Text; -namespace SPACEBAR +namespace RezNetUsage.Core { - public class Net + public class NetHelper { - public static string DownloadHTML(string url) + public static string DownloadHtml(string url) { try { - WebClient webClient = new WebClient(); - byte[] myDatabuffer = webClient.DownloadData(url); + var webClient = new WebClient(); + var myDatabuffer = webClient.DownloadData(url); return Encoding.Default.GetString(myDatabuffer); } catch (Exception ex) @@ -20,18 +20,22 @@ public static string DownloadHTML(string url) } } - public static string DownloadHTML(string url, string username, string password) + public static string DownloadHtml(string url, string username, string password) { try { - WebClient webClient = new WebClient(); + var webClient = new WebClient(); // Credentials management - CredentialCache cache = new CredentialCache(); - cache.Add(new Uri(url), "Basic", new NetworkCredential(username, password)); + var cache = new CredentialCache { + { + new Uri(url), + "Basic", + new NetworkCredential(username, password) + }}; webClient.Credentials = cache; - byte[] myDatabuffer = webClient.DownloadData(url); + var myDatabuffer = webClient.DownloadData(url); return Encoding.Default.GetString(myDatabuffer); } catch (Exception ex) @@ -42,20 +46,20 @@ public static string DownloadHTML(string url, string username, string password) public static void DownloadFile(string url, string fileName) { - WebClient webClient = new WebClient(); + var webClient = new WebClient(); webClient.DownloadFile(url, fileName); } public static bool DoesUrlExists(string url) { - bool urlExists = false; + var urlExists = false; try { - WebRequest req = WebRequest.Create(url); - HttpWebResponse response = (HttpWebResponse)req.GetResponse(); + var req = WebRequest.Create(url); + var response = (HttpWebResponse)req.GetResponse(); urlExists = true; } - catch (System.Net.WebException ex) + catch (WebException ex) { } diff --git a/RezNetUsage.Core/RezNetUsage.Core.csproj b/RezNetUsage.Core/RezNetUsage.Core.csproj index 2061f79..49b2d7b 100644 --- a/RezNetUsage.Core/RezNetUsage.Core.csproj +++ b/RezNetUsage.Core/RezNetUsage.Core.csproj @@ -72,7 +72,7 @@ True Chambres.xsd - + Usage.xsd diff --git a/RezNetUsage.Core/UsageFactory.cs b/RezNetUsage.Core/UsageFactory.cs index 62f54fc..038c0bd 100644 --- a/RezNetUsage.Core/UsageFactory.cs +++ b/RezNetUsage.Core/UsageFactory.cs @@ -13,13 +13,13 @@ public static class UsageFactory /// /// Regex pour définir l'utilisation /// - private readonly static Regex regexUsage = + private readonly static Regex RegexUsage = new Regex("(.*)(.*)(.*)(.*)"); /// /// Regex pour définir l'utilisation maximale permise /// - private static readonly Regex regexMax = + private static readonly Regex RegexMax = new Regex("Quota permis pour la période(.*)"); /// @@ -27,19 +27,6 @@ public static class UsageFactory /// private static readonly CultureInfo Culture = CultureInfo.CreateSpecificCulture("en-CA"); - /// - /// Retourne un objet Dataset Usage pour la phase, appart et mois spécifié - /// - /// - /// - /// - /// - public static Usage GetUsage(int phase, int appart, int mois) - { - var usage = new Usage(); - return GetUsage(usage, phase, appart, mois); - } - /// /// Ajoute les informations de la consommation internet pour un appart dans un objet Usage donné /// @@ -47,20 +34,20 @@ public static Usage GetUsage(int phase, int appart, int mois) /// /// /// - /// - public static Usage GetUsage(Usage usage, int phase, int appart, int mois) + /// Objet usage plein de data + public static Usage GetUsage(this Usage usage, int phase, int appart, int mois) { // Build query string // http://ets-res2-772:ets772@www2.cooptel.qc.ca/services/temps/?mois=9&cmd=Visualiser - string query = "http://www2.cooptel.qc.ca/services/temps/?mois=" + mois + "&cmd=Visualiser"; - string user = "ets-res" + phase + "-" + appart; - string pass = "ets" + appart; + var query = string.Format("http://www2.cooptel.qc.ca/services/temps/?mois={0}&cmd=Visualiser", mois); + var user = string.Format("ets-res{0}-{1}", phase, appart); + var pass = string.Format("ets{0}", appart); string html; try { // Fetch data into a string - html = SPACEBAR.Net.DownloadHTML(query, user, pass); + html = NetHelper.DownloadHtml(query, user, pass); } catch (Exception ex) { @@ -68,8 +55,8 @@ public static Usage GetUsage(Usage usage, int phase, int appart, int mois) } // Match du regeex - MatchCollection mc = regexUsage.Matches(html); - usage.Maximum = Math.Round(Double.Parse(regexMax.Matches(html)[0].Groups[1].Value, Culture), 0); + MatchCollection mc = RegexUsage.Matches(html); + usage.Maximum = Math.Round(Double.Parse(RegexMax.Matches(html)[0].Groups[1].Value, Culture), 0); // Objets temporaires for (var i = 0; i <= mc.Count - 1; i++) diff --git a/RezNetUsage.Web/Default.aspx.cs b/RezNetUsage.Web/Default.aspx.cs index 857eed8..d3aac9b 100644 --- a/RezNetUsage.Web/Default.aspx.cs +++ b/RezNetUsage.Web/Default.aspx.cs @@ -60,7 +60,7 @@ protected void Page_Load(object sender, EventArgs e) try { // Aller chercher l'utilisation - _usage = UsageFactory.GetUsage(_phase, _appart, _mois); + _usage = new Usage().GetUsage(_phase, _appart, _mois); // Mettre les paramètres dans les contrôles lblPhaseAppartMois.Text = String.Format(lblPhaseAppartMois.Text, _phase, _appart, Mois[_mois - 1]);