Add resource structure and implement into User structure

This commit is contained in:
Aelita4 2023-11-11 23:20:47 +01:00
parent 9c99b27547
commit 41bf8d80dc
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
3 changed files with 30 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import { Users } from './mongodb'; import { Users } from './mongodb';
import type User from '../types/User'; import type User from '../types/User';
import type Resources from '../types/Resources';
export const getAllUsers = async () => { export const getAllUsers = async () => {
const users = await Users(); const users = await Users();
@ -19,4 +20,26 @@ export const getUserByNickOrEmail = async (searchString: string) => {
{ email: searchString } { email: searchString }
] ]
}) as Promise<User | null>; }) as Promise<User | null>;
}
export const getUserResources = async (username: string): Promise<Resources> => {
const users = await Users();
const user = await users.findOne({ username });
const defaultResources: Resources = {
coal: 0,
iron: 0,
gold: 0
};
return user?.resources === undefined ? defaultResources : user?.resources;
}
export const updateUserResources = async (username: string, resources: any) => {
const users = await Users();
await users.updateOne({ username }, {
$set: {
resources
}
});
} }

5
src/types/Resources.ts Normal file
View File

@ -0,0 +1,5 @@
export default interface Resources {
coal: number;
iron: number;
gold: number;
}

View File

@ -1,4 +1,5 @@
import type { ObjectId } from "mongodb"; import type { ObjectId } from "mongodb";
import type Resources from "./Resources";
export default interface User { export default interface User {
_id?: ObjectId; _id?: ObjectId;
@ -6,6 +7,7 @@ export default interface User {
email: string; email: string;
password: string; password: string;
lastLogin: Date; lastLogin: Date;
resources: Resources;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
} }