Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating API to search the amount of doujins in database #2

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/metadata/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const app = require("./src/app.js")
const PORT = Number(process?.env?.APP_PORT?.trim() || 3000);

AlexandreSenpai marked this conversation as resolved.
Show resolved Hide resolved
app.listen(PORT, function(err){
if (err) console.log(err);
console.log(`Server listening on http://localhost:${PORT}`);
});
5,035 changes: 5,035 additions & 0 deletions backend/metadata/package-lock.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions backend/metadata/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "erohoshi",
"version": "1.0.0",
"description": "",
"main": "./src/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1",
"firebase": "^9.8.3",
"firebase-admin": "^11.0.0",
"typescript": "^4.7.4"
}
}
12 changes: 12 additions & 0 deletions backend/metadata/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require("express");
const routes = require("./routes/routes.js")

const app = express();

app.use(express.json({
type: ["application.json"]
}))

app.use(routes);

module.exports = app;
25 changes: 25 additions & 0 deletions backend/metadata/src/controller/getTotalDoujins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const db = require("../firebase/db.js")

async function getTotalDoujins(req, res) {

// get the document "doujins" in the section "metadata"
const collection = db.collection("metadata").doc("doujins");
// read the document
const doujins = await collection.get();
const amountOfDoujins = doujins.data()

// return the data from document (in this case, the amount of doujins in database)
if (!doujins.exists) {
return res
.status(404)
.send("There's nothing here")
} else {
return res
.status(200)
.json({
"total": Number(amountOfDoujins.total)
})
}
}

module.exports = getTotalDoujins;
14 changes: 14 additions & 0 deletions backend/metadata/src/firebase/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { initializeApp, cert, applicationDefault } = require("firebase-admin/app")
const { getFirestore } = require("firebase-admin/firestore")
const envName = require("../var.js")

const serviceAccount = require("../../../eroneko-c890a77e5039.json")

initializeApp({
credential: envName == "local" ? cert(serviceAccount) : applicationDefault()
})

const db = getFirestore()

module.exports = db;

14 changes: 14 additions & 0 deletions backend/metadata/src/routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Router } = require("express")
const getTotalDoujins = require("../controller/getTotalDoujins.js")

const router = Router();

router
.get("/metadata/doujins/total", getTotalDoujins)
.get("/healthcheck", (req, res) => {
res
.status(200)
.send("Okay")
})

module.exports = router;
5 changes: 5 additions & 0 deletions backend/metadata/src/var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("dotenv").config()

const envName = process.env.NODE_ENV || "qa";

module.exports = envName
Loading