Skip to content

Commit

Permalink
update to ESM module
Browse files Browse the repository at this point in the history
  • Loading branch information
aniskhan001 committed Aug 30, 2024
1 parent 4934248 commit 045ac6d
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 1,522 deletions.
4 changes: 1 addition & 3 deletions api/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const config = {
export const Config = {
uri: 'mongodb://mongo:27017/users'
}

module.exports = config
6 changes: 3 additions & 3 deletions api/model.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import mongoose from 'mongoose'
import config from './config'
import {Config} from './config.js'

mongoose.connect(config.uri)
mongoose.connect(Config.uri)

const userSchema = {
firstName: String,
lastName: String,
tags: Array
}

module.exports = mongoose.model('users', userSchema)
export const Model = mongoose.model('users', userSchema)
94 changes: 42 additions & 52 deletions api/router.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
// Dependencies
import express from 'express'
const router = express.Router()
const Model = import('./model')
export const Router = express.Router()
import { Model } from './model.js'

// list users
router.get('/', function (req, res) {
Router.get('/', function (req, res) {
let taglist = req.query.tags
if (taglist) {
// get users by tag
taglist = taglist.split(',')

Model.find({ tags: { $in: taglist } }, function (err, data) {
if (err) {
res.status(404).json({ message: 'not found' })
} else {
const users = []
for (const x in data) {
users.push({
id: data[x].id,
name: data[x].firstName + ' ' + data[x].lastName,
tags: data[x].tags
})
}
res.status(200).json(users)
}
Model.find({ tags: { $in: taglist } })
.then(users => {
res.status(200).json(users)
})
.catch(err => {
res.status(404).json({ message: 'not found' })
})
} else {
Model.find({}, function (err, data) {
if (err) {
res.status(500).json(err)
return
}
res.status(200).json(data)
Model.find({})
.then(users => {
res.status(200).json(users)
})
.catch(err => {
res.status(500).json(err)
})
}
})

// save user info
router.post('/', function (req, res) {
Router.post('/', function (req, res) {
const db = new Model()
const firstName = req.body.firstName
const lastName = req.body.lastName
Expand All @@ -47,46 +39,44 @@ router.post('/', function (req, res) {
} else {
db.firstName = firstName
db.lastName = lastName
db.save(function (err, data) {
if (err) {
res.status(500).send({ message: 'error inserting to db' })
} else {
res.status(200).send({ message: data })
}
db.save()
.then(data => {
res.status(200).send({ message: data })
})
.catch(err => {
res.status(500).send({ message: 'error inserting to db' })
})
}
})

// get single user
router.get('/:id', function (req, res) {
Model.findById(req.params.id, function (err, data) {
if (err) {
res.status(404).json(err)
} else {
const message = data.firstName + ' ' + data.lastName
res.status(200).json({ data: message })
}
Router.get('/:id', function (req, res) {
Model.findById(req.params.id)
.then(data => {
const message = data.firstName + ' ' + data.lastName
res.status(200).json({ data: message })
})
.catch(err => {
res.status(404).json(err)
})
})

// post tags
router.post('/:id/tags', function (req, res) {
Router.post('/:id/tags', function (req, res) {
const tags = req.body.tags

Model.findById(req.params.id, function (err, data) {
if (err) {
res.status(500).json({})
return
}
Model.findById(req.params.id)
.then(data => {
data.tags = tags
data.save(function (err, updatedData) {
if (err) {
res.status(500).json({})
} else {
res.status(200).send({})
}
data.save()
.then(updatedData => {
res.status(200).json({})
})
.catch(err => {
res.status(500).json({})
})
})
.catch(err => {
res.status(500).json({})
})
})

module.exports = router
29 changes: 0 additions & 29 deletions azure-pipelines.yml

This file was deleted.

Loading

0 comments on commit 045ac6d

Please sign in to comment.