Skip to content

Commit

Permalink
feat: sequelize integration part 6 - finished services creation in th…
Browse files Browse the repository at this point in the history
…e express and

fastify files
  • Loading branch information
AnthonyLzq committed Aug 7, 2022
1 parent 31fe704 commit efad703
Show file tree
Hide file tree
Showing 3 changed files with 447 additions and 5 deletions.
221 changes: 221 additions & 0 deletions lib/src/functions/api/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,225 @@ export {
])
}

/**
* @param {Object} args
* @param {String} args.projectName
* @param {Boolean} args.dbIsSQL
*/
const services = async ({ projectName, dbIsSQL }) => {
const createFoldersCommand = `mkdir ${projectName}/src/services \
${projectName}/src/services/utils \
${projectName}/src/services/utils/messages`

if (platform() === 'win32')
await exec(createFoldersCommand.replaceAll('/', '\\'))
else await exec(createFoldersCommand)

const services = {
index: {
content: "export * from './user'\n",
file: `${projectName}/src/services/index.ts`
},
user: {
content: `import httpErrors from 'http-errors'
import { store, remove, get, update } from 'database'
import { User, UserDTO, UserWithId } from 'schemas'
import { EFU, MFU, GE, errorHandling } from './utils'
type Process = {
type: 'store' | 'getAll' | 'deleteAll' | 'getOne' | 'update' | 'delete'
}
type Arguments = {
id?: string
user?: User
userWithId?: UserWithId
}
class UserService {
#args: Arguments
constructor(args: Arguments = {}) {
this.#args = args
}
public process({ type }: Process): Promise<string | UserDTO | UserDTO[]> {
switch (type) {
case 'store':
return this.#store()
case 'getAll':
return this.#getAll()
case 'deleteAll':
return this.#deleteAll()
case 'getOne':
return this.#getOne()
case 'update':
return this.#update()
case 'delete':
return this.#delete()
default:
throw new httpErrors.InternalServerError(GE.INTERNAL_SERVER_ERROR)
}
}
async #store(): Promise<UserDTO> {
try {
if (!this.#args.user)
throw new httpErrors.UnprocessableEntity(GE.INTERNAL_SERVER_ERROR)
const result = await store(this.#args.user)
return result
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
async #getAll(): Promise<UserDTO[]> {
try {
const users = (await get()) as UserDTO[]
return users
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
async #deleteAll(): Promise<string> {
try {
const usersDeleted = (await remove()) as number
${
dbIsSQL
? 'if (usersDeleted !== 0) return MFU.ALL_USERS_DELETED'
: `if (usersDeleted >= 1) return MFU.ALL_USERS_DELETED
if (usersDeleted === 0)
throw new httpErrors.Conflict(EFU.NOTHING_TO_DELETE)`
}
throw new httpErrors.InternalServerError(GE.INTERNAL_SERVER_ERROR)
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
async #getOne(): Promise<UserDTO> {
try {
if (!this.#args.id)
throw new httpErrors.UnprocessableEntity(GE.INTERNAL_SERVER_ERROR)
const { id } = this.#args
const user = (await get(id)) as UserDTO | null
if (!user) throw new httpErrors.NotFound(EFU.NOT_FOUND)
return user
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
async #update(): Promise<UserDTO> {
try {
if (!this.#args.userWithId || !this.#args.userWithId.id)
throw new httpErrors.UnprocessableEntity(GE.INTERNAL_SERVER_ERROR)
const updatedUser = await update(this.#args.userWithId)
if (!updatedUser) throw new httpErrors.NotFound(EFU.NOT_FOUND)
return updatedUser
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
async #delete(): Promise<string> {
try {
if (!this.#args.id)
throw new httpErrors.UnprocessableEntity(GE.INTERNAL_SERVER_ERROR)
const { id } = this.#args
const deletedUser = await remove(id)
if (!deletedUser) throw new httpErrors.NotFound(EFU.NOT_FOUND)
return MFU.USER_DELETED
} catch (e) {
return errorHandling(e, GE.INTERNAL_SERVER_ERROR)
}
}
}
export { UserService }
`,
file: `${projectName}/src/services/user.ts`
},
utils: {
index: {
content: `import httpErrors from 'http-errors'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorHandling = (e: any, message?: string): never => {
console.error(e)
if (e instanceof httpErrors.HttpError) throw e
throw new httpErrors.InternalServerError(message ?? e.message)
}
export { errorHandling }
export * from './messages'
`,
file: `${projectName}/src/services/utils/index.ts`
}
},
'utils/messages': {
index: {
content: `enum GenericErrors {
INTERNAL_SERVER_ERROR = 'Something went wrong'
}
export { GenericErrors as GE }
export * from './user'
`,
file: `${projectName}/src/services/utils/messages/index.ts`
},
user: {
content: `enum ErrorForUser {
NOT_FOUND = 'The requested user does not exists',
NOTHING_TO_DELETE = 'There is no user to be deleted'
}
enum MessageForUser {
ALL_USERS_DELETED = 'All the users were deleted successfully',
USER_DELETED = 'The requested user was successfully deleted'
}
export { ErrorForUser as EFU, MessageForUser as MFU }
`,
file: `${projectName}/src/services/utils/messages/user.ts`
}
}
}

await Promise.all([
writeFile(services.index.file, services.index.content),
writeFile(services.user.file, services.user.content),
writeFile(services.utils.index.file, services.utils.index.content),
writeFile(
services['utils/messages'].index.file,
services['utils/messages'].index.content
),
writeFile(
services['utils/messages'].user.file,
services['utils/messages'].user.content
)
])
}

/**
* @param {Object} args
* @param {String} args.projectName
Expand All @@ -1598,6 +1817,8 @@ const main = async ({ projectName, graphQL, database }) => {
await network({ projectName, graphQL })
await schemas({ projectName, dbIsSQL })

if (!graphQL) await services({ projectName, dbIsSQL })

if (dbIsSQL) await sql({ projectName, db: database })
else await mongo({ projectName })
}
Expand Down
Loading

0 comments on commit efad703

Please sign in to comment.