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

Fix/adjustmnet when param is a boolean #12

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
72dbcdd
fix: remove import auth routes
Sep 21, 2023
0e4bc37
Merge pull request #1 from inovando/fix/queryBuilderProvider
jeanc-alves Sep 21, 2023
fe1902a
Merge pull request #2 from inovando/fix/remove-auth-routes
jeanc-alves Sep 21, 2023
1bfb5d2
fix: change version packege
Sep 21, 2023
8e7e9dc
Merge pull request #3 from inovando/fix/version-package
jeanc-alves Sep 21, 2023
eaf2fc2
fix: remove console logs and pagination metodo index
Nov 7, 2023
27e8ad2
feat: events logs
tiago-cristiano-inovan-do Nov 13, 2023
d4758cb
feat: events logs
tiago-cristiano-inovan-do Nov 13, 2023
d2cfade
Merge branch 'tiago-cristiano-inovan-do:main' into main
tiagoCristiano Nov 13, 2023
ee0954e
Merge pull request #4 from inovando/feat/events
tiagoCristiano Nov 13, 2023
8e0f766
feat: events logs
tiago-cristiano-inovan-do Nov 13, 2023
29fe546
feat: clean old code
tiago-cristiano-inovan-do Nov 14, 2023
fb54194
doc: update version to .17
tiago-cristiano-inovan-do Nov 14, 2023
e003dfd
Merge branch 'main' into feat/tiago
tiagoCristiano Nov 14, 2023
67eabe5
Merge pull request #5 from inovando/feat/tiago
tiagoCristiano Nov 14, 2023
426792b
fix: pagination
Dec 12, 2023
e928393
Merge pull request #6 from inovando/fix/order-and-pagination
jeanc-alves Dec 12, 2023
ab526e7
fix: version
Dec 12, 2023
ba3f602
Merge pull request #7 from inovando/fix/version-package
jeanc-alves Dec 12, 2023
4ec7b70
fix: adjustment on Operator In and notIn
Dec 13, 2023
3ad4a35
Merge pull request #8 from inovando/fix/queryBulder
jeanc-alves Dec 13, 2023
bf70061
feat: change package version
Dec 13, 2023
6d6ca1f
Merge pull request #9 from inovando/fix/version-package
jeanc-alves Dec 13, 2023
f810f28
fix: remove debuger and change version
Dec 14, 2023
64c34b5
Merge pull request #10 from inovando/fix/remove-debugger
jeanc-alves Dec 14, 2023
2bd4b15
fix: adjustment on query build order and sort
Dec 19, 2023
ed1a6ae
fix: adjustment on operators
Jan 19, 2024
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ config.json
package.json
*.html
*.txt
*.hbs
templates/**
53 changes: 8 additions & 45 deletions instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function instructions(projectRoot: string) {
const controllerPath = `${projectRoot}/app/Controllers/Http`
copyFolderToProjetct({ pathDestiny: controllerPath, folder: 'Controllers' })

console.log('Coping Prividers')
console.log('Coping Providers')
const providersPath = `${projectRoot}/providers`
copyFolderToProjetct({ pathDestiny: providersPath, folder: 'providers', noCaptlize: true })

Expand Down Expand Up @@ -66,48 +66,11 @@ export default async function instructions(projectRoot: string) {
noCaptlize: true,
})

// Coby Controllers
// console.log('Coping Controllers')
// const controllersPath = `${projectRoot}/app/Controllers/Http`
// console.log({ controllersPath, projectRoot })
// if (!fs.existsSync(controllersPath)) {
// fs.mkdirSync(modelPath, { recursive: true })
// }

// // 3. Copy models
// const modelPath = `${projectRoot}/app/Models`
// if (!fs.existsSync(modelPath)) {
// fs.mkdirSync(modelPath, { recursive: true })
// }
// fs.copyFileSync(`${packageBasePath}/models/Permission.txt`, `${modelPath}/Permission.ts`)
// fs.copyFileSync(`${packageBasePath}/models/Role.txt`, `${modelPath}/Role.ts`)
// fs.copyFileSync(`${packageBasePath}/models/RolePermission.txt`, `${modelPath}/RolePermission.ts`)
// fs.copyFileSync(`${packageBasePath}/models/UserPermission.txt`, `${modelPath}/UserPermission.ts`)
// fs.copyFileSync(`${packageBasePath}/models/UserRole.txt`, `${modelPath}/UserRole.ts`)

// // 4. Copy migrations
// const migrationPath = `${projectRoot}/database/migrations`
// if (!fs.existsSync(migrationPath)) {
// fs.mkdirSync(migrationPath, { recursive: true })
// }
// fs.copyFileSync(
// `${packageBasePath}/migrations/acl_1_roles.txt`,
// `${migrationPath}/acl_1_roles.ts`
// )
// fs.copyFileSync(
// `${packageBasePath}/migrations/acl_2_permissions.txt`,
// `${migrationPath}/acl_2_permissions.ts`
// )
// fs.copyFileSync(
// `${packageBasePath}/migrations/acl_3_role_permissions.txt`,
// `${migrationPath}/acl_3_role_permissions.ts`
// )
// fs.copyFileSync(
// `${packageBasePath}/migrations/acl_4_user_permissions.txt`,
// `${migrationPath}/acl_4_user_permissions.ts`
// )
// fs.copyFileSync(
// `${packageBasePath}/migrations/acl_5_user_roles.txt`,
// `${migrationPath}/acl_5_user_roles.ts`
// )
console.log('Coping code-templates')
const codeTemplatesPath = `${projectRoot}/code-templates`
copyFolderToProjetct({
pathDestiny: codeTemplatesPath,
folder: 'code-templates',
noCaptlize: true,
})
}
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.

4 changes: 2 additions & 2 deletions 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.21",
"description": "A crud abstraction for AdonisJs",
"main": "build/providers/AdonisCrudProvider.js",
"scripts": {
Expand Down Expand Up @@ -142,4 +142,4 @@
"directories": {
"doc": "docs"
}
}
}
17 changes: 1 addition & 16 deletions providers/AdonisCrudProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,5 @@ 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.group(() => {
Route.resource('profiles', 'ProfileController').apiOnly()
}).middleware('auth')

Route.group(() => {
Route.resource('users', 'UserController').apiOnly()
}).middleware('auth')
}
public async boot() {}
}
55 changes: 38 additions & 17 deletions src/Decorators/Controller/CrudDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,32 @@ type FunctionMap = {
export function Crud(options: CrudOperationsOptions): ClassDecorator {
const functionMap: FunctionMap = {
async index(ctx) {
const { transform } = ctx
const authUser = ctx.auth.user
const { all, page, perPage } = ctx.request.all()

const { all, page = 1, perPage = 10, sort = 'created_at', order = 'desc' } = ctx.request.all()

const qs = ctx.request.qs()

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

query.orderBy(sort, order)
if (all) {
return query.exec()
}

const paginatedItems = await query.paginate(page, perPage)
const pagination = {
page: 1,
firstPage: 1,
lastPage: 1,
}

return {
pagination,
data: await transform.withContext(ctx).collection(paginatedItems, options.transformer),
}
const { rows, currentPage, total, lastPage } = await query.paginate(page, perPage)

return ctx.transform.paginate(
{
rows,
pages: {
page: currentPage,
perPage: perPage,
total: total,
lastPage: lastPage,
},
},
options.transformer
)
},

async show(ctx) {
Expand Down Expand Up @@ -81,7 +84,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,24 +105,43 @@ 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' })
}

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)
options.event.emit(`afterUpdate:${currentObject.constructor.table}`, {
body,
id,
updatedObject: updatedObject.toJSON(),
})

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

async destroy(ctx) {
const { params } = ctx

const currentObject = await options.repository.getById({ id: params.id })
const deleted = await options.repository.destroy(params.id)

if (deleted) {
options.event.emit(`afterDelete:${currentObject.constructor.table}`, {
id: params.id,
deleted: currentObject.toJSON(),
})
return ctx.response.status(204)
}

Expand Down
8 changes: 5 additions & 3 deletions src/Decorators/Repository/CrudRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LucidModel } from '@ioc:Adonis/Lucid/Orm'
import { QueryBuilder } from '../../QueryBuilder/QueryBuilder'
import { DateTime } from 'luxon'

type FunctionMap = {
[key: string]: Function
Expand All @@ -25,7 +26,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,10 +47,12 @@ export function CrudRepository<T extends LucidModel>(Model: T): ClassDecorator {
const modelToDelete = await this.getById({ id })
if (!modelToDelete) return false
try {
await modelToDelete.delete()
await modelToDelete.merge({ status: false, deleted_at: DateTime.now() })
await modelToDelete.save()

return true
} catch (error) {
return false
throw error
}
},
async getById({ id, status = true }) {
Expand Down
Loading