created our first service and controller for users > api/users

This commit is contained in:
Hugo Lextrait 2023-03-17 16:37:20 +01:00
parent fafd81db04
commit f95978037a
10 changed files with 95 additions and 70 deletions

View File

@ -3,11 +3,11 @@
"version": "1.0.0", "version": "1.0.0",
"description": "tezosLink project", "description": "tezosLink project",
"_moduleAliases": { "_moduleAliases": {
"@Site": "./dist/site", "@App": "./dist/app",
"@Api": "./dist/site/api", "@Api": "./dist/app/api",
"@Pages": "./dist/pages", "@Pages": "./dist/pages",
"@Common": "./dist/common", "@Common": "./dist/common",
"@Services": "./dist/common/services", "@Services": "./dist/services",
"@Repositories": "./dist/common/repositories", "@Repositories": "./dist/common/repositories",
"@Entries": "./dist/common/entries", "@Entries": "./dist/common/entries",
"@Config": "./dist/common/config", "@Config": "./dist/common/config",
@ -17,7 +17,7 @@
}, },
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"api:start": "node ./dist/entries/Site.js", "api:start": "node ./dist/entries/App.js",
"dev": "nodemon -V", "dev": "nodemon -V",
"api:dev": "nodemon -V --exec 'tsc && npm run api:start'", "api:dev": "nodemon -V --exec 'tsc && npm run api:start'",
"build:test": "tsc && mocha ./dist/entries/Test.js", "build:test": "tsc && mocha ./dist/entries/Test.js",

View File

@ -2,7 +2,7 @@ import { type Response, type Request } from "express";
import { Controller, Get } from "@ControllerPattern/index"; import { Controller, Get } from "@ControllerPattern/index";
import { Service } from "typedi"; import { Service } from "typedi";
import { IsNotEmpty, IsString, IsUUID, validateOrReject } from "class-validator"; import { IsNotEmpty, IsString, IsUUID, validateOrReject } from "class-validator";
import ProjectService from "@Services/_TemplateService/_TemplateService"; // import ProjectService from "@Services/_TemplateService/_TemplateService";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
class Params { class Params {
@ -15,7 +15,7 @@ class Params {
@Controller() @Controller()
@Service() @Service()
export default class ProjectController extends ApiController { export default class ProjectController extends ApiController {
constructor(private projectService: ProjectService) { constructor() {
super(); super();
} }
@ -38,12 +38,12 @@ export default class ProjectController extends ApiController {
return; return;
} }
const project = await this.projectService.getByUUID(); // const project = await this.projectService.getByUUID();
// if (!project) { // if (!project) {
// this.httpNotFoundRequest(res); // this.httpNotFoundRequest(res);
// return; // return;
// } // }
this.httpSuccess(res, project); // this.httpSuccess(res, project);
} }
} }

View File

@ -0,0 +1,38 @@
import { type Response, type Request } from "express";
import { Controller, Get } from "@ControllerPattern/index";
// import { IsNotEmpty, IsString, IsUUID } from "class-validator";
import { Service } from "typedi";
import ApiController from "@Common/system/controller-pattern/ApiController";
import UsersService from "@Services/UsersService/UsersService";
// class Params {
// @IsString()
// @IsNotEmpty()
// @IsUUID()
// public uuid!: string;
// }
@Controller()
@Service()
export default class UserController extends ApiController {
tokensService: any;
constructor(private userService: UsersService) {
super();
}
@Get("/api/users")
protected async get(req: Request, response: Response) {
console.log("UserController > get > response");
// request: Request
// const query = this.buildQueryAsObject<Partial<TokenSupportEntity>>(request);
try {
// await validateOrReject(QueryService.createFrom<Partial<TokenSupportEntity>>(query), {
// forbidUnknownValues: true,
// });
} catch (error) {
this.httpBadRequest(response, error);
return;
}
this.httpSuccess(response, await this.userService.getByUid("uid"));
}
}

View File

@ -1,11 +1,13 @@
import { Container } from "typedi"; import { Container } from "typedi";
import HomeController from "./HomeController"; import HomeController from "./HomeController";
import ProjectController from "./api/ProjectController"; import ProjectController from "./api/ProjectController";
import UserController from "./api/UserController";
export default { export default {
start: () => { start: () => {
Container.get(HomeController); Container.get(HomeController);
Container.get(ProjectController); Container.get(ProjectController);
Container.get(UserController);
} }
} }

View File

