-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added endpoints for creating & retrieving logbooks
- Loading branch information
1 parent
a783c5f
commit 3ec7752
Showing
8 changed files
with
89 additions
and
103 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -26,5 +26,4 @@ const auth = async (req, res, next) => { | |
} | ||
}; | ||
|
||
export default auth; | ||
|
||
export default auth; |
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 was deleted.
Oops, something went wrong.
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,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; |
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |