Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 33e5e6e
Merge: f14e787 2b36977
Author: spicyzboss <supachai@spicyz.io>
Date:   Fri Mar 29 06:45:11 2024 +0700

    Merge branch 'finish-api'

commit 2b36977
Author: spicyzboss <supachai@spicyz.io>
Date:   Fri Mar 29 06:41:41 2024 +0700

    feat: placeholder patcher

commit f14e787
Author: Santhita Krajangwongpaisan <74261236+santhitak@users.noreply.github.com>
Date:   Fri Mar 29 05:16:10 2024 +0700

    tab template (#63)

    * feat(web): Add privacy and terms page

    * feat: Add template tab

    * feat: Navigate to template

    * feat: Navigate to create template

    * fix: remove orphan page

    * fix: remove orphan page

    ---------

    Co-authored-by: SoNicBoom24 <boomzahrk@gmail.com>
  • Loading branch information
santhitak committed Mar 29, 2024
1 parent d496b5b commit fa4f762
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 43 deletions.
172 changes: 138 additions & 34 deletions apps/api/auth/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,24 @@ interface Template {
createdAt: number;
updatedAt: number;
deletedAt: number;
}
};

type CreateTemplateParams = Pick<Template, 'name' | 'userId'>;
type CreateTemplateParams = Pick<Template,
| 'name'
| 'userId'
>;

type FindTemplateParams = Pick<Template, 'userId'>;
// type FindTemplateParams = Pick<Template,
// | 'userId'
// >;

interface TemplateDataParams {
data: JSON;
}
};

type UpdateTemplateParams = Pick<Template, 'name'>;
type UpdateTemplateParams = Pick<Template,
| 'name'
>;

type CreateTemplateRequest = CreateTemplateParams & TemplateDataParams;
type UpdateTemplateRequest = UpdateTemplateParams & TemplateDataParams;
Expand Down Expand Up @@ -194,53 +201,49 @@ app.post('/templates', async (c) => {
}
});

