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

Feat/events #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inovan.do/adonis-crud",
"version": "0.0.14",
"version": "0.1.16",
"description": "A crud abstraction for AdonisJs",
"main": "build/providers/AdonisCrudProvider.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions providers/AdonisCrudProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default class AdonisCrudProvider {
public async boot() {
const Route = this.app.container.use('Adonis/Core/Route')
Route.resource('/codegem', 'CodeGemController').apiOnly()
Route.post('/auth/register', 'AuthController.store')
Route.post('/auth/login', 'AuthController.login')
Route.post('/auth/reset-password', 'AuthController.resetPassword')
Route.get('/auth/me', 'AuthController.me').middleware('auth')
// Route.post('/auth/register', 'AuthController.store')
// Route.post('/auth/login', 'AuthController.login')
// Route.post('/auth/reset-password', 'AuthController.resetPassword')
// Route.get('/auth/me', 'AuthController.me').middleware('auth')

Route.group(() => {
Route.resource('profiles', 'ProfileController').apiOnly()
Expand Down
33 changes: 28 additions & 5 deletions src/Decorators/Controller/CrudDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ export function Crud(options: CrudOperationsOptions): ClassDecorator {

const query = options.repository.index({ qs, authUser })

const total = await query.exec().length

if (all) {
return query.exec()
}

const paginatedItems = await query.paginate(page, perPage)

const pagination = {
page: 1,
firstPage: 1,
lastPage: 1,
lastPage: perPage !== 'all' ? Math.ceil(total / perPage) : 1,
page: parseInt(page),
perPage: perPage !== 'all' ? parseInt(perPage) : total,
total: total,
}

return {
Expand Down Expand Up @@ -81,7 +85,6 @@ export function Crud(options: CrudOperationsOptions): ClassDecorator {
}

const newObject = await options.repository.store(body)
console.log({ new: `${newObject.constructor.table}` })
options.event.emit(`new:${newObject.constructor.table}`, newObject)

return ctx.response.status(201).json(newObject)
Expand All @@ -103,15 +106,35 @@ export function Crud(options: CrudOperationsOptions): ClassDecorator {
return ctx.response.badRequest(this.errorsRequest)
}
}
const currentObject = await options.repository.getById({ id })
if (!currentObject) {
return ctx.response.status(404).json({ msg: 'Not Found' })
}
console.log(`beforeUpdate:${currentObject.constructor.table}`, {
id,
body,
currentObject: currentObject.toJSON(),
})

options.event.emit(`beforeUpdate:${currentObject.constructor.table}`, {
body,
id,
currentObject: currentObject.toJSON(),
})

const updatedObject = await options.repository.update({ id, body })

if (!updatedObject) {
return ctx.response.status(404)
}

const updateOutput = await transform.withContext(ctx).item(updatedObject, options.transformer)
console.log(`afterUpdate:${currentObject.constructor.table}`, {
id,
body,
updatedObject: updatedObject.toJSON(),
})

const updateOutput = await transform.withContext(ctx).item(updatedObject, options.transformer)
return ctx.response.status(200).json(updateOutput)
},

Expand Down
6 changes: 3 additions & 3 deletions src/Decorators/Repository/CrudRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function CrudRepository<T extends LucidModel>(Model: T): ClassDecorator {
qs: rest,
selectFields: [],
})
console.log('updated to return query', query)
return query
},
async show({ id, status }) {
Expand All @@ -47,8 +46,9 @@ export function CrudRepository<T extends LucidModel>(Model: T): ClassDecorator {
const modelToDelete = await this.getById({ id })
if (!modelToDelete) return false
try {
await modelToDelete.delete()
return true
await modelToDelete.merge({ status: false, deleted_at: new Date() })
await modelToDelete.save()
return modelToDelete
} catch (error) {
return false
}
Expand Down
1 change: 0 additions & 1 deletion src/QueryBuilder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class QueryBuilder {
3: () => {
const [relation, field, op] = parts
operator = op.startsWith('$') ? (op as Operator) : Operator.Equals
console.log({ operator })
validateOperator(operator)
query.whereHas(relation, (subQuery) => {
debugger
Expand Down
17 changes: 17 additions & 0 deletions src/Validators/StrongPasswordRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { string } from '@ioc:Adonis/Core/Helpers'
import { validator } from '@ioc:Adonis/Core/Validator'

validator.rule('strongPassword', (value, _, options) => {
if (typeof value !== 'string') {
return
}

if (value !== string.camelCase(value)) {
options.errorReporter.report(
options.pointer,
'camelCase',
'camelCase validation failed',
options.arrayExpressionPointer
)
}
})
Loading