public IActionResult Get() { try { if (String.IsNullOrEmpty(Authorize(Request))) { throw new Exception(); } MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); MySqlCommand comm = connection.CreateCommand(); comm.CommandText = "SELECT * FROM STORE_T "; // List to return to the user List<Store> stores = new List<Store>(); // This while loop reads all of the results MySqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { Store store = new Store(); store.StoreId = reader.GetInt32(0); store.StoreName = reader.GetString(1); store.ZipCode = reader.GetInt32(2); stores.Add(store); } connection.Close(); return new ObjectResult(stores); } catch (Exception) { return HttpBadRequest(); } }
public IActionResult Get(int id) { try { if (String.IsNullOrEmpty(Authorize(Request))) { throw new Exception(); } MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); MySqlCommand comm = connection.CreateCommand(); comm.CommandText = "SELECT * FROM STORE_T WHERE PK_STORE_ID = @id"; comm.Parameters.AddWithValue("@id", id); // List to return to the user Store store = new Store(); // This while loop reads all of the results MySqlDataReader reader = comm.ExecuteReader(); if (reader.Read()) { store.StoreId = reader.GetInt32(0); store.StoreName = reader.GetString(1); store.ZipCode = reader.GetInt32(2); } connection.Close(); return new ObjectResult(store); } catch (Exception) { return HttpBadRequest(); } }