Refactoring 2
This commit is contained in:
parent
dd14dfe72e
commit
e4b78ceec2
76 changed files with 870 additions and 734 deletions
|
|
@ -1,117 +0,0 @@
|
|||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { db, Database } from "@/app/_lib/database";
|
||||
import { FieldDescription, Entity } from "@/app/_lib/service";
|
||||
import crypto from "crypto";
|
||||
|
||||
const generateToken = () => {
|
||||
const length = 20;
|
||||
const randomBytes = crypto.randomBytes(length);
|
||||
const randomString = randomBytes.toString("hex").slice(0, length);
|
||||
|
||||
return randomString;
|
||||
};
|
||||
|
||||
type CreateActionArgs = {
|
||||
entity: Entity;
|
||||
table: keyof Database;
|
||||
fields: FieldDescription[];
|
||||
currentState: any;
|
||||
formData: FormData;
|
||||
};
|
||||
|
||||
export const createAction = async ({
|
||||
entity,
|
||||
table,
|
||||
fields,
|
||||
currentState,
|
||||
formData,
|
||||
}: CreateActionArgs) => {
|
||||
console.log(formData);
|
||||
const newRecord = fields.reduce(
|
||||
(acc: Record<string, any>, field: FieldDescription) => {
|
||||
if (field.autogenerated === "token") {
|
||||
acc[field.name] = generateToken();
|
||||
return acc;
|
||||
}
|
||||
|
||||
acc[field.name] = formData.get(field.name)?.toString() ?? null;
|
||||
return acc;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
console.log({ newRecord });
|
||||
const record = await db
|
||||
.insertInto(table)
|
||||
.values(newRecord)
|
||||
.returning(["id"])
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
console.log({ record });
|
||||
revalidatePath(`/${entity}`);
|
||||
|
||||
return {
|
||||
...currentState,
|
||||
values: { ...newRecord, id: record.id },
|
||||
success: true,
|
||||
};
|
||||
};
|
||||
|
||||
type UpdateActionArgs = {
|
||||
entity: Entity;
|
||||
table: keyof Database;
|
||||
fields: FieldDescription[];
|
||||
currentState: any;
|
||||
formData: FormData;
|
||||
};
|
||||
|
||||
export const updateAction = async ({
|
||||
entity,
|
||||
table,
|
||||
fields,
|
||||
currentState,
|
||||
formData,
|
||||
}: UpdateActionArgs) => {
|
||||
const id = currentState.values.id;
|
||||
const updatedRecord = fields.reduce(
|
||||
(acc: Record<string, any>, field: FieldDescription) => {
|
||||
acc[field.name] = formData.get(field.name)?.toString() ?? null;
|
||||
return acc;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
await db
|
||||
.updateTable(table)
|
||||
.set(updatedRecord)
|
||||
.where("id", "=", id)
|
||||
.executeTakeFirst();
|
||||
|
||||
revalidatePath(`/${entity}/${id}`);
|
||||
|
||||
return {
|
||||
...currentState,
|
||||
values: updatedRecord,
|
||||
success: true,
|
||||
};
|
||||
};
|
||||
|
||||
type DeleteActionArgs = {
|
||||
entity: Entity;
|
||||
table: keyof Database;
|
||||
id: string;
|
||||
};
|
||||
|
||||
export const deleteAction = async ({ entity, table, id }: DeleteActionArgs) => {
|
||||
await db.deleteFrom(table).where("id", "=", id).execute();
|
||||
|
||||
revalidatePath(`/${entity}`);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
export const selectAllAction = async (table: keyof Database) => {
|
||||
return db.selectFrom(table).selectAll().execute();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue