Skip to content

Commit

Permalink
Merge pull request #13 from TrackER-Corporation/feat/Api-setup
Browse files Browse the repository at this point in the history
feat: api setup
  • Loading branch information
LeleDallas authored Apr 27, 2023
2 parents a5dfd3a + f38f4e9 commit 46c39f3
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 223 deletions.
30 changes: 14 additions & 16 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,31 @@ jobs:
npm run coverage
env:
CI: true
DB_NAME: ${{ secrets.DB_NAME }}
DB_CONN_STRING: ${{ secrets.DB_CONN_STRING }}
COLLECTION: "renewables"
- name: Vitest Coverage Report
uses: davelosert/[email protected]
with:
vite-config-path: './vite.config.ts'
vite-config-path: "./vite.config.ts"

docker:
name: Docker
runs-on: ubuntu-latest
needs: sonarcloud
steps:
-
name: Checkout
name: Docker
runs-on: ubuntu-latest
needs: sonarcloud
steps:
- name: Checkout
uses: actions/checkout@v3
-
name: Set up QEMU
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
Expand All @@ -73,4 +71,4 @@ jobs:
with:
node-version: "18"
release-type: node
package-name: release-please-action
package-name: release-please-action
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dist-ssr
*.sln
*.sw?
*.DS_Store
.env
.env
coverage
110 changes: 110 additions & 0 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

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

export const getRenewableByOrganizationId = asyncHandler(async (req: any, res: any) => {
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 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()
res.status(200).json(goal)
})


export const create = asyncHandler(async (req: any, res: any) => {
if (!req.body.organizationId) {
res.status(400)
return
}
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) => {
if (!req?.params?.id) {
res.status(400)
return
}
const renewable = await collections.renewable?.find(req.params.id)
if (!renewable) {
res.status(400)
return
}

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) => {
if (!!req?.params?.id) {
res.status(400)
return
}
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)
}).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)
return
}
if (!req.params.id) {
res.status(401)
return
}
const update = await collections.renewable?.deleteOne(renewable)
res.status(200).json(update)
})
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('/', 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) {
console.error(error);
}
Expand Down
9 changes: 3 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import { connectToDatabase } from './db/services/database.service';
import route from "./db/route/route"

dotenv.config();

Expand All @@ -10,15 +11,11 @@ const port = process.env.PORT;
const app = express();
app.use(cors());
app.use(express.json());
app.use(route);

app.use(
'/',
(req, res, next) => {
return res.status(200).json({ "msg": "test" })
});

connectToDatabase()
app.listen(port, () => console.info(`tracker-renewable-service is running`));


export default { app }
export default app
Loading

0 comments on commit 46c39f3

Please sign in to comment.