Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor users route to be split into controller/service/repository layers #105

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add comments about future additions
  • Loading branch information
jazhen committed Sep 1, 2022
commit 7fcb8bbdbcb880e0883d6858a6a600e1cb236dbf
8 changes: 8 additions & 0 deletions server/routes/users/users-controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import AppError from '../../errors/AppError.js';

/**
* considering doing multiple exports, i.e. something like
* export getRequestHandler(usersService, httpRequest)
* export postRequestHandler(usersService, httpRequest)
* import * as UsersController from '...'
* instead of
* export function buildUsersController({ usersService })
*/
export function buildUsersController({ usersService }) {
return {
async getRequestHandler(httpRequest) {
Expand Down
7 changes: 7 additions & 0 deletions server/routes/users/users-queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Interface that the implementations must extend from.
* Ensures that all implementations have the exact same functions, which
* keeps prod and test repository implementations in always in sync.
* The interface cannnot change without forcing changes in both the
* prod and test repository implementations.
*/
export class IUsersRepository {
constructor({ dataSource }) {
this.dataSource = dataSource;
Expand Down
12 changes: 10 additions & 2 deletions server/routes/users/users-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export const buildUsersService = ({ usersRepository }) => {
/**
* considering doing multiple exports, i.e. something like
* export getUser(usersRepository, userDTO)
* export createUser(usersRepository, userDTO)
* import * as UsersService from '...'
* instead of
* export buildUsersService({ usersRepository })
*/
export function buildUsersService({ usersRepository }) {
return {
async getUser({ userDTO: { email } }) {
// validate business logic here
Expand All @@ -13,4 +21,4 @@ export const buildUsersService = ({ usersRepository }) => {
return usersRepository.createUser({ user: userDTO });
},
};
};
}