Skip to content

Commit

Permalink
feat: create renewable route
Browse files Browse the repository at this point in the history
  • Loading branch information
LeleDallas committed Apr 25, 2023
1 parent fdaccff commit 897f189
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 9 deletions.
124 changes: 124 additions & 0 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

import asyncHandler from 'express-async-handler'
import { ObjectId } from 'mongodb'
import { collections } from '../services/database.service'


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

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')
}
})


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')
}
})


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')
}
})


export const create = asyncHandler(async (req: any, res: any) => {
if (!req.body.organizationId) {
res.status(400)
throw new Error('Please add a text field')
}
const renewable = await collections.renewable?.insertOne({
name: req.body.name,
organizationId: req.body.organizationId,
buildings: req.body.buildings,
earning: req.body.earning,
organization: req.body.organization,
price: req.body.price,
type: req.body.type,
resourcesType: req.body.resourcesType,
})
res.status(200).json(renewable)
})

export const updateRenewable = asyncHandler(async (req: any, res: any) => {
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')
}
const update = await collections.renewable?.updateOne({ _id: new ObjectId(req.params.id) }, 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) {
res.status(400)
throw new Error('Renewable not found')
}
if (!req.params.id) {
res.status(401)
throw new Error('User not found')
}
renewable.buildings.push(new ObjectId(req.body.building))
renewable.save().then(() => {
res.status(200).json(renewable)
}).catch((e: string) => {
res.status(400)
throw new Error(e)
})
})

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')
}
if (!req.params.id) {
res.status(401)
throw new Error('User not found')
}
const update = await collections.renewable?.deleteOne(renewable)
res.status(200).json(update)
})

module.exports = {
updateRenewable,
deleteRenewable,
getAll,
getRenewableById,
create,
getRenewableByOrganizationId,
getRenewableByBuildingId,
updateRenewableBuildingsById
}
14 changes: 14 additions & 0 deletions db/route/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express';
const router = express.Router();
import { create, deleteRenewable, getAll, getRenewableById, updateRenewable, getRenewableByOrganizationId, getRenewableByBuildingId, updateRenewableBuildingsById } from '../controller/controller';

router.get('/:id', getRenewableById)
router.get('/organization/:id', getRenewableByOrganizationId)
router.get('/all/renewable', getAll)
router.get('/building/:id', getRenewableByBuildingId)
router.put('/:id', updateRenewable)
router.put('/buildings/:id', updateRenewableBuildingsById)
router.post('/renewable', create)
router.delete('/:id', deleteRenewable)

export default router;
8 changes: 4 additions & 4 deletions db/services/database.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as mongoDB from "mongodb";
import * as dotenv from "dotenv";

export const collections: { cards?: mongoDB.Collection } = {}
export const collections: { renewable?: mongoDB.Collection } = {}

export async function connectToDatabase() {
dotenv.config();
Expand All @@ -13,11 +13,11 @@ export async function connectToDatabase() {

const db: mongoDB.Db = client.db(process.env.DB_NAME);

const cardsCollection: mongoDB.Collection = db.collection(process.env.CARDS_COLLECTION_NAME!);
const collection: mongoDB.Collection = db.collection(process.env.COLLECTION!);

collections.cards = cardsCollection;
collections.renewable = collection;

console.log(`Successfully connected to database: ${db.databaseName} and collection: ${cardsCollection.collectionName}`);
console.log(`Successfully connected to database: ${db.databaseName} and collection: ${collection.collectionName}`);
} catch (error) {