Skip to content

Commit

Permalink
chore(structure): Refactor APIs structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzuan committed Nov 17, 2024
1 parent 18aeb49 commit 4fa899d
Show file tree
Hide file tree
Showing 60 changed files with 1,281 additions and 1,070 deletions.
31 changes: 31 additions & 0 deletions src/api/controller/ai/getAIChatById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { authGuard } from "@libs/authGuard";
import { createElysia } from "@libs/elysia";
import { prismaClient } from "@libs/prismaDatabase";

export default createElysia()
.use(authGuard)
.get(
"/:id",
async ({ params: { id }, user }) => {
const aiChat = await prismaClient.aIChat.findUnique({
where: {
id,
userId: user.id,
},
select: {
chatTitle: true,
messages: true,
},
});

return {
status: 200,
data: aiChat,
};
},
{
detail: {
tags: ["Tidy AI"],
},
}
);
32 changes: 32 additions & 0 deletions src/api/controller/ai/getCurrentAIChat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { authGuard } from "@libs/authGuard";
import { createElysia } from "@libs/elysia";
import { prismaClient } from "@libs/prismaDatabase";

export default createElysia()
.use(authGuard)
.get(
"/",
async ({ user }) => {
const aiChats = await prismaClient.aIChat.findMany({
where: {
userId: user.id,
},
select: {
userId: false,
chatTitle: true,
id: true,
createdAt: true,
},
});

return {
status: 200,
data: aiChats,
};
},
{
detail: {
tags: ["Tidy AI"],
},
}
);
5 changes: 5 additions & 0 deletions src/api/controller/ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import getAIChatById from "./getAIChatById";
import getCurrentAIChat from "./getCurrentAIChat";
import requestAIChat from "./requestAIChat";

export { getAIChatById, getCurrentAIChat, requestAIChat };
Original file line number Diff line number Diff line change
@@ -1,69 +1,12 @@
import { t } from "elysia";

import { createElysia } from "@libs/elysia";
import { authGuard } from "@libs/authGuard";
import { ChatCompletionResponse, Message } from "@t/ai.types";
import { createElysia } from "@libs/elysia";
import { prismaClient } from "@libs/prismaDatabase";
import { ChatCompletionResponse, Message } from "@t/ai";
import aiModel from "@models/ai.model";

export const AIController = createElysia()
.model({
"ai.req.model": t.Object({
msg: t.String(),
aiChatId: t.Optional(t.String()),
}),
})
export default createElysia()
.use(aiModel)
.use(authGuard)
.get(
"/",
async ({ user }) => {
const aiChats = await prismaClient.aIChat.findMany({
where: {
userId: user.id,
},
select: {
userId: false,
chatTitle: true,
id: true,
createdAt: true,
},
});

return {
status: 200,
data: aiChats,
};
},
{
detail: {
tags: ["AI"],
},
}
)
.get(
"/:id",
async ({ params: { id }, user }) => {
const aiChat = await prismaClient.aIChat.findUnique({
where: {
id,
userId: user.id,
},
select: {
chatTitle: true,
messages: true,
},
});

return {
status: 200,
data: aiChat,
};
},
{
detail: {
tags: ["AI"],
},
}
)
.post(
"/",
async ({ body, user, env }) => {
Expand Down Expand Up @@ -158,7 +101,7 @@ export const AIController = createElysia()
{
body: "ai.req.model",
detail: {
tags: ["AI"],
tags: ["Tidy AI"],
},
}
);
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { t } from "elysia";

import { authGuard } from "@libs/authGuard";
import { cloudinary } from "@libs/cloudinary";
import { createElysia } from "@libs/elysia";
import cloudinaryModel from "@models/cloudinary.model";

export const CloudinaryController = createElysia()
export default createElysia()
.use(cloudinaryModel)
.use(authGuard)
.post(
"/upload",
Expand All @@ -20,9 +20,7 @@ export const CloudinaryController = createElysia()
};
},
{
body: t.Object({
image: t.String(),
}),
body: "cloudinary.model",
tags: ["Assets", "Cloudinary"],
}
);
3 changes: 3 additions & 0 deletions src/api/controller/assets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cloudinaryUpload from "./cloudinaryUpload";

export { cloudinaryUpload };
20 changes: 6 additions & 14 deletions src/api/controller/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { createElysia } from "@libs/elysia";
import { login } from "./login";
import { signup } from "./signup";
import { logout } from "./logout";
import { provider } from "./provider";
import { providerCallback } from "./providerCallback";
import login from "./login";
import signup from "./signup";
import logout from "./logout";
import provider from "./provider";
import providerCallback from "./providerCallback";

