Skip to content

Commit

Permalink
fix(controllers): Change mode for make query for db in loadAll to cre…
Browse files Browse the repository at this point in the history
…ateQueryBuilder
  • Loading branch information
EndyKaufman committed Apr 7, 2018
1 parent dcdaab8 commit f80c543
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
16 changes: 10 additions & 6 deletions src/libs/core/controllers/content-types.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { ApiBearerAuth, ApiImplicitParam, ApiImplicitQuery, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClass } from 'class-transformer';
import { Repository } from 'typeorm';
import { Repository, Like } from 'typeorm';

import { Roles } from '../decorators/roles.decorator';
import { InContentTypeDto } from '../dto/in-content-type.dto';
Expand Down Expand Up @@ -143,11 +143,15 @@ export class ContentTypesController {
@Query('q') q
) {
try {
const objects = await this.contentTypeRepository.findAndCount({
skip: (curPage - 1) * perPage,
take: perPage,
order: { id: 'DESC' }
});
let objects: [ContentType[], number];
let qb = this.contentTypeRepository.createQueryBuilder('contentType');
if (q){
qb = qb.where('contentType.name like :q or contentType.title like :q or contentType.id = :id', { q: `%${q}%`, id: +q });
}
qb = qb.orderBy('contentType.id', 'DESC');
qb = qb.skip((curPage - 1) * perPage)
.take(perPage);
objects = await qb.getManyAndCount();
return plainToClass(OutContentTypesDto, {
contentTypes: objects[0],
meta: {
Expand Down
19 changes: 12 additions & 7 deletions src/libs/core/controllers/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { ApiBearerAuth, ApiImplicitParam, ApiImplicitQuery, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClass } from 'class-transformer';
import { Repository } from 'typeorm';
import { Repository, Like } from 'typeorm';

import { Roles } from '../decorators/roles.decorator';
import { InGroupDto } from '../dto/in-group.dto';
Expand Down Expand Up @@ -152,12 +152,17 @@ export class GroupsController {
@Query('q') q
) {
try {
const objects = await this.groupsRepository.findAndCount({
skip: (curPage - 1) * perPage,
take: perPage,
relations: ['permissions'],
order: { id: 'DESC' }
});
let objects: [Group[], number];
let qb = this.groupsRepository.createQueryBuilder('group');
qb = qb.leftJoinAndSelect('group.permissions', 'permission');
qb = qb.leftJoinAndSelect('permission.contentType', 'contentType');
if (q){
qb = qb.where('group.title like :q or group.name like :q or group.id = :id', { q: `%${q}%`, id: +q });
}
qb = qb.orderBy('group.id', 'DESC');
qb = qb.skip((curPage - 1) * perPage)
.take(perPage);
objects = await qb.getManyAndCount();
return plainToClass(OutGroupsDto, {
groups: objects[0],
meta: {
Expand Down
3 changes: 3 additions & 0 deletions src/libs/core/controllers/permissions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ export class PermissionsController {
.leftJoin('permission.groups', 'group')
.where('group.id = :group', { group: group });
}
if (q){
qb = qb.where('permission.name like :q or permission.title like :q or permission.id = :id', { q: `%${q}%`, id: +q });
}
if (contentType) {
qb = qb.where('contentType.id = :contentType', { contentType: contentType });
}
Expand Down
36 changes: 18 additions & 18 deletions src/libs/core/controllers/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { ApiBearerAuth, ApiImplicitParam, ApiImplicitQuery, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClass } from 'class-transformer';
import { Repository } from 'typeorm';
import { Repository, Like } from 'typeorm';

import { Roles } from '../decorators/roles.decorator';
import { InUserDto } from '../dto/in-user.dto';
Expand Down Expand Up @@ -161,24 +161,24 @@ export class UsersController {
) {
try {
let objects: [User[], number];
if (!group) {
objects = await this.usersRepository.findAndCount({
skip: (curPage - 1) * perPage,
take: perPage,
relations: ['groups', 'groups.permissions'],
order: { id: 'DESC' }
});
} else {
let qb = this.usersRepository.createQueryBuilder('user');
if (group) {
qb = qb.leftJoinAndSelect('user.groups', 'group')
.where('group.id = :group', { group: group })
}
qb = qb.orderBy('user.id', 'DESC');
qb = qb.skip((curPage - 1) * perPage)
.take(perPage);
objects = await qb.getManyAndCount();
let qb = this.usersRepository.createQueryBuilder('user');
if (group) {
qb = qb.leftJoinAndSelect('user.groups', 'group')
.where('group.id = :group', { group: group })
}else{
qb = qb.leftJoinAndSelect('user.groups', 'group')
.where('group.id = :group', { group: group });
qb = qb.leftJoinAndSelect('groups.permissions', 'permission');
qb = qb.leftJoinAndSelect('permission.contentType', 'contentType');
}
if (q){
qb = qb.where('user.first_name like :q or user.last_name like :q or user.username like :q or user.id = :id', { q: `%${q}%`, id: +q });
}
qb = qb.orderBy('user.id', 'DESC');
qb = qb.skip((curPage - 1) * perPage)
.take(perPage);
objects = await qb.getManyAndCount();

return plainToClass(OutUsersDto, {
users: objects[0],
meta: {
Expand Down

0 comments on commit f80c543

Please sign in to comment.