youtuber-blog/src/routes/videos.js

34 lines
950 B
JavaScript
Raw Normal View History

2024-04-21 22:41:38 +00:00
/** @typedef {import("fastify").FastifyInstance} FastifyInstance */
import { eq } from "drizzle-orm";
import { db } from "../db/index.js";
import { users } from "../db/schemas.js";
import { authMiddleware } from "../modules/middleware.js";
2024-06-19 17:27:03 +00:00
import { getAccessToken, getVideosFromPlaylist } from "../utils/youtube.js";
2024-04-21 22:41:38 +00:00
/**
*
* @param {FastifyInstance} fastify
* @param {unknown} _
* @param {() => void} done
*/
export const videoRoutes = (fastify, _, done) => {
fastify.register(authMiddleware);
fastify.get("/", async (request, response) => {
try {
2024-04-24 20:49:59 +00:00
const token = await getAccessToken(fastify, request);
2024-04-21 22:41:38 +00:00
const [user] = await db.select().from(users).where(eq(users.id, request.session.user_id));
2024-04-28 18:58:31 +00:00
const videos = await getVideosFromPlaylist(token, user.uploads_playlist_id);
2024-05-12 20:05:06 +00:00
2024-04-21 22:41:38 +00:00
response.send({
success: true,
videos
2024-04-28 18:58:31 +00:00
});
2024-04-21 22:41:38 +00:00
} catch (e) {
console.log(e);
}
});
done();
};