From a4ef0b342396b626f59941ed161ed5eee37a0e76 Mon Sep 17 00:00:00 2001 From: paha_koha Date: Sun, 22 Oct 2023 01:24:36 +0300 Subject: [PATCH] Add method getAllUsers --- src/domain/entities/userList.ts | 5 ++++ src/domain/index.ts | 9 ++++++ src/domain/service/userListService.ts | 16 ++++++++++ src/presentation/http/http-api.ts | 7 +++++ src/presentation/http/router/userList.ts | 29 +++++++++++++++++++ src/repository/index.ts | 6 ++++ .../storage/postgres/orm/sequelize/user.ts | 6 +++- src/repository/userList.repository.ts | 24 +++++++++++++++ 8 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/domain/entities/userList.ts create mode 100644 src/domain/service/userListService.ts create mode 100644 src/presentation/http/router/userList.ts create mode 100644 src/repository/userList.repository.ts diff --git a/src/domain/entities/userList.ts b/src/domain/entities/userList.ts new file mode 100644 index 00000000..91408095 --- /dev/null +++ b/src/domain/entities/userList.ts @@ -0,0 +1,5 @@ +import User from '@domain/entities/user.js'; + +export type UserList = { + users: User[]; +}; \ No newline at end of file diff --git a/src/domain/index.ts b/src/domain/index.ts index ca0af9d8..650a535a 100644 --- a/src/domain/index.ts +++ b/src/domain/index.ts @@ -7,6 +7,12 @@ import UserService from '@domain/service/user.js'; import AIService from './service/ai.js'; import EditorToolsService from '@domain/service/editorTools.js'; +import UserListRepository from '@repository/userList.repository.js'; +// import userListService from './service/userListService.js'; +// import userListService from './service/userListService.js'; +// import userListService from './service/userListService.js'; +import UserListService from './service/userListService.js'; + /** * Interface for initiated services */ @@ -36,6 +42,7 @@ export interface DomainServices { */ aiService: AIService editorToolsService: EditorToolsService, + userListService: UserListService } /** @@ -59,6 +66,7 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe const aiService = new AIService(repositories.aiRepository); const editorToolsService = new EditorToolsService(repositories.editorToolsRepository); +const userListService = new UserListService(repositories.userListRepository) return { noteService, @@ -67,5 +75,6 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe authService, aiService, editorToolsService, + userListService }; } diff --git a/src/domain/service/userListService.ts b/src/domain/service/userListService.ts new file mode 100644 index 00000000..371f3bb5 --- /dev/null +++ b/src/domain/service/userListService.ts @@ -0,0 +1,16 @@ +import type User from '@domain/entities/user'; +import type UserListRepository from '@repository/userList.repository'; + + +export default class UserListService { + public readonly repository: UserListRepository; + + constructor(repository: UserListRepository) { + this.repository = repository; + } + + public async getAllUsersById(): Promise { + return await this.repository.getAllUsersById(); + } + + } \ No newline at end of file diff --git a/src/presentation/http/http-api.ts b/src/presentation/http/http-api.ts index 8485f53c..53de4182 100644 --- a/src/presentation/http/http-api.ts +++ b/src/presentation/http/http-api.ts @@ -22,6 +22,8 @@ import { NoteSchema } from './schema/Note.js'; import Policies from './policies/index.js'; import type { RequestParams, Response } from '@presentation/api.interface.js'; import NoteSettingsRouter from './router/noteSettings.js'; +import AllUsersRouter from './router/userList.js'; +import userListService from '@domain/service/userListService.js'; const appServerLogger = getLogger('appServer'); @@ -196,6 +198,11 @@ export default class HttpApi implements Api { aiService: domainServices.aiService, }); + await this.server?.register(AllUsersRouter, { + prefix: '/', + userListService: domainServices.userListService, + }) + await this.server?.register(EditorToolsRouter, { prefix: '/editor-tools', editorToolsService: domainServices.editorToolsService, diff --git a/src/presentation/http/router/userList.ts b/src/presentation/http/router/userList.ts new file mode 100644 index 00000000..4a349c06 --- /dev/null +++ b/src/presentation/http/router/userList.ts @@ -0,0 +1,29 @@ + +import type { FastifyPluginCallback } from 'fastify'; +import userListService from '@domain/service/userListService'; +import { request } from 'http'; + +interface UsersRouterOptions { + + userListService: userListService, + } + + +const AllUsersRouter: FastifyPluginCallback = (fastify, opts, done) => { + + const usersService = opts.userListService; + fastify.get('/allUsers', { + + }, async (request, reply) => { + // const { userId } = request; + const userList = await usersService.getAllUsersById(); + if (userList != null) { + return reply.send(userList); + } else { + return reply.send({message: '!'}) + } + }); + + done(); + }; + export default AllUsersRouter; \ No newline at end of file diff --git a/src/repository/index.ts b/src/repository/index.ts index 4699ce73..0c8458d0 100644 --- a/src/repository/index.ts +++ b/src/repository/index.ts @@ -13,6 +13,7 @@ import UserRepository from '@repository/user.repository.js'; import AIRepository from './ai.repository.js'; import OpenAIApi from './transport/openai-api/index.js'; import EditorToolsRepository from '@repository/editorTools.repository.js'; +import UserListRepository from './userList.repository.js'; /** * Interface for initiated repositories @@ -43,6 +44,8 @@ export interface Repositories { */ aiRepository: AIRepository editorToolsRepository: EditorToolsRepository, + + userListRepository: UserListRepository } /** @@ -107,6 +110,8 @@ export async function init(orm: Orm): Promise { const userRepository = new UserRepository(userStorage, googleApiTransport); const aiRepository = new AIRepository(openaiApiTransport); const editorToolsRepository = new EditorToolsRepository(editorToolsStorage); + const userListRepository = new UserListRepository(userStorage) + return { noteRepository, @@ -115,5 +120,6 @@ export async function init(orm: Orm): Promise { userRepository, aiRepository, editorToolsRepository, + userListRepository }; } diff --git a/src/repository/storage/postgres/orm/sequelize/user.ts b/src/repository/storage/postgres/orm/sequelize/user.ts index 10f7b54a..0f418cfa 100644 --- a/src/repository/storage/postgres/orm/sequelize/user.ts +++ b/src/repository/storage/postgres/orm/sequelize/user.ts @@ -229,7 +229,6 @@ export default class UserSequelizeStorage { photo: user.photo, }; } - /** * Get user by id or email * @@ -282,4 +281,9 @@ export default class UserSequelizeStorage { photo: user.photo, }; } + public async getAllUsersById(): Promise { + const userList = await this.model.findAll({ }); + + return userList; + } } diff --git a/src/repository/userList.repository.ts b/src/repository/userList.repository.ts new file mode 100644 index 00000000..aebcb229 --- /dev/null +++ b/src/repository/userList.repository.ts @@ -0,0 +1,24 @@ +import User from '@domain/entities/user'; +// import type User from '@domain/entities/user'; +import type UserStorage from '@repository/storage/user.storage.js'; + + +export default class UserListRepository { + + public storage: UserStorage; + + constructor(storage: UserStorage) { + this.storage = storage; + } + + + + public async getAllUsersById(): Promise { + return await this.storage.getAllUsersById(); + } + + + + + +} \ No newline at end of file