db.php 1.4 KB
Newer Older
stc's avatar
stc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

class DB {

	private $conn;

	function __construct () {
		include 'connexion.php';
		try {
			$this->conn = new PDO('pgsql:host=localhost;port=5432;dbname=apisub', $db_user, $db_pass);
		} catch (PDOException $e) {
			die('Connection failed: ' . $e->getMessage());
		}
	}

16
	function subList(string $utclogin) : array {
17
		$sql = 'SELECT *
Stephane Crozat's avatar
Stephane Crozat committed
18
						FROM vsubscription
19
						WHERE	utclogin=:utclogin';
stc's avatar
stc committed
20
		$st = $this->conn->prepare($sql);
21
22
23
		$st->execute([
			'utclogin' => $utclogin,
		]);
stc's avatar
stc committed
24
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
25
26

		return $res;
stc's avatar
stc committed
27
28
	}

29
	function apiList(string $semester, int $year) : array {
30
		$sql = 'SELECT *
31
						FROM vApi
32
						WHERE semester=:semester AND year=:year';
stc's avatar
stc committed
33
		$st = $this->conn->prepare($sql);
34
35
36
37
		$st->execute([
			'semester' => $semester,
			'year' => $year,
		]);
stc's avatar
stc committed
38
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
39
40
		
		return $res;
stc's avatar
stc committed
41
42
	}

43
44
45
46
47
48
49
50
51
	function subToApi($utclogin, $api) : bool {
		$today = date('Ymd');
		$sql = 'INSERT INTO subscribe(utclogin, api, subdate) VALUES (:utclogin, :api, :today)';
		$st = $this->conn->prepare($sql);
		$res = $st->execute([
			'utclogin' => $utclogin,
			'api' => $api,
			'today' => $today,
		]);
stc's avatar
stc committed
52

53
		return $res;
stc's avatar
stc committed
54
55
	}

56
57
58
59
60
61
62
63
64
65
	function unsubToApi($utclogin, $api) : bool {
		$sql = 'DELETE FROM subscribe WHERE utclogin=:utclogin AND api=:api';
		$st = $this->conn->prepare($sql);
		$res = $st->execute([
			'utclogin' => $utclogin,
			'api' => $api,
		]);
		
		return $res;
	}
stc's avatar
stc committed
66
}