22import { App } from '@tinyhttp/app'
33import dotenv from 'dotenv'
44import mongodb from 'mongodb'
5- import { form } from 'body-parsec'
5+ import assert from 'assert'
6+ import * as parser from 'body-parsec'
67
78dotenv . config ( )
89
@@ -15,6 +16,7 @@ const client = new mongodb.MongoClient(process.env.DB_URI, {
1516} )
1617const dbName = 'notes'
1718client . connect ( ( err ) => {
19+ assert . equal ( null , err )
1820 console . log ( 'successfully connected with mongodb' )
1921 db = client . db ( dbName )
2022} )
@@ -29,16 +31,13 @@ app.get('/notes', async (_, res, next) => {
2931 }
3032} )
3133
32- app . use ( '/notes' , async ( req , res , next ) => {
33- await form ( ) ( req , res , next )
34- } )
35-
3634// add new note
3735app . post ( '/notes' , async ( req , res , next ) => {
36+ await parser . form ( ) ( req , res )
3837 try {
39- await db . collection ( 'notes' ) . insertOne ( { title : req . query . title , desc : req . query . desc } )
40-
41- res . send ( `Note with title of "${ req . query . title } " has been added` )
38+ const r = await db . collection ( 'notes' ) . insertOne ( { title : req . body . title , desc : req . body . desc } )
39+ assert . equal ( 1 , r . insertedCount )
40+ res . send ( `Note with title of "${ req . body . title } " has been added` )
4241 } catch ( err ) {
4342 next ( err )
4443 }
@@ -47,7 +46,8 @@ app.post('/notes', async (req, res, next) => {
4746// delete note
4847app . delete ( '/notes' , async ( req , res , next ) => {
4948 try {
50- await db . collection ( 'notes' ) . deleteOne ( { _id : new mongodb . ObjectId ( req . query . id ) } )
49+ const r = await db . collection ( 'notes' ) . deleteOne ( { _id : new mongodb . ObjectId ( req . query . id ) } )
50+ assert . equal ( 1 , r . deletedCount )
5151 res . send ( `Note with id of ${ req . query . id } has been deleted` )
5252 } catch ( err ) {
5353 next ( err )
0 commit comments