1
- import { App } from '../../app.ts'
2
- // import { MongoClient, Bson } from 'https://deno.land/x/mongo@v0.21.0/mod.ts'
3
- // import * as dotenv from 'https://deno.land/x/dotenv/mod.ts'
1
+ import { App , Handler } from '../../app.ts'
2
+ import { Request } from '../../request.ts'
3
+ import { MongoClient , Bson } from 'https://deno.land/x/mongo@v0.21.2/mod.ts'
4
+ import * as dotenv from 'https://deno.land/x/tiny_env@1.0.0/mod.ts'
4
5
5
- // dotenv.config ()
6
+ dotenv . load ( )
6
7
7
- const app = new App ( )
8
+ type Req = Request & {
9
+ bodyResult : any
10
+ }
11
+
12
+ const app = new App < any , Req > ( {
13
+ onError : ( err , req ) => {
14
+ console . log ( err )
15
+ req . respond ( { body : 'Error' } )
16
+ }
17
+ } )
8
18
const port = parseInt ( Deno . env . get ( 'PORT' ) || '' ) || 3000
9
19
10
20
// connect to mongodb
11
- /* const client = new MongoClient()
21
+ const client = new MongoClient ( )
12
22
13
23
await client . connect ( Deno . env . get ( 'DB_URI' ) || '' )
14
24
15
25
const db = client . database ( 'notes' )
16
- const coll = db.collection('notes') */
17
-
18
- app . get ( async ( req , res ) => {
19
- const buf : Uint8Array = await Deno . readAll ( req . body )
20
-
21
- console . log ( buf . toString ( ) )
26
+ const coll = db . collection ( 'notes' )
22
27
23
- res . send ( 'bruh' )
24
- } )
25
-
26
- /* // get all notes
28
+ // get all notes
27
29
app . get ( '/notes' , async ( _ , res , next ) => {
28
30
try {
29
31
const r = await coll . find ( { } ) . toArray ( )
32
+
30
33
res . send ( r )
31
34
next ( )
32
35
} catch ( err ) {
33
36
next ( err )
34
37
}
35
38
} )
36
39
40
+ const bodyParser : Handler < Req > = async ( req , res , next ) => {
41
+ const buf = await Deno . readAll ( req . body )
42
+
43
+ const dec = new TextDecoder ( )
44
+
45
+ const body = dec . decode ( buf )
46
+
47
+ try {
48
+ const result = JSON . parse ( body )
49
+ req . bodyResult = result
50
+ } catch ( e ) {
51
+ next ( e )
52
+ }
53
+ next ( )
54
+ }
55
+ app . use ( bodyParser )
56
+
37
57
// add new note
38
58
app . post ( '/notes' , async ( req , res , next ) => {
39
59
try {
60
+ const { title, desc } = req . bodyResult
61
+ const r = await coll . insertOne ( { title, desc } )
40
62
41
-
42
- if (result?.value) {
43
- const { title, desc } = result.value
44
- const r = await coll.insertOne({ title, desc })
45
- assertStrictEquals(1, r.insertedCount)
46
- res.send(`Note with title of "${title}" has been added`)
47
- } else {
48
- next('No body provided')
49
- }
63
+ res . send ( `Note with title of "${ title } " has been added` )
50
64
} catch ( err ) {
51
65
next ( err )
52
66
}
53
67
} )
54
- */
55
- /* / / delete note
68
+
69
+ // delete note
56
70
app . delete ( '/notes' , async ( req , res , next ) => {
57
71
try {
58
- const { id } = req.body
59
- const r = await coll.deleteOne({ _id: new Bson.ObjectId(id) })
60
- assertStrictEquals(1, r)
72
+ const { id } = req . bodyResult
73
+ await coll . deleteOne ( { _id : new Bson . ObjectId ( id ) } )
74
+
61
75
res . send ( `Note with id of ${ id } has been deleted` )
62
76
} catch ( err ) {
63
77
next ( err )
@@ -67,16 +81,12 @@ app.delete('/notes', async (req, res, next) => {
67
81
// update existing note
68
82
app . put ( '/notes' , async ( req , res , next ) => {
69
83
try {
70
- const { title, desc, id } = req.body
71
- await coll.findOneAndUpdate(
72
- { _id: new mongodb.ObjectId(id) },
73
- { $set: { title, desc } },
74
- { returnOriginal: false, upsert: true }
75
- )
84
+ const { title, desc, id } = req . bodyResult
85
+ await coll . updateOne ( { _id : new Bson . ObjectId ( id ) } , { $set : { title, desc } } )
76
86
res . send ( `Note with title of ${ title } has been updated` )
77
87
} catch ( err ) {
78
88
next ( err )
79
89
}
80
90
} )
81
- */
91
+
82
92
app . listen ( port , ( ) => console . log ( `Started on http://localhost:${ port } ` ) )
0 commit comments