|
| 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' |
| 4 | + |
| 5 | +// dotenv.config() |
| 6 | + |
| 7 | +const app = new App() |
| 8 | +const port = parseInt(Deno.env.get('PORT') || '') || 3000 |
| 9 | + |
| 10 | +// connect to mongodb |
| 11 | +/* const client = new MongoClient() |
| 12 | +
|
| 13 | +await client.connect(Deno.env.get('DB_URI') || '') |
| 14 | +
|
| 15 | +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()) |
| 22 | + |
| 23 | + res.send('bruh') |
| 24 | +}) |
| 25 | + |
| 26 | +/* // get all notes |
| 27 | +app.get('/notes', async (_, res, next) => { |
| 28 | + try { |
| 29 | + const r = await coll.find({}).toArray() |
| 30 | + res.send(r) |
| 31 | + next() |
| 32 | + } catch (err) { |
| 33 | + next(err) |
| 34 | + } |
| 35 | +}) |
| 36 | +
|
| 37 | +// add new note |
| 38 | +app.post('/notes', async (req, res, next) => { |
| 39 | + try { |
| 40 | +
|
| 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 | + } |
| 50 | + } catch (err) { |
| 51 | + next(err) |
| 52 | + } |
| 53 | +}) |
| 54 | + */ |
| 55 | +/* // delete note |
| 56 | +app.delete('/notes', async (req, res, next) => { |
| 57 | + try { |
| 58 | + const { id } = req.body |
| 59 | + const r = await coll.deleteOne({ _id: new Bson.ObjectId(id) }) |
| 60 | + assertStrictEquals(1, r) |
| 61 | + res.send(`Note with id of ${id} has been deleted`) |
| 62 | + } catch (err) { |
| 63 | + next(err) |
| 64 | + } |
| 65 | +}) |
| 66 | +
|
| 67 | +// update existing note |
| 68 | +app.put('/notes', async (req, res, next) => { |
| 69 | + 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 | + ) |
| 76 | + res.send(`Note with title of ${title} has been updated`) |
| 77 | + } catch (err) { |
| 78 | + next(err) |
| 79 | + } |
| 80 | +}) |
| 81 | + */ |
| 82 | +app.listen(port, () => console.log(`Started on http://localhost:${port}`)) |
0 commit comments