-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
65 lines (54 loc) · 2.28 KB
/
Program.cs
File metadata and controls
65 lines (54 loc) · 2.28 KB
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
namespace FinanceApp;
using Npgsql;
class Program
{
static void Main(string[] args)
{
string connectionString = "Host=localhost;Username=postgres;Password=password;Database=finance_app";
using var npgsqlConnection = new NpgsqlConnection(connectionString);
npgsqlConnection.Open();
var createTablesQuery = @"
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
password TEXT NOT NULL,
balance DECIMAL CHECK(balance >= 0)
);
CREATE TABLE IF NOT EXISTS transactions (
id UUID PRIMARY KEY,
user_id UUID REFERENCES users(id),
amount DECIMAL NOT NULL,
type TEXT NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
using var executeSqlCmd = new NpgsqlCommand(createTablesQuery, npgsqlConnection);
executeSqlCmd.ExecuteNonQuery();
IUserService userService = new PostgresUserService(npgsqlConnection);
IUtilitiesService utilitiesService = new UtilitiesService(npgsqlConnection, userService);
ITransactionService transactionService = new PostgresTransactionService(npgsqlConnection, utilitiesService);
IMenuService menuService = new MenuService();
var loginMenu = new LoginMenu(userService, menuService, transactionService, utilitiesService);
menuService.SetMenu(loginMenu);
var userMenu = new UserMenu(userService, menuService, transactionService, utilitiesService);
while(true) {
string? inputCommand = Console.ReadLine();
if (inputCommand != null) {
try {
menuService.GetMenu().ExecuteCommand(inputCommand);
} catch {
Console.WriteLine("Please enter a valid input");
utilitiesService.PressKeyToContinue();
var user = userService.GetLoggedInUser();
if(user == null) {
menuService.SetMenu(loginMenu);
}
else {
menuService.SetMenu(userMenu);
}
}
} else {
break;
}
}
}
}