Skip to content

Commit

Permalink
refactor: improve validation for search route and refactor test code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JadenKim-dev authored May 26, 2024
1 parent a103a02 commit 19db835
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 327 deletions.
38 changes: 16 additions & 22 deletions src/lib/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,23 @@ export class CrudService<T extends EntityType> {

readonly reservedReadMany = async (crudReadManyRequest: CrudReadManyRequest<T>): Promise<PaginationResponse<T>> => {
crudReadManyRequest.excludedColumns(this.columnNames);
try {
const { entities, total } = await (async () => {
const findEntities = this.repository.find({ ...crudReadManyRequest.findOptions });
const { entities, total } = await (async () => {
const findEntities = this.repository.find({ ...crudReadManyRequest.findOptions });

if (crudReadManyRequest.pagination.isNext) {
const entities = await findEntities;
return { entities, total: crudReadManyRequest.pagination.nextTotal() };
}
const [entities, total] = await Promise.all([
findEntities,
this.repository.count({
where: crudReadManyRequest.findOptions.where,
withDeleted: crudReadManyRequest.findOptions.withDeleted,
}),
]);
return { entities, total };
})();
return crudReadManyRequest.toResponse(entities, total);
} catch (error) {
Logger.error(JSON.stringify(crudReadManyRequest));
Logger.error(error);
throw error;
}
if (crudReadManyRequest.pagination.isNext) {
const entities = await findEntities;
return { entities, total: crudReadManyRequest.pagination.nextTotal() };
}
const [entities, total] = await Promise.all([
findEntities,
this.repository.count({
where: crudReadManyRequest.findOptions.where,
withDeleted: crudReadManyRequest.findOptions.withDeleted,
}),
]);
return { entities, total };
})();
return crudReadManyRequest.toResponse(entities, total);
};

readonly reservedReadOne = async (crudReadOneRequest: CrudReadOneRequest<T>): Promise<T> => {
Expand Down
Loading

0 comments on commit 19db835

Please sign in to comment.