Skip to content

Commit

Permalink
feat: some code improvements: modified user model, user endpoints, us…
Browse files Browse the repository at this point in the history
…er dto, user queries and user schemas. Also the example packages where updated
  • Loading branch information
AnthonyLzq committed Jan 26, 2022
1 parent e29758f commit 5ddbd28
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 307 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**/node_modules
.env
**/.env
2 changes: 1 addition & 1 deletion example/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 AnthonyLzq
Copyright (c) 2022 AnthonyLzq

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions example/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/61f0dd3c921b7811b4640dd3

### Testing update user
PATCH http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
PATCH http://localhost:1996/api/user/61f0dd3c921b7811b4640dd3
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/61f0dd3c921b7811b4640dd3
24 changes: 13 additions & 11 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,36 @@
"express": "^4.17.2",
"http-errors": "^2.0.0",
"joi": "^17.5.0",
"mongoose": "^6.1.4",
"mongoose": "^6.1.8",
"morgan": "^1.10.0",
"swagger-ui-express": "^4.3.0"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/http-errors": "^1.8.1",
"@types/http-errors": "^1.8.2",
"@types/morgan": "^1.9.3",
"@types/node": "^17.0.5",
"@types/node": "^17.0.12",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"dotenv": "^10.0.0",
"eslint": "^8.5.0",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.10.1",
"dotenv": "^14.3.2",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"nodemon": "^2.0.15",
"prettier": "^2.5.1",
"standard-version": "^9.3.2",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.12.0",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"typescript": "^4.5.4",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"typescript": "^4.5.5",
"webpack": "^5.67.0",
"webpack-cli": "^4.9.2",
"webpack-node-externals": "^3.0.0"
}
}
6 changes: 3 additions & 3 deletions example/src/@types/dto/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface DtoUser {
id?: string
lastName?: string
name?: string
id: string
lastName: string
name: string
}
2 changes: 1 addition & 1 deletion example/src/@types/models/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface IUser {
_id: import('mongoose').Types.ObjectId
id: string
name: string
lastName: string
updatedAt: Date
Expand Down
6 changes: 3 additions & 3 deletions example/src/database/mongo/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const User = new Schema<IUser>(
createdAt: false,
updatedAt: true
},
versionKey: false,
toJSON: {
transform(_, ret) {
ret.id = ret._id
ret.id = ret._id.toString()
ret.updatedAt = ret.updatedAt.toISOString()
delete ret._id
delete ret.__v
delete ret.updatedAt
},
versionKey: false,
virtuals: true
}
}
Expand Down
18 changes: 13 additions & 5 deletions example/src/database/mongo/queries/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { UserModel } from '../models'
import { UserModel } from '..'

const store = async (userData: DtoUser): Promise<IUser> => {
const user = new UserModel(userData)
await user.save()

return await user.save()
return user.toJSON()
}

const remove = async (
Expand All @@ -17,15 +18,22 @@ const remove = async (
const get = async (
id: string | null = null
): Promise<IUser[] | IUser | null> => {
if (id) return await UserModel.findById(id)
if (id) {
const user = await UserModel.findById(id)

return await UserModel.find({})
return user ? user.toJSON() : null
}

const users = await UserModel.find({})

return users.map(u => u.toJSON())
}

const update = async (userData: DtoUser): Promise<IUser | null> => {
const { id, ...rest } = userData
const user = await UserModel.findByIdAndUpdate(id, rest, { new: true })

return await UserModel.findByIdAndUpdate(id, rest, { new: true })
return user ? user.toJSON() : null
}

export { store, remove, get, update }
7 changes: 6 additions & 1 deletion example/src/network/routes/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ const userSchema = Joi.object().keys({
name: Joi.string().required()
})

export { userSchema }
const storeUserSchema = Joi.object().keys({
lastName: Joi.string().required(),
name: Joi.string().required()
})

export { userSchema, storeUserSchema }
14 changes: 9 additions & 5 deletions example/src/network/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ValidationError } from 'joi'

import { response } from 'network/response'
import { UserService } from 'services/user'
import { idSchema, userSchema } from './schemas'
import { idSchema, userSchema, storeUserSchema } from './schemas'

const User = Router()

Expand All @@ -18,12 +18,16 @@ User.route('/users')
const {
body: { args }
} = req
const us = new UserService(args as DtoUser)

try {
await storeUserSchema.validateAsync(args)
const us = new UserService(args)
const result = await us.process({ type: 'store' })
response({ error: false, message: result, res, status: 201 })
} catch (e) {
if (e instanceof ValidationError)
return next(new httpErrors.UnprocessableEntity(e.message))

next(e)
}
}
Expand Down Expand Up @@ -74,7 +78,7 @@ User.route('/user/:id')

try {
await idSchema.validateAsync(id)
const us = new UserService({ id } as DtoUser)
const us = new UserService({ id })
const result = await us.process({ type: 'getOne' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
Expand All @@ -95,7 +99,7 @@ User.route('/user/:id')
body: { args },
params: { id }
} = req
const user: DtoUser = {
const user = {
id,
...args
}
Expand Down Expand Up @@ -125,7 +129,7 @@ User.route('/user/:id')

try {
await idSchema.validateAsync(id)
const us = new UserService({ id } as DtoUser)
const us = new UserService({ id })
const result = await us.process({ type: 'delete' })
response({ error: false, message: result, res, status: 200 })
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions example/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type Process = {
}

class UserService {
private _args: DtoUser | null
private _args: Partial<DtoUser> | null

constructor(args: DtoUser | null = null) {
constructor(args: Partial<DtoUser> | null = null) {
this._args = args
}

Expand Down
Loading

0 comments on commit 5ddbd28

Please sign in to comment.