-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbcode.dart
107 lines (80 loc) · 2.29 KB
/
dbcode.dart
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
import 'package:sqlite3/sqlite3.dart';
import 'dart:io';
void initDatabase() {
final db = sqlite3.open('movies.db');
db.execute ('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL);
''');
db.execute ('''
CREATE TABLE IF NOT EXISTS movies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
film_title TEXT NOT NULL,
rank INTEGER NOT NULL);
''');
db.dispose();
}
void addMovie(String userID, String channelId, String title, int rank){
final db = sqlite3.open("movies.db");
final name = title.toLowerCase();
db.execute('''
INSERT INTO movies (user_id, channel_id, film_title, rank)
VALUES (?, ?, ?, ?)
''', [userID, channelId, name, rank]);
db.dispose();
}
List<Map <String, dynamic>> getFilms(String channelId) {
final db = sqlite3.open('movies.db');
final result = db.select('''
SELECT film_title, rank
FROM movies
WHERE channel_id = ?
ORDER BY rank DESC
''', [channelId]);
final films = result.map((row) => {
'film_title': row['film_title'],
'rank': row['rank']
}).toList();
db.dispose();
return films;
}
int upvote(String channelId, String title) {
// Open db
final db = sqlite3.open('movies.db');
// Check if movie exists in queue already
final ResultSet rs = db.select(
'SELECT COUNT(*) as count FROM movies WHERE film_title = ?', [title]
);
final int count = rs.first['count'];
// If movie not in queue, return false
if(count == 0) {
db.dispose();
return 0;
}
// Otherwise, proceed with upvote
db.execute('''
UPDATE movies
SET rank = rank + 1
WHERE channel_id = ? AND film_title = ?
''', [channelId, title]);
// Close db, return true
db.dispose();
return 1;
}
void removeMovie(String channelId, String title){
final db = sqlite3.open('movies.db');
db.execute('''
DELETE FROM movies
WHERE channel_id = ? AND film_title = ?
''', [channelId, title]);
db.dispose();
}
void report(String reportText, String userID){
final file = File('reports.txt');
final ts = DateTime.now().toIso8601String();
final entry = '[$ts] User: $userID\n$reportText\n\n';
file.writeAsStringSync(entry, mode: FileMode.append);
}