Skip to content

Commit

Permalink
added endpoints for creating & retrieving logbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
parkrafael committed Nov 26, 2024
1 parent a783c5f commit 3ec7752
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 103 deletions.
7 changes: 3 additions & 4 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import authRoutes from "./src/routes/authRoutes.js";
import logRoutes from "./src/routes/logRoutes.js";
import authRoutes from "./src/routes/auth-route.js";
import logRoutes from "./src/routes/logbooks-route.js";

dotenv.config();

Expand All @@ -15,9 +15,8 @@ const PORT = process.env.PORT || 8080;
app.use(cors(corsOptions));
app.use(express.json());

//Routes
app.use('/api/auth', authRoutes);
app.use('/api/log', logRoutes);
app.use('/api/logbooks', logRoutes);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
3 changes: 0 additions & 3 deletions backend/src/middlewares/__tests__/auth.test.js

This file was deleted.

3 changes: 1 addition & 2 deletions backend/src/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ const auth = async (req, res, next) => {
}
};

export default auth;

export default auth;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import auth from "../middlewares/auth.js";

const router = express.Router();

//auth test
router.post("/check", auth, async (req, res) => {
res.json({ message: "Auth Check Ok"});
})
Expand Down
17 changes: 0 additions & 17 deletions backend/src/routes/logRoutes.js

This file was deleted.

27 changes: 27 additions & 0 deletions backend/src/routes/logbooks-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from "express";
import auth from "../middlewares/auth.js";
import { createLogbook, getUserLogbooks, getLogbook, createLog } from "../services/logbooks-service.js";

const router = express.Router();

router.post("/", auth, async (req, res) => {
const logbook = await createLogbook(req);
res.status(201).json({ data: logbook });
});

router.get("/", auth, async (req, res) => {
const logbooks = await getUserLogbooks(req);
res.status(200).json({ data: logbooks });
});

router.get("/:id", auth, async (req, res) => {
const logbook = await getLogbook(req);
res.status(200).json({ data: logbook });
});

router.post("/logs", auth, async (req, res) => {
const log = await createLog(req);
res.status(201).json({ data: log });
});

export default router;
76 changes: 0 additions & 76 deletions backend/src/services/cardiacSurgeryAdultService.js

This file was deleted.

58 changes: 58 additions & 0 deletions backend/src/services/logbooks-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export async function createLogbook(req) {
try {
const supabase = req.supabase;
const { type } = req.body;
const { data, error } = await supabase.from("logbooks").insert({ type: type }).select();
if (error) {
console.error("Error in createLogbook:", error.message);
throw new Error(error.message);
} else {
return data[0];
}
} catch (error) {
console.error("Error in createLogbook:", error.message);
throw new Error(error.message);
}
}

export async function getUserLogbooks(req) {
try {
const supabase = req.supabase;
const token = req.header("Authorization")?.split(" ")[1];
const userID = parseUserID(token)
const { data, error } = await supabase.from("logbooks").select().eq("user_id", userID);
if (error) {
console.error("Error in getUserLogbooks:", error.message);
throw new Error(error.message);
} else {
return data;
}
} catch (error) {
console.error("Error in getUserLogbooks:", error.message);
throw new Error(error.message);
}
}

function parseUserID(token) {
const parts = token.split('.');
const decodedPayload = JSON.parse(atob(parts[1]));
const userID = decodedPayload["sub"]
return userID
}

export async function getLogbook(req) {
try {
const supabase = req.supabase;
const { id } = req.params;
const { data, error } = await supabase.from("logbooks").select().eq("logbook_id", id);
if (error) {
console.error("Error in getLogbook:", error.message);
throw new Error(error.message);
} else {
return data[0];
}
} catch (error) {
console.error("Error in getLogbook:", error.message);
throw new Error(error.message);
}
}

0 comments on commit 3ec7752

Please sign in to comment.