Skip to content

Commit

Permalink
add books api
Browse files Browse the repository at this point in the history
  • Loading branch information
matteotagliatti committed Nov 17, 2023
1 parent 53d1743 commit 8c98a8a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/routes/api/books/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { error } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";

export const GET: RequestHandler = async ({ url, locals: { supabase } }) => {
const username = String(url.searchParams.get("username"));
const status = String(url.searchParams.get("status"));
const limit = Number(url.searchParams.get("limit") || 10);

if (!username) {
throw error(400, "username is required");
}

if (!status) {
throw error(400, "status is required");
}

const { data: user, error: user_error } = await supabase
.from("users")
.select()
.eq("username", username)
.single();

if (user_error) {
throw error(404, "User not found");
}

const { data: books, error: books_error } = await supabase
.from("books")
.select()
.eq("owner", user.id)
.eq("status", status)
.order("finished", { ascending: false })
.limit(limit);

if (books_error) throw error;

return new Response(JSON.stringify(books));
};

1 comment on commit 8c98a8a

@vercel
Copy link

@vercel vercel bot commented on 8c98a8a Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.