-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
125 lines (102 loc) · 3.99 KB
/
api.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
/**
* Get all people
*/
const getAllPeople = async (knex, queryParams) => {
let knexQuery = knex.select('DB_id', 'title', 'firstname', 'lastname_keyname').from('people');
if(queryParams.sort !== undefined) {
switch(queryParams.sort) {
case 'lastname':
knexQuery = knexQuery.orderBy('lastname_keyname', 'asc');
break;
case 'firstname':
knexQuery = knexQuery.orderBy('firstname', 'asc')
break;
case 'birthyear':
knexQuery = knexQuery.orderBy(['birth_year', 'birth_month', 'birth_day_of_month'], 'asc')
break;
}
} else {
knexQuery = knexQuery.orderBy('lastname_keyname', 'asc');
}
if(queryParams.search !== undefined) {
knexQuery = knexQuery
.where('lastname_keyname', 'like', '%' + queryParams.search + '%')
.orWhere('firstname', 'like', '%' + queryParams.search + '%');
}
if(queryParams.lastname !== undefined) {
knexQuery = knexQuery.where('lastname_keyname', 'like', '%' + queryParams.lastname + '%');
}
const count = await knexQuery.clone().count().then((result) => result[0]['count(*)']);
if(queryParams.limit !== undefined && queryParams.offset !== undefined) {
knexQuery = knexQuery.limit(parseInt(queryParams.limit)).offset(parseInt(queryParams.offset));
}
return {
count: count,
data: await knexQuery
}
};
/**
* Get all Publications
*/
const getAllPublications = async (knex, queryParams) => {
let knexQuery = knex.select(['title', 'DB_id']).from('pub_Publications');
knexQuery = knexQuery.orderBy('title', 'asc');
if(queryParams.title !== undefined) {
knexQuery = knexQuery.where('title', 'like', '%' + queryParams.title + '%');
}
if(queryParams.search !== undefined) {
knexQuery = knexQuery
.where('title', 'like', '%' + queryParams.search + '%');
}
const count = await knexQuery.clone().count().then((result) => result[0]['count(*)']);
if(queryParams.limit !== undefined && queryParams.offset !== undefined) {
knexQuery = knexQuery.limit(parseInt(queryParams.limit)).offset(parseInt(queryParams.offset));
}
return {
count: count,
data: await knexQuery
}
};
/**
* Get a single publication
*/
const getSinglePublication = async (knex, id) => {
const core = await knex.select().from('pub_Publications').where({ DB_id: id }).first();
// play, movie, book, book chapter, journal edition or article
const play = await knex.select().from('pub_play').where({ publication_id: id }).first();
const movie = await knex.select().from('pub_movie').where({ publication_id: id }).first();
const journalEdition = await knex.select().from('pub_journal_edition').where({ publication_id: id }).first();
const book = await knex.select().from('pub_book').where({ publication_id: id }).first();
const bookChapter = await knex.select().from('pub_book_chapter').where({ publication_id: id }).first();
const article = await knex.select().from('pub_article').where({ publication_id: id }).first();
const authors = await knex.select('col_Authors.*', 'people.title', 'people.firstname', 'people.lastname_keyname').from('col_Authors')
.where({ publication_id: id })
.innerJoin('people', 'col_Authors.person_id', 'people.DB_id')
return { core, play, movie, journalEdition, book, bookChapter, article, authors };
};
/**
* Get a single person
*/
const getSinglePerson = async (knex, id) => {
const core = await knex
.select(['people.*',
'alsoKnownAs.DB_id as alt_DB_id',
'alsoKnownAs.lastname_keyname as alt_lastname_keyname',
'alsoKnownAs.firstname as alt_firstname',
'alsoKnownAs.title as alt_title'])
.from('people')
.leftJoin('people as alsoKnownAs', 'people.is_alternative_name_of', 'alsoKnownAs.DB_id')
.where({ 'people.DB_id': id })
.first();
// play, movie, book, book chapter, journal edition or article
const works = await knex.select('col_Authors.*', 'pub_Publications.title').from('col_Authors')
.where({ person_id: id })
.innerJoin('pub_Publications', 'col_Authors.publication_id', 'pub_Publications.DB_id');
return { core, works };
}
module.exports = {
getAllPublications,
getAllPeople,
getSinglePublication,
getSinglePerson,
};