-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from TrackER-Corporation/feat/Api-setup
feat: api setup
- Loading branch information
Showing
12 changed files
with
541 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: . | ||
|
@@ -73,4 +71,4 @@ jobs: | |
with: | ||
node-version: "18" | ||
release-type: node | ||
package-name: release-please-action | ||
package-name: release-please-action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ dist-ssr | |
*.sln | ||
*.sw? | ||
*.DS_Store | ||
.env | ||
.env | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.