Beispiel #1
0
        // Will return a random item with its max price
        public Object GetItemMaxURIEndpoint()
        {
            maxPrices mP = new maxPrices();

            try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();
                // Get only the item names, no repetition
                string          sql = "SELECT DISTINCT(ITEM_NAME) FROM items";
                MySqlCommand    cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                string name;
                // Store all the names into the arraylist
                ArrayList names = new ArrayList();

                while (rdr.Read())
                {
                    names.Add(rdr[0]);
                }

                rdr.Close();
                // Randomly get a name out
                Random rand = new Random();
                name = names[rand.Next(names.Capacity)].ToString();

                // Get the max cost based off of the name
                // SQL cquery to insert the values from the text file to the DB
                sql = "SELECT ID, ITEM_NAME, MAX(COST) FROM items WHERE ITEM_NAME = @item";
                cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@item", name);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    mP.itemID   = (int)rdr[0];
                    mP.itemName = rdr[1].ToString();
                    mP.itemCost = (int)rdr[2];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            // Return an object instance of the maxPrice
            return(JsonConvert.SerializeObject(mP));
        }
Beispiel #2
0
        // ---------------------- URI endpoints -------------------
        public Object GetItemsPricesURIEndpoint()
        {
            // ArrayList that will hold all the maxPrices objects
            ArrayList mPL = new ArrayList();

            try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();

                // Get the max cost for each item
                string          sql = "SELECT ID,ITEM_NAME, MAX(COST) FROM items GROUP BY ITEM_NAME";
                MySqlCommand    cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    // Create an object of the ID, ITEM_NAME, and COST returned from the
                    // query and store it in an array
                    maxPrices t = new maxPrices();
                    t.itemID   = (int)rdr[0];
                    t.itemName = rdr[1].ToString();
                    t.itemCost = (int)rdr[2];

                    mPL.Add(t);
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            conn.Close();
            Console.WriteLine("Done.");
            // Return an object instance of the ArrayList of maxPrices
            return(JsonConvert.SerializeObject(mPL));
        }