app.get('/templates/:userId', async (c) => {
const { userId } = c.req.param() as FindTemplateParams;
app.get('/templates', async (c) => {
const { userId } = c.req.query();

const getTemplateStatement = c.env.DB.prepare(`
SELECT
t.id,
t.user_id 'userId',
t.key,
t.name,
t.created_at 'createdAt',
t.updated_at 'updatedAt'
FROM template as t
WHERE t.user_id = ?1 AND t.deleted_at NOT NULL
WHERE t.user_id = ?1 AND t.deleted_at IS NULL
`);

const { results } = await getTemplateStatement.bind(userId).all<Template>();

results.map((template) => {
template.id;
});

return c.json(response(results));
});

app.get('/templates/:userId/:id', async (c) => {
const { userId, id } = c.req.param();
app.get('/templates/data', async (c) => {
const { userId, id } = c.req.query();
const key = ['templates', userId, id].join('/');

try {
const objectRef = await c.env.BUCKET.get(key);
const data = objectRef.json();
const data = await objectRef.json();

return c.json(response(data));
} catch (e) {
return c.json(response(null, e));
}
});

app.put('/templates/:userId/:id', async (c) => {
const { userId, id } = c.req.param();
const { data, name } = (await c.req.json()) as UpdateTemplateRequest;
app.put('/templates/data', async (c) => {
const { userId, id } = c.req.query();
const { data, name } = await c.req.json() as UpdateTemplateRequest;

const key = ['templates', userId, id].join('/');

const updateTemplateStatement = c.env.DB.prepare(`
UPDATE template
SET name = ?1
WHERE id = ?2 AND deleted_at NOT NULL
WHERE id = ?2 AND deleted_at IS NULL
`);

try {
Expand All @@ -262,13 +265,22 @@ interface Project {
createdAt: number;
updatedAt: number;
deletedAt: number;
}
};

type CreateProjectRequest = Pick<Project, 'userId' | 'templateId' | 'name'>;
type CreateProjectRequest = Pick<Project,
| 'userId'
| 'templateId'
| 'name'
>;

type FindProjectRequest = Pick<Project, 'userId'>;
type FindProjectRequest = Pick<Project,
| 'userId'
>;

type UpdateProjectRequest = Pick<Project, 'id' | 'name'>;
type UpdateProjectRequest = Pick<Project,
| 'id'
| 'name'
>;

app.post('/projects', async (c) => {
const body = await c.req.json<CreateProjectRequest>();
Expand All @@ -293,10 +305,11 @@ app.get('/projects', async (c) => {
p.id,
p.user_id 'userId',
p.template_id 'templateId',
p.name,
p.created_at 'createdAt',
p.updated_at 'updatedAt'
FROM project as p
WHERE p.user_id = ?1 AND p.deleted_at NOT NULL
WHERE p.user_id = ?1 AND p.deleted_at IS NULL
`);

const { results } = await getProjectStatement.bind(userId).all<Project>();
Expand All @@ -310,21 +323,112 @@ app.patch('/projects', async (c) => {
const updateProjectStatement = c.env.DB.prepare(`
UPDATE project
SET name = ?1
WHERE id = ?2 AND deleted_at NOT NULL
WHERE id = ?2 AND deleted_at IS NULL
`);

const { results } = await updateProjectStatement.bind(body.name, body.id).run();

return c.json(response(results));
});

// interface Generate {
// id: string;
// projectId: string;
// key: string;
// args: string;
// createdAt: number;
// deletedAt: number;
// }
interface Generate {
id: string;
projectId: string;
args: string;
createdAt: number;
deletedAt: number;
};

type CreateGenerateParams = Pick<Generate,
| 'projectId'
| 'args'
>;

app.post('/generates', async (c) => {
const body = await c.req.json<CreateGenerateParams>();

const createGenerateStatement = c.env.DB.prepare(`
INSERT INTO generate (id, project_id, args, created_at)
VALUES (?1, ?2, ?3, ?4)
`);

const getTemplateStatement = c.env.DB.prepare(`
SELECT
t.id,
t.user_id 'userId',
t.name,
p.user_id,
t.created_at 'createdAt',
t.updated_at 'updatedAt'
FROM template as t
JOIN project as p
ON (p.template_id = t.id)
WHERE p.id = ?1 AND t.deleted_at IS NULL
`);

const templateMetadata = await getTemplateStatement.bind(body.projectId).first<Template>();
const templateKey = ['templates', templateMetadata.userId, templateMetadata.id].join('/');
const templateRef = await c.env.BUCKET.get(templateKey);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const template = await templateRef.json<Record<string, any>>();

const args = Object.fromEntries([...new URLSearchParams(body.args)]);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
template.objects = template.objects.map((object: Record<string, any>) => {
if (!object.text) return object;

Object.keys(args).map((key) => {
object.text = object.text.replace(`{${key}}`, args[key]);
});

return object;
});

const id = typeid('gen').toString();
const generatesKey = ['generates', body.projectId, id].join('/');

await c.env.BUCKET.put(generatesKey, JSON.stringify(template));

const result = createGenerateStatement.bind(id, body.projectId, body.args, Date.now()).run();

return c.json(response(result));
});

app.get('/generates', async (c) => {
const { projectId } = c.req.query();

const getGenerateStatement = c.env.DB.prepare(`
SELECT
g.id,
g.project_id 'projectId',
g.args,
g.created_at 'createdAt'
FROM generate as g
WHERE g.project_id = ?1 AND g.deleted_at IS NULL
`);

const { results } = await getGenerateStatement.bind(projectId).all<Generate>();

return c.json(response(results));
});

app.get('/generates/data', async (c) => {
const { projectId, id } = c.req.query();

const key = ['generates', projectId, id].join('/');

const objectRef = await c.env.BUCKET.get(key);
const file = await objectRef.json();

return c.json(response(file));
// const fileBuffer = await objectRef.arrayBuffer();
// const fileSlices = new Uint8Array(fileBuffer);

// return stream(c, async (stream) => {
// await stream.write(fileSlices);
// await stream.close();
// });
});

export default app;
18 changes: 9 additions & 9 deletions apps/web/src/routes/api/auth/google/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestHandler } from '@builder.io/qwik-city';
import { google } from 'apps/web/src/configs/auth';
import { google } from '../../../../configs/auth';
import { generateCodeVerifier, generateState } from 'arctic';

export const onGet: RequestHandler = async ({ redirect, cookie }) => {
Expand All @@ -9,21 +9,21 @@ export const onGet: RequestHandler = async ({ redirect, cookie }) => {
scopes: ['profile', 'email'],
});

cookie.set("google_oauth_state", state, {
path: "/",
cookie.set('google_oauth_state', state, {
path: '/',
secure: import.meta.env.PROD,
httpOnly: true,
maxAge: [10, 'minutes'],
sameSite: "lax"
})
sameSite: 'lax'
});

cookie.set("google_oauth_code_verifier", codeVerifier, {
path: "/",
cookie.set('google_oauth_code_verifier', codeVerifier, {
path: '/',
secure: import.meta.env.PROD,
httpOnly: true,
maxAge: [10, 'minutes'],
sameSite: "lax"
})
sameSite: 'lax'
});

throw redirect(302, url.toString());
};
3 changes: 3 additions & 0 deletions db/schema/drop.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
DROP TABLE generate;
DROP TABLE project;
DROP TABLE template;
DROP TABLE oauth_account;
DROP TABLE oauth_provider;
DROP TABLE session;
Expand Down

0 comments on commit fa4f762

Please sign in to comment.