-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresData.cs
117 lines (95 loc) · 3.81 KB
/
PostgresData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
namespace myrecipebook
{
public class PostgresData : IDatabase
{
private string _connection;
private Npgsql.NpgsqlConnection _conn;
public PostgresData()
{
_connection = "Host=localhost;Username=;Password=;Database=recipe_book";
_conn = new Npgsql.NpgsqlConnection(_connection);
}
public PostgresData(string conn)
{
_connection = conn;
_conn = new Npgsql.NpgsqlConnection(_connection);
}
public void CreateRecipe(Recipe recipe) {
_conn.Open();
// Insert some data
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = _conn;
cmd.CommandText = "INSERT INTO recipes (name,ingredients,instructions) VALUES (@name, @ingr, @instr)";
cmd.Parameters.AddWithValue("name", recipe.GetName());
cmd.Parameters.AddWithValue("ingr", recipe.GetIngredients());
cmd.Parameters.AddWithValue("instr", recipe.GetInstructions());
cmd.ExecuteNonQuery();
}
_conn.Close();
}
public Recipe GetRecipe(int id)
{
_conn.Open();
var data = new object[] { };
// Retrieve all rows
using (var cmd = new Npgsql.NpgsqlCommand("SELECT id, user_id, name, ingredients, instructions FROM recipes WHERE id = @id LIMIT 1", _conn))
{
cmd.Parameters.AddWithValue("id", id);
var reader = cmd.ExecuteReader();
while (reader.Read())
reader.GetValues(data);
Console.WriteLine(reader.GetString(0));
}
_conn.Close();
return new Recipe(int.Parse(data[0].ToString()), data[1].ToString(), data[2].ToString(), data[3].ToString());
}
public void UpdateRecipe(Recipe recipe)
{
_conn.Open();
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = _conn;
cmd.CommandText = "UPDATE recipes SET (user_id,name,ingredients,instructions) = (@uid, @name, @ingr, @instr) WHERE id = @id";
cmd.Parameters.AddWithValue("id", recipe.GetID());
cmd.Parameters.AddWithValue("name", recipe.GetName());
cmd.Parameters.AddWithValue("ingr", recipe.GetIngredients());
cmd.Parameters.AddWithValue("instr", recipe.GetInstructions());
cmd.ExecuteNonQuery();
}
_conn.Close();
}
public void DeleteRecipe(Recipe recipe)
{
_conn.Open();
// Insert some data
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = _conn;
cmd.CommandText = "DELETE from recipes where id = @id";
cmd.Parameters.AddWithValue("id", recipe.GetID());
cmd.ExecuteNonQuery();
}
_conn.Close();
}
public List<Recipe> GetRecipes()
{
_conn.Open();
var recipes = new List<Recipe>{};
var data = new object[] { };
// Retrieve all rows
using (var cmd = new Npgsql.NpgsqlCommand("SELECT id, user_id, name, ingredients, instructions FROM recipes", _conn))
{
var reader = cmd.ExecuteReader();
while (reader.Read())
reader.GetValues(data);
recipes.Add(new Recipe(int.Parse(data[0].ToString()), data[1].ToString(), data[2].ToString(), data[3].ToString()));
data = new object[] { };
}
_conn.Close();
return recipes;
}
}
}