Skip to content

Commit

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

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

findByIdAsync: async (id: number): Promise<User | null> => {
return users.find((user) => user.id === id) || null;
},
};
export async function findByIdAsync(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 { userService } from '@modules/user/userService';
import * as userService from '@modules/user/userService';

import { GetUserSchema } from './userModel';

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

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

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);
// 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);
}
},
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
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);
// 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);
}
},
};
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 { userRepository } from '@modules/user/userRepository';
import { userService } from '@modules/user/userService';
import * as userRepository from '@modules/user/userRepository';
import * as userService from '@modules/user/userService';

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

Expand Down

0 comments on commit d5486de

Please sign in to comment.