This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
postChoirSongPart.js
100 lines (94 loc) · 3.28 KB
/
postChoirSongPart.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
const Nano = require('nano')
const debug = require('debug')('choirless')
const kuuid = require('kuuid')
let nano = null
let db = null
const DB_NAME = process.env.COUCH_CHOIRLESS_DATABASE
// create/edit a choir's song part
// Parameters:
// - `choirId` - the id of the choir (required)
// - `songId` - the id of the song (required)
// - `partId` - the id of the part (required for updates, if omitted a new song part is created)
// - `partName` - name of the part e.g. drums, alto
// - `partType` - one of `backing`/`reference`/`rendition`
// - `userId` - the id of the user (required for new parts)
// - `userName` - the name of the user (required for new parts)
const postChoirSongPart = async (opts) => {
// connect to db - reuse connection if present
if (!db) {
nano = Nano(process.env.COUCH_URL)
db = nano.db.use(DB_NAME)
}
// extract parameters
const now = new Date()
let partId
let doc = {}
// is this a request to edit an existing song part
if (opts.choirId && opts.songId && opts.partId) {
try {
const id = opts.choirId + ':song:' + opts.songId + ':part:' + opts.partId
debug('postChoirSongPart fetch partId', id)
doc = await db.get(id)
doc.partType = opts.partType ? opts.partType : doc.partType
doc.offset = typeof opts.offset === 'number' ? opts.offset : doc.offset
doc.frontendOffset = typeof opts.frontendOffset === 'number' ? opts.frontendOffset : doc.frontendOffset || 0
doc.aspectRatio = opts.aspectRatio ? opts.aspectRatio : doc.aspectRatio
doc.hidden = typeof opts.hidden === 'boolean' ? opts.hidden : doc.hidden || false
doc.audio = typeof opts.audio === 'boolean' ? opts.audio : doc.audio || false
doc.volume = opts.volume ? opts.volume : doc.volume
partId = opts.partId
} catch (e) {
return {
body: { ok: false, message: 'song part not found' },
statusCode: 404,
headers: { 'Content-Type': 'application/json' }
}
}
} else {
if (!opts.choirId || !opts.songId || !opts.userId || !opts.userName) {
return {
body: { ok: false, message: 'missing mandatory parameters' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
partId = kuuid.id()
doc = {
_id: opts.choirId + ':song:' + opts.songId + ':part:' + partId,
type: 'songpart',
partId: partId,
songId: opts.songId,
choirId: opts.choirId,
userId: opts.userId,
userName: opts.userName,
createdOn: now.toISOString(),
partNameId: opts.partNameId || '',
partName: opts.partName || '',
partType: opts.partType || 'backing',
offset: opts.offset || 0,
frontendOffset: opts.frontendOffset || 0,
aspectRatio: opts.aspectRatio || '',
volume: opts.volume || 1.0,
hidden: false,
audio: typeof opts.audio === 'boolean' ? opts.audio : false
}
}
// write songpart to database
let statusCode = 200
let body = null
try {
debug('postChoirSongPart write data', doc)
await db.insert(doc)
body = { ok: true, partId: partId }
} catch (e) {
body = { ok: false }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = postChoirSongPart