private SqlCommand ConsumptionCommand(string query, ConsumptionInput consumption, SqlConnection conn) { SqlCommand command = new SqlCommand(query, conn); command.Parameters.AddWithValue("@food_name", consumption.FoodName); command.Parameters.AddWithValue("@kcal", consumption.KCal); command.Parameters.AddWithValue("@protein", consumption.Protein); command.Parameters.AddWithValue("@fiber", consumption.Fiber); command.Parameters.AddWithValue("@calium", consumption.Calium); command.Parameters.AddWithValue("@sodium", consumption.Sodium); command.Parameters.AddWithValue("@amount", consumption.Amount); command.Parameters.AddWithValue("@weight_unit_id", consumption.WeightUnitId); command.Parameters.AddWithValue("@date", consumption.Date); command.Parameters.AddWithValue("@patient_id", consumption.PatientId); return(command); }
public async Task <bool> AddConsumption(ConsumptionInput consumption) { bool added; var insert = $"INSERT INTO Consumption " + $"(food_name, kcal, protein, fiber, calium, sodium, amount, weight_unit_id, date, patient_id)"; var values = $" VALUES (@food_name, @kcal, @protein, @fiber, @calium, @sodium, @amount, @weight_unit_id, @date, @patient_id)"; var insertQuery = insert + values; using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection"))) { conn.Open(); try { added = await ConsumptionCommand(insertQuery, consumption, conn).ExecuteNonQueryAsync() > 0; } catch (Exception) { added = false; } conn.Close(); } return(added); }
public async Task <bool> UpdateConsumption(int consumptionId, ConsumptionInput consumption) { bool updated; var updateQuery = $"UPDATE Consumption SET " + $"food_name = @food_name, kcal = @kcal, protein = @protein, fiber = @fiber, " + $"calium = @calium, sodium = @sodium, amount = @amount, weight_unit_id = @weight_unit_id, " + $"date = @date, patient_id = @patient_id " + $"Where Id = {consumptionId}"; using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection"))) { conn.Open(); try { updated = await ConsumptionCommand(updateQuery, consumption, conn).ExecuteNonQueryAsync() > 0; } catch (Exception) { updated = false; } conn.Close(); } return(updated); }