feat: Introduce new modules for class, inventory, leveling, and quests with expanded schema, refactor user service, and add verification scripts.
This commit is contained in:
44
src/modules/user/user.service.ts
Normal file
44
src/modules/user/user.service.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { users } from "@/db/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
|
||||
export const userService = {
|
||||
getUserById: async (id: string) => {
|
||||
const user = await DrizzleClient.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id)),
|
||||
with: { class: true }
|
||||
});
|
||||
return user;
|
||||
},
|
||||
getUserByUsername: async (username: string) => {
|
||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
|
||||
return user;
|
||||
},
|
||||
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [user] = await txFn.insert(users).values({
|
||||
id: BigInt(id),
|
||||
username,
|
||||
classId,
|
||||
}).returning();
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
updateUser: async (id: string, data: Partial<typeof users.$inferInsert>, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [user] = await txFn.update(users)
|
||||
.set(data)
|
||||
.where(eq(users.id, BigInt(id)))
|
||||
.returning();
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
deleteUser: async (id: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
await txFn.delete(users).where(eq(users.id, BigInt(id)));
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user