-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
100 lines (95 loc) · 3.17 KB
/
models.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
var pool = require('./db.js');
getConnection = pool.getConnection()
var skillsObj = {
insert: async function(skills,sql){
// difference of new skills
console.log(sql)
existing_skills = await sql.query('select from skills where in ?',skills)
new_skills = skills.filter((x)=>!existing_skills.included(x))
// insert new skills
sql.query('insert into skills(skill) values ?',
new_skills
);
}
}
exports.subscriptions = {
create : async function(email,skills){
let sql = null;
try {
sql = await getConnection();
skillsObj.insert(skills,sql);
console.log(sql);
// insert subscription
let result = sql.query(
'insert into subscriptions(email) values(?)',
email
)
id_subscription = result.insertId
modified_skills = skills.map((x)=>[id_subscription,x])
result = result && await sql.query(
'insert into subscriptions_skills(id_subscription,skill) values ?',
modified_skills
)
return result;
} catch (error) {
if (conn != null) {
conn.query('ROLLBACK');
}
return 0;
}
},
}
exports.employees = {
getMatching : async function(account){
// select * from employees that have the skills join
let sql = await getConnection();
var matching = await sql.query(`
select employees.*,skills.* from employees
inner join employees_skills
on employees.id=employees_skills.id_employee
inner join skills
on skills.id=employees_skills.id_skill
where (select count(*) from
employees_skills
inner join skills
on skills.id=employees_skills.id_skill
where skills.id in (?)
and where employees_skills.id_employee = employees.id_employee
) > 0
`, account)
return matching;
},
create : async function(email,password,skills){
let sql = null;
try {
let sql = await getConnection();
console.log(sql)
skillsObj.insert(skills,sql);
// insert employee
await sql.query('insert into employee(email, password) values ?, ?',
email,
password,
// insert relations
function(err, result){
if(err)
return 0
id_employee = result.insertId
modified_skills = skills.map((x)=>[id_employee,x])
sql.query(
'insert into employee_skills(id_employee,skill) values ?',
modified_skills,
)
return 1
}
)
sql.release()
} catch (error) {
if (sql != null) {
sql.query('ROLLBACK');
sql.release()
}
console.log(error)
return 0;
}
}
}