@ -19,23 +19,17 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly DATABASE_NAME!: string; public readonly DATABASE_NAME!: string;
@IsOptional()
public readonly API_LABEL!: string;
@IsNotEmpty()
public readonly API_PORT!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly API_ROOT_URL!: string; public readonly API_ROOT_URL!: string;
@IsOptional() @IsOptional()
public readonly SITE_LABEL!: string; public readonly APP_LABEL!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly SITE_PORT!: string; public readonly APP_PORT!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly SITE_ROOT_URL!: string; public readonly APP_ROOT_URL!: string;
public readonly NODE_ENV = process.env.NODE_ENV; public readonly NODE_ENV = process.env.NODE_ENV;
@ -46,11 +40,10 @@ export class BackendVariables {
this.DATABASE_USER = process.env["DATABASE_USER"]!; this.DATABASE_USER = process.env["DATABASE_USER"]!;
this.DATABASE_PASSWORD = process.env["DATABASE_PASSWORD"]!; this.DATABASE_PASSWORD = process.env["DATABASE_PASSWORD"]!;
this.DATABASE_NAME = process.env["DATABASE_NAME"]!; this.DATABASE_NAME = process.env["DATABASE_NAME"]!;
this.API_PORT = process.env["API_PORT"]!;
this.API_ROOT_URL = process.env["API_ROOT_URL"]!; this.API_ROOT_URL = process.env["API_ROOT_URL"]!;
this.SITE_PORT = process.env["SITE_PORT"]!; this.APP_PORT = process.env["APP_PORT"]!;
this.SITE_ROOT_URL = process.env["SITE_ROOT_URL"]!; this.APP_ROOT_URL = process.env["APP_ROOT_URL"]!;
this.SITE_LABEL = process.env["SITE_LABEL"]!; this.APP_LABEL = process.env["APP_LABEL"]!;
} }
public async validate() { public async validate() {
await validateOrReject(this); await validateOrReject(this);

View File

@ -1,41 +0,0 @@
// import ProjectsRepository from "@Common/repositories/projects/ProjectsRepository";
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
@Service()
export default class ProjectsService extends BaseService {
constructor() {
super();
}
/**
* @throws {Error} If projects are undefined
* @returns : Promise<T[]>
* @param : query: ReturnType<typeof processFindManyQuery>
*/
public async getByCriterias() {
// return this.projectRepository.findMany(query);
}
/**
* @throws {Error} If project is undefined
* @returns : Partial<T>
* @param : t : T
*/
public async getByUUID() {
// const project = await this.projectRepository.findOne(projectEntity);
// if (!project) Promise.reject(new Error("Cannot get project by uuid"));
// return project;
}
/**
*
* @throws {Error} If project cannot be created
* @returns : T
* @param : projectEntity: Partial<ProjectEntity>
*/
public async create() {
// const project = await this.projectRepository.create(projectEntity);
// if (!project) Promise.reject(new Error("Cannot create project"));
// return project;
}
}

View File

@ -2,20 +2,20 @@ import "module-alias/register";
import "reflect-metadata"; import "reflect-metadata";
import { Container } from "typedi"; import { Container } from "typedi";
import ExpressServer from "@Common/system/ExpressServer"; import ExpressServer from "@Common/system/ExpressServer";
import routes from "@Site/index"; import routes from "@App/index";
import cors from "cors"; import cors from "cors";
import bodyParser from "body-parser"; import bodyParser from "body-parser";
// import TezosLink from "@Common/databases/TezosLink"; // import TezosLink from "@Common/databases/TezosLink";
import errorHandler from "@Site/middlewares/ErrorHandler"; import errorHandler from "@App/middlewares/ErrorHandler";
import { BackendVariables } from "@Common/config/variables/Variables"; import { BackendVariables } from "@Common/config/variables/Variables";
(async () => { (async () => {
try { try {
const variables = await Container.get(BackendVariables).validate(); const variables = await Container.get(BackendVariables).validate();
const port = variables.SITE_PORT; const port = variables.APP_PORT;
const rootUrl = variables.SITE_ROOT_URL; const rootUrl = variables.APP_ROOT_URL;
const label = variables.SITE_LABEL ?? "Unknown Service"; const label = variables.APP_LABEL ?? "Unknown Service";
// Container.get(TezosLink).connect(); // Container.get(TezosLink).connect();
Container.get(ExpressServer).init({ Container.get(ExpressServer).init({

View File

@ -0,0 +1,32 @@
// import ProjectsRepository from "@Common/repositories/projects/ProjectsRepository";
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
@Service()
export default class UsersService extends BaseService {
constructor() {
super();
}
/**
* @returns : T
* @throws {Error} If project cannot be created
* @param : projectEntity: Partial<ProjectEntity>
*/
public async getByUid(uuid: string) {
// const user = await this.usersRepository.findOne(uuid);
// if (!user) Promise.reject(new Error("Cannot get user by uuid"));
return "User/getByUid > Not implemented yet";
}
/**
* @returns : T
* @throws {Error} If project cannot be created
* @param : projectEntity: Partial<ProjectEntity>
*/
public async create() {
// const project = await this.projectRepository.create(projectEntity);
// if (!project) Promise.reject(new Error("Cannot create project"));
// return project;
}
}

View File

@ -38,14 +38,14 @@
"moduleResolution": "node", "moduleResolution": "node",
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@Site/*": [ "@App/*": [
"src/app/*" "src/app/*"
], ],
"@Api/*": [ "@Api/*": [
"src/app/api/*" "src/app/api/*"
], ],
"@Services/*": [ "@Services/*": [
"src/common/services/*" "src/services/*"
], ],
"@Repositories/*": [ "@Repositories/*": [
"src/common/repositories/*" "src/common/repositories/*"
@ -90,7 +90,8 @@
}, },
"include": [ "include": [
"**/*.ts", "**/*.ts",
"**/*.tsx" "**/*.tsx",
"src/app/api/UserController.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules"