public ResSummary GetRestaurant(int id) { ResSummary res = null; using (var con = new NpgsqlConnection(connectionString)) { con.Open(); using (var cmd = new NpgsqlCommand($"SELECT * FROM restaurants WHERE restaurant_id = {id} ", con)) using (var reader = cmd.ExecuteReader()) while (reader.Read()) { res = new ResSummary { restaurant_id = reader.GetInt32(0), name = reader.GetString(1), description = reader.GetString(2), rating = reader.GetIntOrDefault(3), photo_url = reader.GetStringOrDefault(4), address = reader.GetStringOrDefault(5), link_to_360 = reader.GetStringOrDefault(6), latitude = reader.GetFloatOrDefault(7), longitude = reader.GetFloatOrDefault(8), created_at = reader.GetDateTimeOrDefault(9) } } ; return(res); } }
public ResSummary PatchRestaurant(int id, ResPatch resPatch) { ResSummary resSummary = null; using (var con = new NpgsqlConnection(connectionString)) { con.Open(); using (var cmd = new NpgsqlCommand($"UPDATE restaurants SET rating = {resPatch.rating} WHERE restaurant_id = {id} RETURNING restaurant_id", con)) id = int.Parse(cmd.ExecuteScalar().ToString()); using (var newcmd = new NpgsqlCommand($"SELECT * FROM restaurants WHERE restaurant_id = {id} ", con)) using (var reader = newcmd.ExecuteReader()) while (reader.Read()) { resSummary = new ResSummary { restaurant_id = reader.GetInt32(0), name = reader.GetString(1), description = reader.GetString(2), rating = reader.GetIntOrDefault(3), photo_url = reader.GetStringOrDefault(4), address = reader.GetStringOrDefault(5), link_to_360 = reader.GetStringOrDefault(6), latitude = reader.GetFloatOrDefault(7), longitude = reader.GetFloatOrDefault(8), created_at = reader.GetDateTimeOrDefault(9) } } ; } return(resSummary); } }
public void FillFields(ResSummary sum) { nameText.text = sum.name; populationText.text = "Population Units: " + sum.population; incomeText.text = "Income Level: " + sum.income; preferenceText.text = "Preferences: " + sum.preference; likesText.text = "Favorites: " + sum.favorites; hatesText.text = "Dislikes: " + sum.hates; debugIncome.text = "Debug: " + sum.debugIncome; debugPref.text = "Debug: " + sum.debugPref; }
public ResSummary PostRestaurant(ResPost resPost) { ResSummary resSummary = null; using (var con = new NpgsqlConnection(connectionString)) { con.Open(); int id = -1; using (var cmd = new NpgsqlCommand($"INSERT INTO restaurants (name,description,rating,photo_url,address,link_to_360,latitude,longitude) VALUES (@name,@description,@rating,@photo_url,@address,@link_to_360,@latitude,@longitude) RETURNING restaurant_id", con)) { cmd.Parameters.AddWithValue("name", resPost.Name); cmd.Parameters.AddWithValue("description", resPost.Description); cmd.Parameters.AddWithValue("rating", resPost.Rating); cmd.Parameters.AddWithValue("photo_url", resPost.Photo_url); cmd.Parameters.AddWithValue("address", resPost.Address); cmd.Parameters.AddWithValue("link_to_360", resPost.Link_to_360); cmd.Parameters.AddWithValue("latitude", resPost.Latitude); cmd.Parameters.AddWithValue("longitude", resPost.Longitude); id = int.Parse(cmd.ExecuteScalar().ToString()); } using (var cmd = new NpgsqlCommand($"SELECT * FROM restaurants WHERE restaurant_id = {id} ", con)) using (var reader = cmd.ExecuteReader()) while (reader.Read()) { resSummary = new ResSummary { restaurant_id = reader.GetInt32(0), name = reader.GetString(1), description = reader.GetString(2), rating = reader.GetIntOrDefault(3), photo_url = reader.GetStringOrDefault(4), address = reader.GetStringOrDefault(5), link_to_360 = reader.GetStringOrDefault(6), latitude = reader.GetFloatOrDefault(7), longitude = reader.GetFloatOrDefault(8), created_at = reader.GetDateTimeOrDefault(9) } } ; } return(resSummary); }
public ResSummary GenerateSummary() { ResSummary summary = new ResSummary(); summary.population = residents.Count; summary.name = name; int lowIn = 0; int midIn = 0; int highIn = 0; int amerPref = 0; int italPref = 0; int mexPref = 0; List <RecipeIngredient> likes = new List <RecipeIngredient>(); List <RecipeIngredient> hates = new List <RecipeIngredient>(); foreach (Pop res in residents) { switch (res.income) { case IncomeLevel.LOW: lowIn++; break; case IncomeLevel.MIDDLE: midIn++; break; case IncomeLevel.HIGH: highIn++; break; } switch (res.preference) { case FoodPreference.AMERICAN: amerPref++; break; case FoodPreference.ITALIAN: italPref++; break; case FoodPreference.MEXICAN: mexPref++; break; } if (res.favoriteIngredient != RecipeIngredient.EMPTY && !likes.Contains(res.favoriteIngredient)) { likes.Add(res.favoriteIngredient); } if (res.hatedIngredient != RecipeIngredient.EMPTY && !hates.Contains(res.hatedIngredient)) { hates.Add(res.hatedIngredient); } } float perLowIn = (float)lowIn / residents.Count; float perMidIn = (float)midIn / residents.Count; float perHighIn = (float)highIn / residents.Count; float perAmerPref = (float)amerPref / residents.Count; float perItalPref = (float)italPref / residents.Count; float perMexPref = (float)mexPref / residents.Count; if (lowIn == residents.Count) { summary.income = "Low"; } else if (midIn == residents.Count) { summary.income = "Middle"; } else if (highIn == residents.Count) { summary.income = "High"; } else if (perLowIn >= 0.4f && perMidIn >= 0.4f) { summary.income = "Mixed Low/Middle"; } else if (perLowIn >= 0.4f && perHighIn >= 0.4f) { summary.income = "Mixed Low/High"; } else if (perMidIn >= 0.4f && perHighIn >= 0.4f) { summary.income = "Mixed Middle/High"; } else if (perLowIn >= 2.0f / 3) { summary.income = "Mostly Low"; } else if (perMidIn >= 2.0f / 3) { summary.income = "Mostly Middle"; } else if (perHighIn >= 2.0f / 3) { summary.income = "Mostly High"; } else { summary.income = "Mixed"; } List <string> preferences = new List <string>(); if (perAmerPref >= 1.0f / 3) { preferences.Add("American"); } if (perItalPref >= 1.0f / 3) { preferences.Add("Italian"); } if (perMexPref >= 1.0f / 3) { preferences.Add("Mexican"); } if (preferences.Count == 0) { summary.preference = "None"; } else { string pref = ""; foreach (string s in preferences) { pref = pref + s + ", "; } pref = pref.Substring(0, pref.Length - 2); summary.preference = pref; } if (likes.Count == 0) { summary.favorites = "None"; } else { string like = ""; foreach (RecipeIngredient ing in likes) { if (ing != RecipeIngredient.EMPTY) { like = like + Ingredient.IngredientToString(ing) + ", "; } } if (like[like.Length - 1] == ' ') { like = like.Substring(0, like.Length - 2); } summary.favorites = like; } if (hates.Count == 0) { summary.hates = "None"; } else { string hate = ""; foreach (RecipeIngredient ing in hates) { if (ing != RecipeIngredient.EMPTY) { hate = hate + Ingredient.IngredientToString(ing) + ", "; } } if (hate[hate.Length - 1] == ' ') { hate = hate.Substring(0, hate.Length - 2); } summary.hates = hate; } summary.debugIncome = "Low: " + lowIn + " / Mid: " + midIn + " / High: " + highIn; summary.debugPref = "Amer: " + amerPref + " / Ital: " + italPref + " / Mex: " + mexPref; return(summary); }