-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_implementations.ts
157 lines (125 loc) · 3.82 KB
/
db_implementations.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { connect, Client } from "ts-postgres";
import { COOKIE_TIMEOUT } from "./controller";
const SQL_CONNECTION = connect({
user: "Matixannder",
host: "localhost",
port: 5432,
database: "TODOAppDB",
});
type UIID = string;
type TaskInfo = {
title: string;
description: string;
}
export interface Tasks {
[key: UIID]: TaskInfo;
}
export async function createTable(): Promise<void> {
const psqlConnection = await SQL_CONNECTION;
const createUserTable =
`CREATE TABLE users (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
cookie_id VARCHAR(15),
cookie_expiration_date_utc TIMESTAMP
);`;
const createTasksTable =
`CREATE TABLE tasks (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id BIGINT REFERENCES users (id),
title VARCHAR(255),
description TEXT
);`;
try {
await psqlConnection.query(createUserTable);
} catch (e) {
console.log("User's table already exists");
}
try {
await psqlConnection.query(createTasksTable);
} catch (e) {
console.log("Tasks table already exists");
}
}
// Why I don't just, like put the cookie time span as autogenerated by the DB?
function cookieTimeStamp(): string {
let date = new Date().getTime();
date += COOKIE_TIMEOUT * 1000;
let newDate = new Date(date);
return newDate.toISOString().replace("T", " ").replace(/\..*$/, "");
}
// Query to delete users when their cookie expires:
// delete from users where cookie_expiration_date_utc < now() at time zone 'utc';
//
// Because now every user id is related to the tasks table, the query
// must delete the tasks first, or it'll throw an error
export async function createUser(cookie_id: string): Promise<void> {
let psqlConnection = await SQL_CONNECTION;
let raw_sessionId = cookie_id.replace(/sessionId=/g, "");
try {
await psqlConnection.query(`SELECT * FROM users WHERE cookie_id='${raw_sessionId}'`);
} catch (e) {
console.log("User already exists");
console.log(e);
}
let createUserQuery =
`INSERT INTO users (cookie_id, cookie_expiration_date_utc)
VALUES (${raw_sessionId}, '${cookieTimeStamp()}');`;
await psqlConnection.query(createUserQuery);
}
export async function getUserTasks(userCookie: string): Promise<Tasks | void> {
const raw_sessionId = userCookie.replace(/sessionId=/g, "");
const psqlConnection = await SQL_CONNECTION;
const getTasksQuery =
`SELECT task_uuid, title, description FROM tasks
WHERE user_id=
(SELECT id FROM users WHERE cookie_id='${raw_sessionId}')`;
const getTasks = await psqlConnection.query(getTasksQuery);
if (getTasks.status !== "SELECT 0") {
const user_tasks: Tasks = {};
const UUID = 0, TITLE = 1, DESCRIPTION = 2;
for (let row of getTasks.rows) {
user_tasks[row[UUID]] = {
title: row[TITLE],
description: row[DESCRIPTION]
}
}
return user_tasks;
}
else {
return;
}
}
export async function createTask(userCookie: string, task: TaskInfo): Promise<void> {
let psqlConnection = await SQL_CONNECTION;
const cookie = userCookie.replace(/sessionId=/g, "");
const lookForUser = `SELECT id FROM users WHERE cookie_id='${cookie}'`
const createNewTaskQuery =
`INSERT INTO tasks (user_id, title, description)
VALUES ((${lookForUser}), ${task.title}, ${task.description})`;
// Why the hell this is "any"?
let result: any;
try {
result = await psqlConnection.query(createNewTaskQuery);
} catch (e) {
console.log("An error ocurred");
console.log(cookie);
console.log(task.title);
console.log(task.description);
console.log(e);
}
console.log(result);
return;
}
export async function getTask(task_uuid: String): Promise<TaskInfo>
{
let psqlConnection = await SQL_CONNECTION;
const query = await psqlConnection.query(
`SELECT title, description FROM tasks
WHERE task_uuid='${task_uuid}'`);
const info = query.rows[0]
const task: TaskInfo = {
title: info[0],
description: info[1]
}
return task;
}