Skip to content

Commit

Permalink
refactor: use query instead of service (typo)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Jun 2, 2024
1 parent 6ad7d2d commit da821dc
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main
workflow_dispatch:
push:
branches:
- main

jobs:
build-app:
Expand Down
6 changes: 3 additions & 3 deletions src/app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Flex, Heading } from "@radix-ui/themes";
import { LoginForm } from "./components/loginForm/loginForm";
import { redirect } from "next/navigation";
import AuthService from "@/server/services/auth/auth.service";
import AuthQueries from "@/server/queries/auth/auth.queries";

export default async function LoginPage() {
const authService = new AuthService();
const authQueries = new AuthQueries();

if (await authService.doSignUp()) redirect("/signup");
if (await authQueries.doSignUp()) redirect("/signup");

return (
<Flex className="flex-col gap-10 p-10 text-center h-screen">
Expand Down
6 changes: 3 additions & 3 deletions src/app/(auth)/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Flex, Heading } from "@radix-ui/themes";
import { redirect } from "next/navigation";
import { SignupForm } from "./components/signupForm";
import AuthService from "@/server/services/auth/auth.service";
import AuthQueries from "@/server/queries/auth/auth.queries";

export default async function LoginPage() {
const authService = new AuthService();
const authQueries = new AuthQueries();

if (!(await authService.doSignUp())) redirect("/login");
if (!(await authQueries.doSignUp())) redirect("/login");

return (
<Flex className="flex-col gap-10 p-10 text-center h-screen">
Expand Down
6 changes: 3 additions & 3 deletions src/app/actions/change-shell-password-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { logger } from "@/lib/logger";
import { action } from "@/lib/safe-action";
import { z } from "zod";
import { shellSchema } from "@/schemas/shellSchema";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

const schema = z.object({
shell: shellSchema,
Expand All @@ -24,9 +24,9 @@ export const changeShellPasswordAction = action(
).changePassword();

if (success) {
const shellService = new ShellService();
const shellQueries = new ShellQueries();
logger.info("Password changed!");
await shellService.changeShellPassword(shell);
await shellQueries.changeShellPassword(shell);
revalidatePath("/", "layout");
return { success: true };
}
Expand Down
10 changes: 5 additions & 5 deletions src/app/actions/create-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PortHelper from "@/lib/helpers/port.helper";
import ContainerHelper from "@/lib/helpers/container.helper";
import { z } from "zod";
import { action } from "@/lib/safe-action";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

const schema = z.object({
name: z.string(),
Expand All @@ -19,17 +19,17 @@ const schema = z.object({
export const createShellAction = action(
schema,
async ({ name, distro, extraArgs }): Promise<OperationResult> => {
const shellService = new ShellService();
const shellQueries = new ShellQueries();

logger.info(
`Creating shell with name ${name}, distro ${distro}, extra arguments ${extraArgs}...`
);

if (await shellService.checkIfShellExists(name)) {
if (await shellQueries.checkIfShellExists(name)) {
return { success: false, shellExists: true };
}

const port = await PortHelper.getAvailablePort();
const port = await new PortHelper().getAvailablePort();

const data = {
id: 0,
Expand All @@ -47,7 +47,7 @@ export const createShellAction = action(

if (success) {
logger.info("Server ready!");
await shellService.addShell(data);
await shellQueries.addShell(data);
revalidatePath("/", "layout");
return { success: true };
}
Expand Down
16 changes: 5 additions & 11 deletions src/app/actions/login-action.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use server";

import { getConfig } from "@/config/config";
import { getSession } from "@/lib/helpers/session.helper";
import { action } from "@/lib/safe-action";
import AuthService from "@/server/services/auth/auth.service";
import AuthQueries from "@/server/queries/auth/auth.queries";
import { compare } from "bcrypt-ts";
import { redirect } from "next/navigation";
import { z } from "zod";
Expand All @@ -14,19 +13,14 @@ const schema = z.object({
});

export const loginAction = action(schema, async ({ username, password }) => {
const user = {
username: getConfig().username,
password: getConfig().password,
};

const session = await getSession();
const authService = new AuthService();
const authQueries = new AuthQueries();

if (await authService.checkIfUserExists(username)) {
if (await authQueries.checkIfUserExists(username)) {
if (
await compare(password, (await authService.getUser(username)).password)
await compare(password, (await authQueries.getUser(username)).password)
) {
session.username = user.username;
session.username = username;
session.isLoggedIn = true;
await session.save();
redirect("/");
Expand Down
10 changes: 5 additions & 5 deletions src/app/actions/remove-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { revalidatePath } from "next/cache";
import ContainerHelper from "@/lib/helpers/container.helper";
import { z } from "zod";
import { action } from "@/lib/safe-action";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

const schema = z.object({
id: z.number(),
Expand All @@ -15,9 +15,9 @@ const schema = z.object({
export const removeShellAction = action(
schema,
async ({ id }): Promise<OperationResult> => {
const shellService = new ShellService();
const shellQueries = new ShellQueries();

const shell = await shellService.getShellFromId(id);
const shell = await shellQueries.getShellFromId(id);

if (!shell) {
logger.info(
Expand All @@ -29,14 +29,14 @@ export const removeShellAction = action(

if (remove.success) {
logger.info("Container killed! Removing from db...");
await shellService.deleteShell(id);
await shellQueries.deleteShell(id);
revalidatePath("/", "layout");
return { success: true };
}
logger.warn(
`Cannot remove container, still removing from db, error: ${remove.error}`
);
await shellService.deleteShell(id);
await shellQueries.deleteShell(id);
revalidatePath("/", "layout");
return { success: false };
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/actions/signup-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { getSession } from "@/lib/helpers/session.helper";
import { action } from "@/lib/safe-action";
import AuthService from "@/server/services/auth/auth.service";
import AuthQueries from "@/server/queries/auth/auth.queries";
import { genSalt, hash } from "bcrypt-ts";
import { redirect } from "next/navigation";
import { z } from "zod";
Expand All @@ -13,12 +13,12 @@ const schema = z.object({
});

export const signupAction = action(schema, async ({ username, password }) => {
const authService = new AuthService();
const authQueries = new AuthQueries();
const session = await getSession();
const salt = await genSalt(10);
const hashedPassword = await hash(password, salt);

await authService.addUser(username, hashedPassword);
await authQueries.addUser(username, hashedPassword);

session.username = username;
session.isLoggedIn = true;
Expand Down
6 changes: 3 additions & 3 deletions src/app/actions/start-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ContainerHelper from "@/lib/helpers/container.helper";
import { shellSchema } from "@/schemas/shellSchema";
import { z } from "zod";
import { action } from "@/lib/safe-action";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

const schema = z.object({
shell: shellSchema,
Expand All @@ -22,10 +22,10 @@ export const startShellAction = action(
).startContainer();

if (success) {
const shellService = new ShellService();
const shellQueries = new ShellQueries();
logger.info("Shell started!");
revalidatePath("/", "layout");
await shellService.changeShellRunningStatus(shell);
await shellQueries.changeShellRunningStatus(shell);
return { success: true };
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/actions/stop-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ContainerHelper from "@/lib/helpers/container.helper";
import { z } from "zod";
import { action } from "@/lib/safe-action";
import { shellSchema } from "@/schemas/shellSchema";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

const schema = z.object({
shell: shellSchema,
Expand All @@ -20,10 +20,10 @@ export const stopShellAction = action(
const { success, error } = await new ContainerHelper(shell).stopContainer();

if (success) {
const shellService = new ShellService();
const shellQueries = new ShellQueries();
logger.info("Shell stopped!");
revalidatePath("/", "layout");
await shellService.changeShellRunningStatus(shell);
await shellQueries.changeShellRunningStatus(shell);
return { success: true };
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import distroHelper from "@/lib/helpers/distro.helper";
import { redirect } from "next/navigation";
import { LogoutButton } from "./components/logout-button";
import { getSession } from "@/lib/helpers/session.helper";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

export default async function Home() {
const session = await getSession();
const shellService = new ShellService();
const shellQueries = new ShellQueries();

if (!session.isLoggedIn) redirect("/login");

const shellData = await shellService.getShells();
const shellData = await shellQueries.getShells();
const availableDistos = new distroHelper().getDistros();

return (
Expand Down
6 changes: 3 additions & 3 deletions src/lib/helpers/port.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { exec as execCallback } from "child_process";
import * as util from "util";
import { getConfig } from "../../config/config";
import { logger } from "../logger";
import ShellService from "@/server/services/shell/shell.service";
import ShellQueries from "@/server/queries/shell/shell.queries";

export default class PortHelper {
private exec = util.promisify(execCallback);

public getAvailablePort = async (): Promise<number> => {
let port: number | undefined;
const shellService = new ShellService();
const ShellQueries = new ShellQueries();

do {
if (port)
Expand All @@ -20,7 +20,7 @@ export default class PortHelper {
);
} while (
!(await this.checkPortIsAvailable(port)).success ||
!shellService.isPortAvailable(port)
!ShellQueries.isPortAvailable(port)
);
logger.info(`Success! Using port ${port}`);
return port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { eq } from "drizzle-orm";
import { db } from "../../db/db";
import { users } from "../../db/schema";
import Database from "better-sqlite3";
import { userData } from "@/types/types";
import { userData } from "../../../types/types";

export default class AuthService {
export default class AuthQueries {
public doSignUp = async (): Promise<boolean> => {
return (await db.select().from(users)).length === 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { shells } from "../../db/schema";
import { ContainerData } from "../../../types/types";
import Database from "better-sqlite3";

export default class ShellService {
export default class ShellQueries {
public isPortAvailable = async (port: number): Promise<boolean> => {
return (
(await db.select().from(shells).where(eq(shells.port, port))).length === 0
Expand Down

0 comments on commit da821dc

Please sign in to comment.