Add resource structure and implement into User structure
This commit is contained in:
parent
9c99b27547
commit
41bf8d80dc
|
@ -1,5 +1,6 @@
|
|||
import { Users } from './mongodb';
|
||||
import type User from '../types/User';
|
||||
import type Resources from '../types/Resources';
|
||||
|
||||
export const getAllUsers = async () => {
|
||||
const users = await Users();
|
||||
|
@ -19,4 +20,26 @@ export const getUserByNickOrEmail = async (searchString: string) => {
|
|||
{ email: searchString }
|
||||
]
|
||||
}) 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
|
||||
}
|
||||
});
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
export default interface Resources {
|
||||
coal: number;
|
||||
iron: number;
|
||||
gold: number;
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import type { ObjectId } from "mongodb";
|
||||
import type Resources from "./Resources";
|
||||
|
||||
export default interface User {
|
||||
_id?: ObjectId;
|
||||
|
@ -6,6 +7,7 @@ export default interface User {
|
|||
email: string;
|
||||
password: string;
|
||||
lastLogin: Date;
|
||||
resources: Resources;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
Loading…
Reference in New Issue