tethys.backend/app/Controllers/Http/Api/AuthorsController.ts

36 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2024-03-14 19:25:27 +00:00
import type { HttpContext } from '@adonisjs/core/http';
import Person from '#models/person';
2023-03-03 15:54:28 +00:00
// import Dataset from 'App/Models/Dataset';
// node ace make:controller Author
export default class AuthorsController {
2024-03-14 19:25:27 +00:00
public async index({}: HttpContext) {
2023-03-17 15:13:37 +00:00
// select * from gba.persons
// where exists (select * from gba.documents inner join gba.link_documents_persons on "documents"."id" = "link_documents_persons"."document_id"
// where ("link_documents_persons"."role" = 'author') and ("persons"."id" = "link_documents_persons"."person_id"));
const authors = await Person.query()
.whereHas('datasets', (dQuery) => {
dQuery.wherePivot('role', 'author');
})
.withCount('datasets', (query) => {
query.as('datasets_count');
});
2023-03-03 15:54:28 +00:00
2023-03-17 15:13:37 +00:00
return authors;
}
2023-03-03 15:54:28 +00:00
2024-03-14 19:25:27 +00:00
public async persons({ request }: HttpContext) {
2023-03-17 15:13:37 +00:00
const authors = Person.query().where('status', true);
if (request.input('filter')) {
// users = users.whereRaw('name like %?%', [request.input('search')])
const searchTerm = request.input('filter');
authors.whereILike('first_name', `%${searchTerm}%`).orWhereILike('last_name', `%${searchTerm}%`);
// .orWhere('email', 'like', `%${searchTerm}%`);
2023-03-17 15:13:37 +00:00
}
let persons = await authors;
return persons;
}
2023-03-03 15:54:28 +00:00
}