const auth = createElysia()
.use(provider)
.use(providerCallback)
.use(login)
.use(signup)
.use(logout);

export { auth };
export { login, signup, logout, provider, providerCallback };
117 changes: 57 additions & 60 deletions src/api/controller/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,69 @@ import { createElysia } from "@libs/elysia";
import { lucia } from "@libs/luciaAuth";
import { prismaClient } from "@libs/prismaDatabase";
import { password as bunPassword } from "bun";
import { t } from "elysia";
import authModel from "@models/auth.model";

const login = createElysia().post(
"/login",
async ({
body: { email, password },
cookie,
set,
log,
env: { DOMAIN, PASSWORD_PEPPER },
}) => {
const user = await prismaClient.user.findUnique({
where: {
email,
},
});
export default createElysia()
.use(authModel)
.post(
"/login",
async ({
body: { email, password },
cookie,
set,
log,
env: { DOMAIN, PASSWORD_PEPPER },
}) => {
const user = await prismaClient.user.findUnique({
where: {
email,
},
});

if (!user || !user.passwordSalt || !user.hashedPassword) {
log.error("User not found.");
throw new BadRequestException("User not found.");
}
if (!user || !user.passwordSalt || !user.hashedPassword) {
log.error("User not found.");
throw new BadRequestException("User not found.");
}

const passwordPepper = PASSWORD_PEPPER;
const passwordPepper = PASSWORD_PEPPER;

if (!passwordPepper) {
log.error("Password pepper is not set.");
throw new Error("Password pepper is not set.");
}
if (!passwordPepper) {
log.error("Password pepper is not set.");
throw new Error("Password pepper is not set.");
}

if (!user || !user.passwordSalt || !user.hashedPassword) {
log.error("User data is missing or incomplete.");
} else if (
!(await bunPassword.verify(
user.passwordSalt + password + passwordPepper,
user.hashedPassword
))
) {
log.error("Password is invalid.");
throw new BadRequestException("Password is invalid.");
} else {
try {
const session = await lucia.createSession(user.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);
if (!user || !user.passwordSalt || !user.hashedPassword) {
log.error("User data is missing or incomplete.");
} else if (
!(await bunPassword.verify(
user.passwordSalt + password + passwordPepper,
user.hashedPassword
))
) {
log.error("Password is invalid.");
throw new BadRequestException("Password is invalid.");
} else {
try {
const session = await lucia.createSession(user.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);

set.status = 200;
cookie[sessionCookie.name]?.set({
value: sessionCookie.value,
domain: DOMAIN,
...sessionCookie.attributes,
});
set.status = 200;
cookie[sessionCookie.name]?.set({
value: sessionCookie.value,
domain: DOMAIN,
...sessionCookie.attributes,
});

return user;
} catch (error) {
set.status = 500;
return user;
} catch (error) {
set.status = 500;
}
}
}
},
{
detail: {
tags: ["Authorization Service"],
},
body: t.Object({
email: t.String(),
password: t.String(),
}),
}
);

export { login };
{
detail: {
tags: ["Authorization Service"],
},
body: "auth.login",
}
);
4 changes: 1 addition & 3 deletions src/api/controller/auth/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BadRequestException } from "@constants/exceptions";
import { createElysia } from "@libs/elysia";
import { lucia } from "@libs/luciaAuth";

const logout = createElysia().post(
export default createElysia().post(
"/logout",
async ({ cookie, env: { DOMAIN } }) => {
const sessionCookie = cookie[lucia.sessionCookieName];
Expand All @@ -25,5 +25,3 @@ const logout = createElysia().post(
},
}
);

export { logout };
9 changes: 5 additions & 4 deletions src/api/controller/auth/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { genAuthUrl } from "@utils/authUtils";
import { generateCodeVerifier, generateState } from "arctic";
import { t } from "elysia";

const provider = createElysia().get(
export default createElysia().get(
"/:provider",
async ({
params: { provider },
Expand All @@ -15,7 +15,7 @@ const provider = createElysia().get(
const state = generateState();
const codeVerifier = generateCodeVerifier();

const redirectUrl = await genAuthUrl(provider, state, codeVerifier);
const redirectUrl = genAuthUrl(provider, state, codeVerifier);

oauth_state?.set({
value: state,
Expand Down Expand Up @@ -47,6 +47,9 @@ const provider = createElysia().get(
set.redirect = redirectUrl.toString();
},
{
detail: {
tags: ["Authorization Service"],
},
query: t.Object({
next: t.Optional(t.String()),
}),
Expand All @@ -59,5 +62,3 @@ const provider = createElysia().get(
}),
}
);

export { provider };
Loading

0 comments on commit 4fa899d

Please sign in to comment.