/* * class methods will do more than * simply set or get data member values * (which is the responsibility of the * class properties) * * These methods are sometimes referred * to as "action" methods */ public void RecordPopulation(Int32 inPopulation) { _Population = inPopulation; /* * Status is dependent on the population */ if (inPopulation > 1000000) { _Status = SpeciesStatus.Viable; } else { if (inPopulation > 500000) { _Status = SpeciesStatus.Concerned; } else { if (inPopulation > 250000) { _Status = SpeciesStatus.Threatened; } else { if (inPopulation > 100000) { _Status = SpeciesStatus.Endangered; } else { _Status = SpeciesStatus.Extinct; } } } } }
public FactsheetRepository() { var requestUrl = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Url.Segments.Last()); /*Setup for array of Segments' id number is for Live server, Change this for local machine if require. */ int idspecies = 0; int n; bool id = int.TryParse(requestUrl, out n); if (requestUrl.Length > 0 && id == true) { idspecies = int.Parse(requestUrl); try { myConn = new MySqlConnection(connStr); myConn.Open(); MySqlCommand myCmd = new MySqlCommand(cmdStr, myConn); myCmd.Prepare(); myCmd.Parameters.Add("@species_id", MySqlDbType.Int32).Value = idspecies; myReader = myCmd.ExecuteReader(); while (myReader.Read()) { PopulationStatus[] ps = new PopulationStatus[] { new PopulationStatus { speciesid = myReader.IsDBNull(0) ? 0 : myReader.GetInt32(0), gb = myReader.IsDBNull(15) ? "" : myReader.GetString(15), england = myReader.IsDBNull(16) ? "" : myReader.GetString(16), scotland = myReader.IsDBNull(17) ? "" : myReader.GetString(17), wales = myReader.IsDBNull(18) ? "" : myReader.GetString(18) } }; SpeciesStatus[] ss = new SpeciesStatus[] { new SpeciesStatus { speciesid = myReader.IsDBNull(0) ? 0 : myReader.GetInt32(0), gb = myReader.IsDBNull(11) ? "" : myReader.GetString(11), england = myReader.IsDBNull(12) ? "" : myReader.GetString(12), scotland = myReader.IsDBNull(13) ? "" : myReader.GetString(13), wales = myReader.IsDBNull(14) ? "" : myReader.GetString(14) } }; Factsheet[] fs = new Factsheet[] { new Factsheet { fact_id = myReader.IsDBNull(21) ? 0 : myReader.GetInt32(21), short_desc = myReader.IsDBNull(22) ? "" : WriteXml(myReader.GetString(22)), biology_ecology = myReader.IsDBNull(23) ? "" : WriteXml(myReader.GetString(23)), habitat = myReader.IsDBNull(24) ? "" : WriteXml(myReader.GetString(24)), habitat_summary = myReader.IsDBNull(25) ? "" : WriteXml(myReader.GetString(25)), distribution = myReader.IsDBNull(26) ? "" : WriteXml(myReader.GetString(26)), impact = myReader.IsDBNull(27) ? "" : WriteXml(myReader.GetString(27)), impact_summary = myReader.IsDBNull(28) ? "" : WriteXml(myReader.GetString(28)), management = myReader.IsDBNull(29) ? "" : WriteXml(myReader.GetString(29)), status_summary = myReader.IsDBNull(30) ? "" : WriteXml(myReader.GetString(30)), invasion_history = myReader.IsDBNull(31) ? "" : WriteXml(myReader.GetString(31)), references = myReader.IsDBNull(33) ? "" : WriteXml(myReader.GetString(33)), other_references = myReader.IsDBNull(34) ? "" : WriteXml(myReader.GetString(34)), authors = myReader.IsDBNull(35) ? "" : WriteXml(myReader.GetString(35)), last_updated = myReader.IsDBNull(36) ? "" : WriteXml(myReader.GetString(36)) } }; //legislation = myReader.IsDBNull(32) ? "" : WriteXml(myReader.GetString(32)), List <string> syn = getSynonyms(idspecies); List <string> cmn = getCommonNames(idspecies); List <string> nrange = getNativeRanges(idspecies); Species[] sp = new Species[] { new Species { speciesid = myReader.IsDBNull(0) ? 0 : myReader.GetInt32(0), brcnumber = myReader.IsDBNull(1) ? "" : myReader.GetString(1), nbntvk = myReader.IsDBNull(2) ? "" : myReader.GetString(2), latinname = myReader.IsDBNull(3) ? "" : myReader.GetString(3), authority = myReader.IsDBNull(4) ? "" : myReader.GetString(4), phylum = myReader.IsDBNull(6) ? "" : myReader.GetString(6), family = myReader.IsDBNull(7) ? "" : myReader.GetString(7), order = myReader.IsDBNull(8) ? "" : myReader.GetString(8), environment = myReader.IsDBNull(9) ? "" : myReader.GetString(9), functional = myReader.IsDBNull(10) ? "" : myReader.GetString(10), locationfound = myReader.IsDBNull(19) ? "" : myReader.GetString(19), firstdate = myReader.IsDBNull(20) ? "" : myReader.GetString(20), factsheet = fs.Count <Factsheet>() == 0 ? null : fs[0], speciesStatus = ss.Count <SpeciesStatus>() == 0 ? null : ss[0], populationStatus = ps.Count <PopulationStatus>() == 0 ? null : ps[0], CommonNames = cmn, Synonyms = syn, NativeRanges = nrange, displaynativemap = myReader.IsDBNull(37) ? "no" : myReader.GetString(37), displaydistributionmap = myReader.IsDBNull(38) ? "no" : myReader.GetString(38), } }; species.Add(sp[0]); } } catch (MySqlException mysqlErr) { Console.WriteLine("MySQL Error :" + mysqlErr.ToString()); } finally { if (myReader != null) { myReader.Close(); } if (myConn != null) { myConn.Close(); } } } if (species == null) { // option 1 (throw) var notFoundMessage = new HttpResponseMessage(HttpStatusCode.NotFound); throw new HttpResponseException(notFoundMessage); } }