-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborga-db-int.js
148 lines (116 loc) · 4.65 KB
/
borga-db-int.js
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
'use strict'
const crypto = require('crypto')
const errors = require('./errors')
const users = []
module.exports = function () {
function getUserIndex(userID) {
const userIndex = users.findIndex(user => user.id === userID)
if(!users[userIndex]) return -1
return userIndex
}
function getGroupIndex(userIndex, groupID) {
const groups = users[userIndex].groups
const groupIndex = groups.findIndex(group => group.id === groupID)
if(!groups[groupIndex]) return -1
return groupIndex
}
function createGroup(userID, name, description) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const uuid = crypto.randomUUID()
const group = {
id: uuid,
name: name,
description: description,
games: []
}
users[userIndex].groups.push(group)
return Promise.resolve(group)
}
function getGroups(userID) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
return Promise.resolve(users[userIndex].groups)
}
function getGroup(userID, groupID) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const group = users[userIndex].groups.find(group => group.id === groupID)
return group ? Promise.resolve(group) : Promise.reject(errors.MISSING_DATA)
}
function updateGroup(userID, groupID, newName, newDescription) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const groupIndex = getGroupIndex(userIndex, groupID)
if(groupIndex === -1) return Promise.reject(errors.MISSING_DATA)
users[userIndex].groups[groupIndex].name = newName
users[userIndex].groups[groupIndex].description = newDescription
return Promise.resolve()
}
function deleteGroup(userID, groupID) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const groupIndex = getGroupIndex(userIndex, groupID)
if(groupIndex === -1) return Promise.reject(errors.MISSING_DATA)
users[userIndex].groups.splice(groupIndex,1)
return Promise.resolve()
}
function addGameToGroup(userID, groupID, game) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const groupIndex = getGroupIndex(userIndex, groupID)
if(groupIndex === -1) return Promise.reject(errors.MISSING_DATA)
users[userIndex].groups[groupIndex].games.push(game)
return Promise.resolve(game)
}
function deleteGameFromGroup(userID, groupID, game) {
const userIndex = getUserIndex(userID)
if (userIndex === -1) return Promise.reject(errors.INVALID_USER)
const groupIndex = getGroupIndex(userIndex, groupID)
if(groupIndex === -1) return Promise.reject(errors.MISSING_DATA)
const foundGroup = users[userIndex].groups[groupIndex]
const gameIndex = foundGroup.games.findIndex(g => g.id === game.id)
if(gameIndex === -1) return Promise.reject(errors.MISSING_DATA)
users[userIndex].groups[groupIndex].games.splice(gameIndex, 1)
return Promise.resolve()
}
function createUser(username, password) {
return getUser(username).then(user => {
if(user) return Promise.reject(errors.INVALID_USER)
const uuid = crypto.randomUUID()
const newUser = {
username: username,
password: password,
id: uuid,
groups: []
}
users.push(newUser)
return Promise.resolve(newUser)
}).catch(err => {
return Promise.reject(err)
})
}
function getUser(username) {
const userIndex = users.findIndex(user => user.username === username)
return Promise.resolve(users[userIndex])
}
function checkDatabase() {
return Promise.resolve(true)
}
function createUsersIndex() {
return Promise.resolve(true)
}
return {
getUser: getUser,
createGroup: createGroup,
getGroups: getGroups,
getGroup: getGroup,
updateGroup: updateGroup,
deleteGroup: deleteGroup,
addGameToGroup: addGameToGroup,
deleteGameFromGroup: deleteGameFromGroup,
createUser: createUser,
checkDatabase: checkDatabase,
createUsersIndex: createUsersIndex
}
}