update
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import Contact from '#base_modules/Contact/model/contact'
|
||||
import { createContactValidator } from '#base_modules/Contact/validators/contact_create_validator'
|
||||
import { updateContactValidator } from '#base_modules/Contact/validators/contact_update_validator'
|
||||
|
||||
export default class ContactsController {
|
||||
/**
|
||||
* @summary List contacts in the current tenant
|
||||
* @responseBody 200 - {"data":[{"id":1,"display_name":"Ada Lovelace","email":"ada@example.test","phone":null,"status":"active"}]}
|
||||
* @responseBody 401 - {"message":"Unauthorized"}
|
||||
* @responseBody 400 - {"code":"E_TENANT_REQUIRED","message":"X-Tenant-Id header is required"}
|
||||
* @responseBody 403 - {"code":"E_TENANT_ACCESS_DENIED","message":"You do not have access to this tenant"}
|
||||
*/
|
||||
async index({ response, tenant }: HttpContext) {
|
||||
const contacts = await Contact.query().where('tenant_id', tenant.id).orderBy('id', 'asc')
|
||||
return response.ok({ data: contacts })
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Show a contact in the current tenant
|
||||
* @responseBody 200 - {"data":{"id":1,"display_name":"Katherine Johnson","email":"katherine@example.test","phone":null,"status":"active"}}
|
||||
* @responseBody 404 - {"message":"Not found."}
|
||||
*/
|
||||
async show({ request, response, tenant }: HttpContext) {
|
||||
const contact = await Contact.query()
|
||||
.where('id', request.param('id'))
|
||||
.where('tenant_id', tenant.id)
|
||||
.first()
|
||||
if (!contact) return response.notFound({ message: 'Not found.' })
|
||||
return response.ok({ data: contact })
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Update a contact in the current tenant
|
||||
* @responseBody 200 - {"data":{"id":1,"display_name":"Updated Name","email":"updated@example.test","phone":null,"status":"active"}}
|
||||
* @responseBody 404 - {"message":"Not found."}
|
||||
*/
|
||||
async update({ request, response, tenant }: HttpContext) {
|
||||
const contact = await Contact.query()
|
||||
.where('id', request.param('id'))
|
||||
.where('tenant_id', tenant.id)
|
||||
.first()
|
||||
if (!contact) return response.notFound({ message: 'Not found.' })
|
||||
const data = await updateContactValidator.validate({
|
||||
displayName: request.input('display_name'),
|
||||
email: request.input('email'),
|
||||
phone: request.input('phone'),
|
||||
status: request.input('status'),
|
||||
})
|
||||
contact.merge(data)
|
||||
await contact.save()
|
||||
return response.ok({ data: contact })
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Delete a contact in the current tenant
|
||||
* @responseBody 200 - {"message":"Deleted."}
|
||||
* @responseBody 404 - {"message":"Not found."}
|
||||
*/
|
||||
async destroy({ request, response, tenant }: HttpContext) {
|
||||
const contact = await Contact.query()
|
||||
.where('id', request.param('id'))
|
||||
.where('tenant_id', tenant.id)
|
||||
.first()
|
||||
if (!contact) return response.notFound({ message: 'Not found.' })
|
||||
await contact.delete()
|
||||
return response.ok({ message: 'Deleted.' })
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Create a contact in the current tenant
|
||||
* @responseBody 201 - {"data":{"id":1,"display_name":"Grace Hopper","email":"grace@example.test","phone":"+1-555-0100","status":"active"}}
|
||||
* @responseBody 401 - {"message":"Unauthorized"}
|
||||
* @responseBody 400 - {"code":"E_TENANT_REQUIRED","message":"X-Tenant-Id header is required"}
|
||||
* @responseBody 403 - {"code":"E_TENANT_ACCESS_DENIED","message":"You do not have access to this tenant"}
|
||||
*/
|
||||
async store({ request, response, tenant }: HttpContext) {
|
||||
const input = await createContactValidator.validate(request.all())
|
||||
const contact = await Contact.create({
|
||||
tenantId: tenant.id,
|
||||
displayName: input.display_name,
|
||||
email: input.email,
|
||||
phone: input.phone,
|
||||
status: 'active',
|
||||
})
|
||||
return response.created({ data: contact })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user