new services + new scripts

This commit is contained in:
2022-10-14 18:49:09 +02:00
parent 8866784d49
commit 5538a042df
23 changed files with 200 additions and 126 deletions

View File

@ -1,15 +1,21 @@
import Server, { Services } from './framework/express/server';
import Mongo from './framework/mongo/mongo';
import Server from './framework/express/server';
import ip from 'ip';
import UserModel from './framework/mongo/user';
import { MongoClient } from 'mongodb';
const PORT = 8000;
const MONGOURI = 'mongodb://localhost:27017';
const DBNAME = 'dev';
const mongo = new Mongo(MONGOURI);
export type Services = {
userModel: UserModel;
};
const mongo = new MongoClient(MONGOURI);
const db = mongo.db(DBNAME);
const services: Services = {
db: mongo.getDb(DBNAME),
userModel: new UserModel(db),
};
const server = new Server(services);

View File

@ -1,4 +1,10 @@
import { UserCtor, UserInfo } from '@core';
import {
UserCtor,
UserInfo,
UserInfoWithPassword,
UserWithPasswordCtor,
} from '@core';
import { createHash, Hash } from 'crypto';
import { Entity } from './entity';
export class User extends Entity implements UserInfo {
@ -12,7 +18,18 @@ export class User extends Entity implements UserInfo {
Info(): UserInfo {
return {
uuid: this.uuid,
name: this.name,
};
}
}
export class UserWithPassword extends User implements UserInfoWithPassword {
password: Hash;
constructor(raw: UserWithPasswordCtor) {
super(raw);
this.password = createHash('sha256').update(`${this.uuid}+${raw.password}`);
}
}

View File

@ -1,5 +1,5 @@
import { Router } from 'express';
import { Services } from './server';
import { Services } from '../../app';
import * as user from './user';
export function getRoutes(services: Services) {

View File

@ -2,11 +2,7 @@ import express, { Express } from 'express';
import cors from 'cors';
import * as router from './router';
import { BeforeEach } from './middleware';
import { Db } from 'mongodb';
export type Services = {
db: Db;
};
import { Services } from '../../app';
class Server {
private app: Express;

View File

@ -1,7 +1,7 @@
import { RequestHandler, Router } from 'express';
import { Services } from '../../app';
import { Create } from '../../functions/user';
import { getId } from './middleware';
import { Services } from './server';
export function LoginHandler(services: Services): RequestHandler {
return async (req, res) => {

View File

@ -1,15 +0,0 @@
import { MongoClient, Db } from 'mongodb';
class Mongo {
private client: MongoClient;
constructor(uri: string) {
this.client = new MongoClient(uri);
}
getDb(dbName: string): Db {
return this.client.db(dbName);
}
}
export default Mongo;

View File

@ -1,28 +1,22 @@
import { Collection, Db } from 'mongodb';
import { User } from '../../entities/user';
import { Services } from '../express/server';
export function Create(
services: Services,
): (tracker: string, user: User) => Promise<User> {
const { db } = services;
const coll = db.collection<User>('users');
const readUser = Read(services);
class UserModel {
private collection: Collection<User>;
return async (tracker, user) => {
await coll.insertOne(user);
return readUser(tracker, user.uuid);
};
}
constructor(db: Db) {
this.collection = db.collection<User>('users');
}
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 });
public async create(tracker: string, user: User) {
await this.collection.insertOne(user);
return this.read(tracker, user.uuid);
}
public async read(tracker: string, uuid: string) {
const user = await this.collection.findOne({ uuid });
return new User(user || { name: 'not found' });
};
}
}
export default UserModel;

View File

@ -1,15 +1,14 @@
import { CreateUserBody, UserInfo } from '@core';
import { Services } from '../app';
import { User } from '../entities/user';
import { Services } from '../framework/express/server';
import * as mongo from '../framework/mongo/user';
export function Create(
services: Services,
): (tracker: string, raw: CreateUserBody) => Promise<UserInfo> {
const createUser = mongo.Create(services);
const { userModel } = services;
return async (tracker, raw) => {
const user = await createUser(tracker, new User(raw));
const user = await userModel.create(tracker, new User(raw));
return user.Info();
};
}