Skip to content

Commit

Permalink
Add method getAllUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
PahaKoha committed Oct 21, 2023
1 parent f9a4eac commit a4ef0b3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/domain/entities/userList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import User from '@domain/entities/user.js';

export type UserList = {
users: User[];
};
9 changes: 9 additions & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -36,6 +42,7 @@ export interface DomainServices {
*/
aiService: AIService
editorToolsService: EditorToolsService,
userListService: UserListService
}

/**
Expand All @@ -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,
Expand All @@ -67,5 +75,6 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe
authService,
aiService,
editorToolsService,
userListService
};
}
16 changes: 16 additions & 0 deletions src/domain/service/userListService.ts
Original file line number Diff line number Diff line change
@@ -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<User[]> {
return await this.repository.getAllUsersById();
}

}
7 changes: 7 additions & 0 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/presentation/http/router/userList.ts
Original file line number Diff line number Diff line change
@@ -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<UsersRouterOptions> = (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;
6 changes: 6 additions & 0 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,6 +44,8 @@ export interface Repositories {
*/
aiRepository: AIRepository
editorToolsRepository: EditorToolsRepository,

userListRepository: UserListRepository
}

/**
Expand Down Expand Up @@ -107,6 +110,8 @@ export async function init(orm: Orm): Promise<Repositories> {
const userRepository = new UserRepository(userStorage, googleApiTransport);
const aiRepository = new AIRepository(openaiApiTransport);
const editorToolsRepository = new EditorToolsRepository(editorToolsStorage);
const userListRepository = new UserListRepository(userStorage)


return {
noteRepository,
Expand All @@ -115,5 +120,6 @@ export async function init(orm: Orm): Promise<Repositories> {
userRepository,
aiRepository,
editorToolsRepository,
userListRepository
};
}
6 changes: 5 additions & 1 deletion src/repository/storage/postgres/orm/sequelize/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ export default class UserSequelizeStorage {
photo: user.photo,
};
}

/**
* Get user by id or email
*
Expand Down Expand Up @@ -282,4 +281,9 @@ export default class UserSequelizeStorage {
photo: user.photo,
};
}
public async getAllUsersById(): Promise<User[]> {
const userList = await this.model.findAll({ });

return userList;
}
}
24 changes: 24 additions & 0 deletions src/repository/userList.repository.ts
Original file line number Diff line number Diff line change
@@ -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<User[]> {
return await this.storage.getAllUsersById();
}





}

0 comments on commit a4ef0b3

Please sign in to comment.