Skip to content

Commit

Permalink
feat: replaced controllers folder in favor of services folder
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Dec 20, 2021
1 parent a1a39ed commit b7428d6
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 221 deletions.
26 changes: 13 additions & 13 deletions example/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router, NextFunction } from 'express'
import httpErrors from 'http-errors'
import { ValidationError } from 'joi'

import { User as UserC } from 'controllers/user'
import { UserService } from 'services/user'
import { idSchema, userSchema } from 'schemas'

const User = Router()
Expand All @@ -17,10 +17,10 @@ User.route('/users')
const {
body: { args }
} = req
const u = new UserC(args as DtoUser)
const us = new UserService(args as DtoUser)

try {
const result = await u.process({ type: 'store' })
const result = await us.process({ type: 'store' })
response({ error: false, message: result, res, status: 201 })
} catch (e) {
next(e)
Expand All @@ -33,10 +33,10 @@ User.route('/users')
res: CustomResponse,
next: NextFunction
): Promise<void> => {
const u = new UserC()
const us = new UserService()

try {
const result = await u.process({ type: 'getAll' })
const result = await us.process({ type: 'getAll' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
next(e)
Expand All @@ -49,10 +49,10 @@ User.route('/users')
res: CustomResponse,
next: NextFunction
): Promise<void> => {
const u = new UserC()
const us = new UserService()

try {
const result = await u.process({ type: 'deleteAll' })
const result = await us.process({ type: 'deleteAll' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
next(e)
Expand All @@ -73,8 +73,8 @@ User.route('/user/:id')

try {
await idSchema.validateAsync(id)
const u = new UserC({ id } as DtoUser)
const result = await u.process({ type: 'getOne' })
const us = new UserService({ id } as DtoUser)
const result = await us.process({ type: 'getOne' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
if (e instanceof ValidationError)
Expand All @@ -101,8 +101,8 @@ User.route('/user/:id')

try {
await userSchema.validateAsync(user)
const u = new UserC(user)
const result = await u.process({ type: 'update' })
const us = new UserService(user)
const result = await us.process({ type: 'update' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
if (e instanceof ValidationError)
Expand All @@ -124,8 +124,8 @@ User.route('/user/:id')

try {
await idSchema.validateAsync(id)
const u = new UserC({ id } as DtoUser)
const result = await u.process({ type: 'delete' })
const us = new UserService({ id } as DtoUser)
const result = await us.process({ type: 'delete' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
if (e instanceof ValidationError)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Process = {
type: 'store' | 'getAll' | 'deleteAll' | 'getOne' | 'update' | 'delete'
}

class User {
class UserService {
private _args: DtoUser | null

constructor(args: DtoUser | null = null) {
Expand Down Expand Up @@ -109,4 +109,4 @@ class User {
}
}

export { User }
export { UserService }
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions example/src/test/index.http
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ GET http://localhost:1996/api/users
DELETE http://localhost:1996/api/users

### Testing getOne user
GET http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
GET http://localhost:1996/api/user/61bfc8e401e954ad15f4b998

### Testing update user
PATCH http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
PATCH http://localhost:1996/api/user/61bfc8e401e954ad15f4b998
Content-Type: application/json

{
Expand All @@ -30,4 +30,4 @@ Content-Type: application/json
}

### Testing delete user
DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
DELETE http://localhost:1996/api/user/61bfc8e401e954ad15f4b998
Loading

0 comments on commit b7428d6

Please sign in to comment.