forked from rjcorwin/BeLL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
171 lines (143 loc) · 3.65 KB
/
app.js
File metadata and controls
171 lines (143 loc) · 3.65 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var couchapp = require('couchapp')
, path = require('path')
ddoc =
{ _id:'_design/library'
, rewrites :
[ {from:"/", to:'index.html'}
, {from:"/api", to:'../../'}
, {from:"/api/*", to:'../../*'}
, {from:"/*", to:'*'}
]
}
ddoc.views = {
/*
* Get all grades and number of documents in that grade.
*
* /_design/hammock/_view/grades?group=true
*/
grades: {
map: function(doc){
if (doc.grade && doc.type == 'resource') {
emit(doc.grade, 1)
}
},
reduce: function(keys, values) {
return values.length
}
},
/*
* Get all subjects in each grade and number of documents in that grade subject.
view: _design/hammock/_view/grade_subjects?group_level=1
result: distinct grades and number of documents in that grade.
view: _design/hammock/_view/grade_subjects?group=true&startkey=[1,"a"]&endkey=[1,"z"]
result: all subjects for grade one
*/
grade_subjects: {
map: function(doc) {
if (doc.grade && doc.subject && doc.type == 'resource') {
emit([doc.grade, doc.subject], doc._id)
}
},
reduce: function(tag, counts) {
return tag.length
}
},
subject_levels: {
map: function(doc) {
if (doc.subject && doc.level && doc.type == 'resource') {
emit([doc.subject, doc.level], doc._id)
}
},
reduce: function(tag, counts) {
return tag.length
}
},
/*
* Get all resources for given grade and subject.
* @todo Figure out how to modify the reduce in grade_subjects view so I we can roll this
view into it.
view: _design/hammock/_view/grade_subjec_resourcess?key=['grade', 'subject']
result: A list of document IDs for the given grade and subject.
*/
grade_subject_resources: {
map: function(doc) {
if (doc.grade && doc.subject && doc.type == 'resource') {
emit([doc.grade, doc.subject], doc.title)
}
}
},
subject_level_resources: {
map: function(doc) {
if (doc.subject && doc.level && doc.type == 'resource') {
emit([doc.subject, doc.level], doc.title)
}
}
},
/*
* SELECT subject FROM documents_table;
*/
subjects_all: {
map: function(doc) {
if (doc.subject && doc.type == 'resource') {
emit(doc.subject, 1)
}
}
},
grades_all: {
map: function(doc){
if (doc.grade && doc.type == 'resource') {
emit(doc.grade, 1)
}
}
},
resource_all: {
map: function(doc) {
if (doc.type == "resource") {
emit(doc._id, true)
}
}
},
feedback_all: {
map: function(doc) {
if (doc.type == "feedback") {
emit(doc._id, true)
}
}
},
feedback_by_resource: {
map: function(doc) {
if (doc.type == "feedback") {
emit(doc.resource, doc._id)
}
}
},
/*
* _design/library/_view/feedback_rating_totals?group=true&startkey=["resource-0001","1"]&endkey=["resource-0001","5"]
*/
feedback_rating_totals: {
map: function(doc) {
if (doc.type == "feedback") {
emit([doc.resource, doc.rating], doc.rating)
}
},
reduce: function(keys, values, rereduce) {
if(!rereduce) {
log("keys")
log(keys)
log("values")
log(values)
return values.length
}
else {
log("REREDUCE ENGAGE")
}
}
}
}
ddoc.validate_doc_update = function (newDoc, oldDoc, userCtx) {
if (newDoc._deleted === true && userCtx.roles.indexOf('_admin') === -1) {
throw "Only admin can delete documents on this database."
}
}
couchapp.loadAttachments(ddoc, path.join(__dirname, 'attachments'))
module.exports = ddoc