Skip to content

Commit

Permalink
feat: api route and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
LeleDallas committed Apr 27, 2023
1 parent 3bf4c50 commit 6782e38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
74 changes: 30 additions & 44 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,41 @@ export const getRenewableById = asyncHandler(async (req: any, res: any) => {
res.status(200).json(goal)
else {
res.status(400)
throw new Error('renewable not found')
return
}
})

export const getRenewableByOrganizationId = asyncHandler(async (req: any, res: any) => {
const goal = await collections.renewable?.find({ organizationId: new ObjectId(req.params.id) })
if (goal)
res.status(200).json(goal)
else {
res.status(400).json({})
throw new Error('Renewable not found')
console.log(req.params.id)
const renewable = await collections.renewable?.find({ organizationId: new ObjectId(req.params.id) }).toArray()
if (renewable && renewable.length > 0) {
res.status(200).json(renewable)
} else {
res.status(400).json({ message: 'Renewable not found.' })
}
})


export const getRenewableByBuildingId = asyncHandler(async (req: any, res: any) => {
const goal = await collections.renewable?.find({ buildings: new ObjectId(req.params.id) })
if (goal)
res.status(200).json(goal)
else {
res.status(400).json({})
throw new Error('Renewable not found')
const renewable = await collections.renewable?.findOne({ buildings: new ObjectId(req.params.id) })
if (renewable) {
res.status(200).json(renewable)
} else {
res.status(400).json({ message: 'Renewable not found.' })
}
})


export const getAll = asyncHandler(async (req: any, res: any) => {
const goal = await collections.renewable?.find().toArray()
if (goal) res.status(200).json(goal)
else {
res.status(400)
throw new Error('Renewable not found')
}
res.status(200).json(goal)
})


export const create = asyncHandler(async (req: any, res: any) => {
if (!req.body.organizationId) {
res.status(400)
throw new Error('Please add a text field')
return
}
const renewable = await collections.renewable?.insertOne({
name: req.body.name,
Expand All @@ -65,30 +60,32 @@ export const create = asyncHandler(async (req: any, res: any) => {
})

export const updateRenewable = asyncHandler(async (req: any, res: any) => {
if (!req?.params?.id) {
res.status(400)
return
}
const renewable = await collections.renewable?.find(req.params.id)
if (!renewable) {
res.status(400)
throw new Error('Renewable not found')
}
if (!req.params.id) {
res.status(401)
throw new Error('User not found')
return
}
const update = await collections.renewable?.updateOne({ _id: new ObjectId(req.params.id) }, req.body, {})

const update = await collections.renewable?.updateOne({ _id: new ObjectId(req?.params?.id) }, { $set: { ...req.body } }, {})
res.status(200).json(update)
})


export const updateRenewableBuildingsById = asyncHandler(async (req: any, res: any) => {
const renewable = await collections.renewable?.findOne(req.params.id)
if (!renewable) {
if (!!req?.params?.id) {
res.status(400)
throw new Error('Renewable not found')
return
}
if (!req.params.id) {
res.status(401)
throw new Error('User not found')
const renewable = await collections.renewable?.findOne(new ObjectId(req.params.id))
if (!renewable) {
res.status(400)
return
}

renewable.buildings.push(new ObjectId(req.body.building))
renewable.save().then(() => {
res.status(200).json(renewable)
Expand All @@ -102,23 +99,12 @@ export const deleteRenewable = asyncHandler(async (req: any, res: any) => {
const renewable = await collections.renewable?.findOne({ _id: new ObjectId(req.params.id) })
if (!renewable) {
res.status(400)
throw new Error('Renewable not found')
return
}
if (!req.params.id) {
res.status(401)
throw new Error('User not found')
return
}
const update = await collections.renewable?.deleteOne(renewable)
res.status(200).json(update)
})

module.exports = {
updateRenewable,
deleteRenewable,
getAll,
getRenewableById,
create,
getRenewableByOrganizationId,
getRenewableByBuildingId,
updateRenewableBuildingsById
}
2 changes: 1 addition & 1 deletion db/route/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { create, deleteRenewable, getAll, getRenewableById, updateRenewable, get

router.get('/:id', getRenewableById)
router.get('/organization/:id', getRenewableByOrganizationId)
router.get('/all/renewable', getAll)
router.get('/', getAll)
router.get('/building/:id', getRenewableByBuildingId)
router.put('/:id', updateRenewable)
router.put('/buildings/:id', updateRenewableBuildingsById)
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ connectToDatabase()
app.listen(port, () => console.info(`tracker-renewable-service is running`));