-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes: - get books (GET /api/books) - get single book (GET /api/books/[slug]) - get book preview (GET /api/books/[slug]/preview) - get book chapter (GET /api/books/[slug]/[chapter]) - get featured book (GET /api/books/featured) additional: - server types closes: #17
- Loading branch information
1 parent
5c0067e
commit 594ea2e
Showing
15 changed files
with
312 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,48 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Book { | ||
id Int @id @default(autoincrement()) | ||
slug String @unique | ||
title String | ||
author String | ||
description String | ||
cover String | ||
datePublished DateTime | ||
length Int | ||
genre String | ||
accentColor String | ||
chapters Chapter[] | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} | ||
|
||
model Chapter { | ||
id Int @id @default(autoincrement()) | ||
number Int | ||
audioUrl String | ||
title String | ||
content String | ||
bookId Int | ||
book Book @relation(fields: [bookId], references: [id]) | ||
ambientSections AmbientSection[] | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} | ||
|
||
model AmbientSection { | ||
id Int @id @default(autoincrement()) | ||
start Int | ||
end Int | ||
description String | ||
chapterId Int | ||
chapter Chapter @relation(fields: [chapterId], references: [id]) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} |
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 @@ | ||
// seed script |
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 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 @@ | ||
export * from "./prisma"; |
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,13 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prismaClientSingleton = () => { | ||
return new PrismaClient(); | ||
}; | ||
|
||
declare const globalThis: { | ||
prismaGlobal: ReturnType<typeof prismaClientSingleton>; | ||
} & typeof global; | ||
|
||
export const prisma = globalThis.prismaGlobal ?? prismaClientSingleton(); | ||
|
||
if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma; |
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,28 @@ | ||
import type { AmbientSection, Book, Chapter } from "@prisma/client"; | ||
import type { Handler } from "~/types"; | ||
import { prisma } from "~/lib/server"; | ||
|
||
export type GetBookChapterParams = { query: { slug: string; chapter: string } }; | ||
export type GetBookChapterPayload = Chapter & { | ||
ambientSections: AmbientSection[]; | ||
book: Pick<Book, "title" | "author" | "accentColor">; | ||
}; | ||
|
||
const handler: Handler<GetBookChapterParams, GetBookChapterPayload> = async (req, res) => { | ||
try { | ||
const chapter = await prisma.chapter.findFirst({ | ||
where: { number: parseInt(req.query.chapter), book: { slug: req.query.slug } }, | ||
include: { ambientSections: true, book: { select: { title: true, author: true, accentColor: true } } }, | ||
}); | ||
|
||
if (!chapter) { | ||
return res.status(404).json({ status: "error", message: "Chapter not found" }); | ||
} | ||
|
||
return res.status(200).json({ status: "success", ...chapter }); | ||
} catch (e) { | ||
return res.status(500).json({ status: "error", message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export default handler; |
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,25 @@ | ||
import type { Book } from "@prisma/client"; | ||
import type { Handler } from "~/types"; | ||
import { prisma } from "~/lib/server"; | ||
|
||
export type GetBookParams = { query: { slug: string } }; | ||
export type GetBookPayload = Book; | ||
|
||
const handler: Handler<GetBookParams, GetBookPayload> = async (req, res) => { | ||
try { | ||
const book = await prisma.book.findUnique({ | ||
where: { slug: req.query.slug }, | ||
include: { chapters: { include: { ambientSections: true } } }, | ||
}); | ||
|
||
if (!book) { | ||
return res.status(404).json({ status: "error", message: "Book not found" }); | ||
} | ||
|
||
return res.status(200).json({ status: "success", ...book }); | ||
} catch (error) { | ||
return res.status(500).json({ status: "error", message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export default handler; |
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,22 @@ | ||
import type { Book } from "@prisma/client"; | ||
import type { Handler } from "~/types"; | ||
import { prisma } from "~/lib/server"; | ||
|
||
export type GetBookPreviewParams = { query: { slug: string } }; | ||
export type GetBookPreviewPayload = Book; | ||
|
||
const handler: Handler<GetBookPreviewParams, GetBookPreviewPayload> = async (req, res) => { | ||
try { | ||
const book = await prisma.book.findUnique({ where: { slug: req.query.slug } }); | ||
|
||
if (!book) { | ||
return res.status(404).json({ status: "error", message: "Book not found" }); | ||
} | ||
|
||
return res.status(200).json({ status: "success", ...book }); | ||
} catch (e) { | ||
return res.status(500).json({ status: "error", message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export default handler; |
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,17 @@ | ||
import type { Book } from "@prisma/client"; | ||
import type { Handler } from "~/types"; | ||
import { prisma } from "~/lib/server"; | ||
|
||
export type GetFeaturedBookParams = {}; | ||
export type GetFeaturedBookPayload = Pick<Book, "slug">; | ||
|
||
const handler: Handler<GetFeaturedBookParams, GetFeaturedBookPayload> = async (req, res) => { | ||
try { | ||
const latestBook = (await prisma.book.findMany({ orderBy: { id: "desc" }, take: 1, select: { slug: true } }))[0]; | ||
return res.status(200).json({ status: "success", slug: latestBook.slug }); | ||
} catch (e) { | ||
return res.status(500).json({ status: "error", message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export default handler; |
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 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,15 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
export const config = { | ||
matcher: "/api/:function*", | ||
}; | ||
|
||
export const isAuthenticated = (req: NextRequest) => { | ||
return false; | ||
}; | ||
|
||
export function middleware(request: NextRequest) { | ||
if (!isAuthenticated(request)) { | ||
return Response.json({ success: false, message: "authentication failed" }, { status: 401 }); | ||
} | ||
} |
Oops, something went wrong.