Skip to content

Commit

Permalink
Revert "refactor: user module functions"
Browse files Browse the repository at this point in the history
This reverts commit 4603d19.
  • Loading branch information
edwinhern committed Jan 21, 2024
1 parent 4603d19 commit f6d9bea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
14 changes: 8 additions & 6 deletions src/modules/user/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export const users: User[] = [
{ id: 2, name: 'Bob', email: '[email protected]', age: 21, createdAt: new Date(), updatedAt: new Date() },
];

export async function findAllAsync(): Promise<User[]> {
return users;
}
export const userRepository = {
findAllAsync: async (): Promise<User[]> => {
return users;
},

export async function findByIdAsync(id: number): Promise<User | null> {
return users.find((user) => user.id === id) || null;
}
findByIdAsync: async (id: number): Promise<User | null> => {
return users.find((user) => user.id === id) || null;
},
};
2 changes: 1 addition & 1 deletion src/modules/user/userRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express, { Request, Response, Router } from 'express';

import { handleServiceResponse, validateRequest } from '@common/utils/httpHandlers';
import * as userService from '@modules/user/userService';
import { userService } from '@modules/user/userService';

import { GetUserSchema } from './userModel';

Expand Down
56 changes: 29 additions & 27 deletions src/modules/user/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@ import { StatusCodes } from 'http-status-codes';

import { ResponseStatus, ServiceResponse } from '@common/models/serviceResponse';
import { User } from '@modules/user/userModel';
import * as userRepository from '@modules/user/userRepository';
import { userRepository } from '@modules/user/userRepository';
import { logger } from '@src/server';

// Retrieves all users from the database
export async function findAll(): Promise<ServiceResponse<User[] | null>> {
try {
const users = await userRepository.findAllAsync();
if (!users) {
return new ServiceResponse(ResponseStatus.Failed, 'No Users found', null, StatusCodes.NOT_FOUND);
export const userService = {
// Retrieves all users from the database
findAll: async (): Promise<ServiceResponse<User[] | null>> => {
try {
const users = await userRepository.findAllAsync();
if (!users) {
return new ServiceResponse(ResponseStatus.Failed, 'No Users found', null, StatusCodes.NOT_FOUND);
}
return new ServiceResponse<User[]>(ResponseStatus.Success, 'Users found', users, StatusCodes.OK);
} catch (ex) {
const errorMessage = `Error finding all users: $${(ex as Error).message}`;
logger.error(errorMessage);
return new ServiceResponse(ResponseStatus.Failed, errorMessage, null, StatusCodes.INTERNAL_SERVER_ERROR);
}
return new ServiceResponse<User[]>(ResponseStatus.Success, 'Users found', users, StatusCodes.OK);
} catch (ex) {
const errorMessage = `Error finding all users: $${(ex as Error).message}`;
logger.error(errorMessage);
return new ServiceResponse(ResponseStatus.Failed, errorMessage, null, StatusCodes.INTERNAL_SERVER_ERROR);
}
}
},

// Retrieves a single user by their ID
export async function findById(id: number): Promise<ServiceResponse<User | null>> {
try {
const user = await userRepository.findByIdAsync(id);
if (!user) {
return new ServiceResponse(ResponseStatus.Failed, 'User not found', null, StatusCodes.NOT_FOUND);
// Retrieves a single user by their ID
findById: async (id: number): Promise<ServiceResponse<User | null>> => {
try {
const user = await userRepository.findByIdAsync(id);
if (!user) {
return new ServiceResponse(ResponseStatus.Failed, 'User not found', null, StatusCodes.NOT_FOUND);
}
return new ServiceResponse<User>(ResponseStatus.Success, 'User found', user, StatusCodes.OK);
} catch (ex) {
const errorMessage = `Error finding user with id ${id}:, ${(ex as Error).message}`;
logger.error(errorMessage);
return new ServiceResponse(ResponseStatus.Failed, errorMessage, null, StatusCodes.INTERNAL_SERVER_ERROR);
}
return new ServiceResponse<User>(ResponseStatus.Success, 'User found', user, StatusCodes.OK);
} catch (ex) {
const errorMessage = `Error finding user with id ${id}:, ${(ex as Error).message}`;
logger.error(errorMessage);
return new ServiceResponse(ResponseStatus.Failed, errorMessage, null, StatusCodes.INTERNAL_SERVER_ERROR);
}
}
},
};
4 changes: 2 additions & 2 deletions tests/modules/user/userService.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StatusCodes } from 'http-status-codes';

import { User } from '@modules/user/userModel';
import * as userRepository from '@modules/user/userRepository';
import * as userService from '@modules/user/userService';
import { userRepository } from '@modules/user/userRepository';
import { userService } from '@modules/user/userService';

jest.mock('@modules/user/userRepository');

Expand Down

0 comments on commit f6d9bea

Please sign in to comment.