-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseRepository.cs
110 lines (100 loc) · 3.86 KB
/
CourseRepository.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
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Npgsql;
namespace sqltest
{
public class CourseRepository
{
private readonly string connectionString;
public CourseRepository(IConfiguration configuration)
{
connectionString = "Host=localhost;Username=postgres;Password=password;Database=postgres";
//?? throw new InvalidOperationException("Connection string not found in configuration.");
}
public async Task AddCourseAsync(Course course)
{
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var cmd = dataSource.CreateCommand(@"
INSERT INTO courses (name, duration, description, credits)
VALUES (@Name, @Duration, @Description, @Credits)");
cmd.Parameters.AddWithValue("Name", course.Name);
cmd.Parameters.AddWithValue("Duration", course.Duration);
cmd.Parameters.AddWithValue("Description", course.Description);
cmd.Parameters.AddWithValue("Credits", course.Credits);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine("Course added successfully.");
}
catch (NpgsqlException ex)
{
Console.WriteLine($"Database error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
public async Task<List<Course>> GetAllCoursesAsync()
{
var courses = new List<Course>();
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var cmd = dataSource.CreateCommand("SELECT * FROM courses");
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
courses.Add(new Course
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Duration = reader.GetTimeSpan(2),
Description = reader.GetString(3),
Credits = reader.GetInt32(4)
});
}
}
catch (NpgsqlException ex)
{
Console.WriteLine($"Database error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return courses;
}
public async Task<Course?> GetCourseByIdAsync(int id)
{
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var cmd = dataSource.CreateCommand("SELECT * FROM courses WHERE id = @Id");
cmd.Parameters.AddWithValue("Id", id);
await using var reader = await cmd.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
return new Course
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Duration = reader.GetTimeSpan(2),
Description = reader.GetString(3),
Credits = reader.GetInt32(4)
};
}
}
catch (NpgsqlException ex)
{
Console.WriteLine($"Database error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return null;
}
}
}