api entities

This commit is contained in:
2022-10-11 12:18:12 +02:00
parent 63478aa887
commit 8866784d49
19 changed files with 106 additions and 21 deletions

View File

@ -2,14 +2,13 @@ import { MongoClient, Db } from 'mongodb';
class Mongo {
private client: MongoClient;
private dbName: string;
constructor(uri: string) {
this.client = new MongoClient(uri);
}
getDb(): Db {
return this.client.db(this.dbName);
getDb(dbName: string): Db {
return this.client.db(dbName);
}
}

View File

@ -1,16 +1,28 @@
import { CreateUserBody, User, UserInfo } from '@core';
import { User } from '../../entities/user';
import { Services } from '../express/server';
export function Create(
services: Services,
): (tracker: string, user: CreateUserBody) => Promise<UserInfo> {
): (tracker: string, user: User) => Promise<User> {
const { db } = services;
const coll = db.collection('users');
const coll = db.collection<User>('users');
const readUser = Read(services);
return async (tracker, user) => {
await coll.insertOne(user);
return {
name: 'test',
};
return readUser(tracker, user.uuid);
};
}
export function Read(
services: Services,
): (tracker: string, uuid: string) => Promise<User> {
const { db } = services;
const coll = db.collection<User>('users');
return async (tracker, uuid) => {
const user = await coll.findOne({ uuid });
return new User(user || { name: 'not found' });
};
}