This repository was archived by the owner on Aug 8, 2020. It is now read-only.
forked from jrs-innovation-center/art-api-exam-nolist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdal.js
109 lines (97 loc) · 2.45 KB
/
dal.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
//REQUIREMENTS
require('dotenv').config() //connects to .env
const PouchDB = require('pouchdb-core')
PouchDB.plugin(require('pouchdb-adapter-http'))
PouchDB.plugin(require('pouchdb-find'))
const pkGen = require('./lib/pk-generator.js') //generates unique id
const HTTPError = require('node-http-error')
const { pluck } = require('ramda')
const db = new PouchDB(process.env.COUCHDB_URL) // most important, this is how you talk to your database!
//POST a painting (Crudls)
const createPainting = function(painting, cb) {
const newName = pkGen(painting.name)
painting._id = `painting_${newName}`
db.put(painting, cb)
}
//GET a painting (cRudls)
const getPainting = paintingId => db.get(paintingId)
//PUT to update a painting (crUdls)
const updatePainting = function(painting, cb) {
db.put(painting, function(err, updatedPainting) {
if (err) {
cb(err)
return
}
cb(null, updatedPainting)
})
}
//DELETE to delete a painting (cruDls)
const deletePainting = function(paintingId, cb) {
db.get(paintingId, function(err, painting) {
if (err) {
cb(err)
return
}
db.remove(painting, function(err, painting) {
if (err) {
cb(err)
return
}
cb(null, painting)
})
})
}
//GET to list and search paintings (crudLS)
const getPaintings = options =>
db.allDocs(options).then(result => pluck('doc', result.rows))
//POST an artist (Crudls)
const createArtist = function(artist, cb) {
const name = pkGen(artist.name)
artist._id = `artist_${name}`
db.put(artist, cb)
}
//GET an artist (cRudls)
const getArtist = artistId => db.get(artistId)
//PUT to update an artist (crUdls)
const updateArtist = function(artist, cb) {
db.put(artist, function(err, updatedArtist) {
if (err) {
cb(err)
return
}
cb(null, updatedArtist)
})
}
//DELETE to delete an artist (cruDls)
const deleteArtist = function(artistId, cb) {
db.get(artistId, function(err, artist) {
if (err) {
cb(err)
return
}
db.remove(artist, function(err, artist) {
if (err) {
cb(err)
return
}
cb(null, artist)
})
})
}
//GET to list and search artists (crudLS)
const getArtists = options =>
db.allDocs(options).then(result => pluck('doc', result.rows))
//EXPORT IT OUT
const dal = {
createPainting,
getPainting,
updatePainting,
deletePainting,
getPaintings,
createArtist,
getArtist,
updateArtist,
deleteArtist,
getArtists
}
module.exports = dal