Skip to content

Commit

Permalink
Merge pull request #152 from COS301-SE-2024/feat/writeathons
Browse files Browse the repository at this point in the history
Feat/writeathons
  • Loading branch information
KhyalKara authored Sep 28, 2024
2 parents f6eb9b0 + 68e5b9a commit 7563b1c
Show file tree
Hide file tree
Showing 37 changed files with 3,009 additions and 568 deletions.
68 changes: 20 additions & 48 deletions apps/writeme/app/api/export/chapter/route.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,35 @@
/* v8 ignore start */
import { generateHTML } from '@tiptap/html';
import type { JSONContent} from '@tiptap/core'

import { auth } from '../../../../auth';
import { NextResponse } from 'next/server';
import { exportChapterSchema } from '../../../../db/chapter-schema';
import { getChapter } from '../../../../services/chapters';
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Bold from '@tiptap/extension-bold'
import StarterKit from '@tiptap/starter-kit'

import { any } from 'zod';
import { getPublishedChapter } from '../../../../services/chapters';

const puppeteer = require("puppeteer")

export async function POST(req: Request){
export async function POST(req: Request) {
try {
const session = await auth();

if (!session?.user){
return new NextResponse(JSON.stringify({
status: 'fail', message: "You are not logged in",
}), { status : 401})
if (!session?.user) {
return new NextResponse(
JSON.stringify({ status: 'fail', message: 'You are not logged in' }),
{ status: 401 }
);
}

const input = exportChapterSchema.parse(await req.json());
const chapter = await getPublishedChapter(input.id);

const chapter = await getChapter(input.id);

const content :JSONContent = {
"type": 'doc',
"content": <any>chapter.blocks,
if (!chapter) {
return new NextResponse(
JSON.stringify({ status: 'fail', message: 'Chapter not found' }),
{ status: 404 }
);
}

// console.log(content)

const html = generateHTML( content , [
StarterKit,
])

console.log(html)
return new NextResponse(JSON.stringify(chapter), { status: 200 });
} catch (error) {
console.error('Error fetching chapter:', error);


const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html, {waitUntil: 'networkidle0'})
const pdfBuffer = await page.pdf({format: 'A4'})
await browser.close();
const response = new NextResponse(pdfBuffer);

response.headers.set("Content-Type", "application/pdf")
console.log("render complete")

return response;

}catch (e) {
console.log(e)
return new NextResponse(
JSON.stringify({ status: 'error', message: 'Failed to fetch chapter' }),
{ status: 500 }
);
}


}
68 changes: 18 additions & 50 deletions apps/writeme/app/api/export/story/route.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,35 @@
/* v8 ignore start */
import { generateHTML } from '@tiptap/html';
import { auth } from '../../../../auth';
import { NextResponse } from 'next/server';
import { exportChapterSchema } from '../../../../db/chapter-schema';
import { getChapter } from '../../../../services/chapters';
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Bold from '@tiptap/extension-bold'
import StarterKit from '@tiptap/starter-kit'

import { any } from 'zod';
import { getPublishedStory } from '../../../../services/stories';

const puppeteer = require("puppeteer")

export async function POST(req: Request){
export async function POST(req: Request) {
try {
const session = await auth();

if (!session?.user){
return new NextResponse(JSON.stringify({
status: 'fail', message: "You are not logged in",
}), { status : 401})
if (!session?.user) {
return new NextResponse(
JSON.stringify({ status: 'fail', message: 'You are not logged in' }),
{ status: 401 }
);
}

const input = exportChapterSchema.parse(await req.json());

const story = await getPublishedStory(input.id);

let jsonContent = []

story.chapters.forEach((c) => jsonContent.push(...c.blocks))


const content = {
"type": 'doc',
"content": jsonContent
if (!story) {
return new NextResponse(
JSON.stringify({ status: 'fail', message: 'Story not found' }),
{ status: 404 }
);
}

console.log(content)

const html = generateHTML( content , [
StarterKit,
])
return new NextResponse(JSON.stringify(story), { status: 200 });
} catch (error) {
console.error('Error fetching story:', error);

console.log(html)


const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html, {waitUntil: 'networkidle0'})
const pdfBuffer = await page.pdf({format: 'A4'})
await browser.close();
const response = new NextResponse(pdfBuffer);

response.headers.set("Content-Type", "application/pdf")
console.log("render complete")

return response;

}catch (e) {
console.log(e)
return new NextResponse(
JSON.stringify({ status: 'error', message: 'Failed to fetch story' }),
{ status: 500 }
);
}


}
Loading

0 comments on commit 7563b1c

Please sign in to comment.