Skip to content
Snippets Groups Projects
Commit 1184295c authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat: parse boolean string to boolean for search filter + Refacto

parent 609b51b8
No related branches found
No related tags found
No related merge requests found
...@@ -8,12 +8,12 @@ export class CategoriesAccompagnementController { ...@@ -8,12 +8,12 @@ export class CategoriesAccompagnementController {
constructor(private readonly categoriesAccompagnementService: CategoriesAccompagnementService) {} constructor(private readonly categoriesAccompagnementService: CategoriesAccompagnementService) {}
@Post() @Post()
async create(@Body() createStructureDto: CreateCategoriesAccompagnement) { public async create(@Body() createStructureDto: CreateCategoriesAccompagnement) {
await this.categoriesAccompagnementService.create(createStructureDto); await this.categoriesAccompagnementService.create(createStructureDto);
} }
@Get() @Get()
async findAll(): Promise<CategoriesAccompagnement[]> { public async findAll(): Promise<CategoriesAccompagnement[]> {
return this.categoriesAccompagnementService.findAll(); return this.categoriesAccompagnementService.findAll();
} }
} }
...@@ -8,12 +8,12 @@ export class CategoriesFormationsController { ...@@ -8,12 +8,12 @@ export class CategoriesFormationsController {
constructor(private readonly categoriesFormationsService: CategoriesFormationsService) {} constructor(private readonly categoriesFormationsService: CategoriesFormationsService) {}
@Post() @Post()
async create(@Body() createStructureDto: CreateCategoriesFormations) { public async create(@Body() createStructureDto: CreateCategoriesFormations) {
await this.categoriesFormationsService.create(createStructureDto); await this.categoriesFormationsService.create(createStructureDto);
} }
@Get() @Get()
async findAll(): Promise<CategoriesFormations[]> { public async findAll(): Promise<CategoriesFormations[]> {
return this.categoriesFormationsService.findAll(); return this.categoriesFormationsService.findAll();
} }
} }
...@@ -8,12 +8,12 @@ export class CategoriesOthersController { ...@@ -8,12 +8,12 @@ export class CategoriesOthersController {
constructor(private readonly categoriesOthersService: CategoriesOthersService) {} constructor(private readonly categoriesOthersService: CategoriesOthersService) {}
@Post() @Post()
async create(@Body() createStructureDto: CreateCategoriesOthers) { public async create(@Body() createStructureDto: CreateCategoriesOthers) {
await this.categoriesOthersService.create(createStructureDto); await this.categoriesOthersService.create(createStructureDto);
} }
@Get() @Get()
async findAll(): Promise<CategoriesOthers[]> { public async findAll(): Promise<CategoriesOthers[]> {
return this.categoriesOthersService.findAll(); return this.categoriesOthersService.findAll();
} }
} }
...@@ -10,12 +10,12 @@ export class CategoriesAccompagnementService { ...@@ -10,12 +10,12 @@ export class CategoriesAccompagnementService {
@InjectModel(CategoriesAccompagnement.name) private structureModel: Model<CategoriesAccompagnementDocument> @InjectModel(CategoriesAccompagnement.name) private structureModel: Model<CategoriesAccompagnementDocument>
) {} ) {}
async create(createDto: CreateCategoriesAccompagnement): Promise<CategoriesAccompagnement> { public async create(createDto: CreateCategoriesAccompagnement): Promise<CategoriesAccompagnement> {
const createdStructure = new this.structureModel(createDto); const createdStructure = new this.structureModel(createDto);
return createdStructure.save(); return createdStructure.save();
} }
async findAll(): Promise<CategoriesAccompagnement[]> { public async findAll(): Promise<CategoriesAccompagnement[]> {
return this.structureModel.find().exec(); return this.structureModel.find().exec();
} }
} }
...@@ -8,12 +8,12 @@ import { CategoriesFormations, CategoriesFormationsDocument } from '../schemas/c ...@@ -8,12 +8,12 @@ import { CategoriesFormations, CategoriesFormationsDocument } from '../schemas/c
export class CategoriesFormationsService { export class CategoriesFormationsService {
constructor(@InjectModel(CategoriesFormations.name) private structureModel: Model<CategoriesFormationsDocument>) {} constructor(@InjectModel(CategoriesFormations.name) private structureModel: Model<CategoriesFormationsDocument>) {}
async create(createDto: CreateCategoriesFormations): Promise<CategoriesFormations> { public async create(createDto: CreateCategoriesFormations): Promise<CategoriesFormations> {
const createdStructure = new this.structureModel(createDto); const createdStructure = new this.structureModel(createDto);
return createdStructure.save(); return createdStructure.save();
} }
async findAll(): Promise<CategoriesFormations[]> { public async findAll(): Promise<CategoriesFormations[]> {
return this.structureModel.find().exec(); return this.structureModel.find().exec();
} }
} }
...@@ -8,12 +8,12 @@ import { CategoriesOthers, CategoriesOthersDocument } from '../schemas/categorie ...@@ -8,12 +8,12 @@ import { CategoriesOthers, CategoriesOthersDocument } from '../schemas/categorie
export class CategoriesOthersService { export class CategoriesOthersService {
constructor(@InjectModel(CategoriesOthers.name) private structureModel: Model<CategoriesOthersDocument>) {} constructor(@InjectModel(CategoriesOthers.name) private structureModel: Model<CategoriesOthersDocument>) {}
async create(createDto: CreateCategoriesOthers): Promise<CategoriesOthers> { public async create(createDto: CreateCategoriesOthers): Promise<CategoriesOthers> {
const createdStructure = new this.structureModel(createDto); const createdStructure = new this.structureModel(createDto);
return createdStructure.save(); return createdStructure.save();
} }
async findAll(): Promise<CategoriesOthers[]> { public async findAll(): Promise<CategoriesOthers[]> {
return this.structureModel.find().exec(); return this.structureModel.find().exec();
} }
} }
import { Filter } from '../schemas/filter.schema';
export class QueryStructure {
query: string;
filters: Filter[];
}
import { SchemaFactory } from '@nestjs/mongoose';
export type FilterDocument = Filter & Document;
export class Filter {
name: string;
value: string | boolean;
}
export const FilterSchema = SchemaFactory.createForClass(Filter);
import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { Body, Controller, Get, Param, Post, Query, Req } from '@nestjs/common';
import { CreateStructureDto } from './dto/create-structure.dto'; import { CreateStructureDto } from './dto/create-structure.dto';
import { QueryStructure } from './dto/query-structure.dto';
import { Structure } from './schemas/structure.schema'; import { Structure } from './schemas/structure.schema';
import { StructuresService } from './structures.service'; import { StructuresService } from './structures.service';
...@@ -8,22 +9,22 @@ export class StructuresController { ...@@ -8,22 +9,22 @@ export class StructuresController {
constructor(private readonly structureService: StructuresService) {} constructor(private readonly structureService: StructuresService) {}
@Post() @Post()
async create(@Body() createStructureDto: CreateStructureDto) { public async create(@Body() createStructureDto: CreateStructureDto) {
await this.structureService.create(createStructureDto); await this.structureService.create(createStructureDto);
} }
@Get() @Post('search')
async findAll(): Promise<Structure[]> { public async search(@Query() query: QueryStructure, @Body() body): Promise<Structure[]> {
return this.structureService.findAll(); return this.structureService.search(query.query, body ? body.filters : null);
} }
@Get(':text') @Get()
async search(@Param('text') text: string): Promise<Structure[]> { public async findAll(): Promise<Structure[]> {
return this.structureService.search(text); return this.structureService.findAll();
} }
@Get('count') @Get('count')
async countCategories(): Promise<Array<{ id: string; count: number }>> { public async countCategories(): Promise<Array<{ id: string; count: number }>> {
const data = await Promise.all([ const data = await Promise.all([
this.structureService.countByStructureKey('accesAuxDroits'), this.structureService.countByStructureKey('accesAuxDroits'),
this.structureService.countByStructureKey('aideALaParentalite'), this.structureService.countByStructureKey('aideALaParentalite'),
...@@ -34,11 +35,11 @@ export class StructuresController { ...@@ -34,11 +35,11 @@ export class StructuresController {
this.structureService.countByStructureKey('publicsAcceptes'), this.structureService.countByStructureKey('publicsAcceptes'),
this.structureService.countByStructureKey('modalitesDacces'), this.structureService.countByStructureKey('modalitesDacces'),
this.structureService.countByStructureKey('lesCompetencesDeBase'), this.structureService.countByStructureKey('lesCompetencesDeBase'),
this.structureService.countByEquipmentsKey('wifiEnAccesLibre'), this.structureService.countByEquipmentsKey('wifiEnAccesLibre', 'Wifi en accès libre'),
this.structureService.countByEquipmentsKey('ordinateurs'), this.structureService.countByEquipmentsKey('ordinateurs', 'Ordinateurs'),
this.structureService.countByEquipmentsKey('tablettes'), this.structureService.countByEquipmentsKey('tablettes', 'Tablettes'),
this.structureService.countByEquipmentsKey('bornesNumeriques'), this.structureService.countByEquipmentsKey('bornesNumeriques', 'Bornes numériques'),
this.structureService.countByEquipmentsKey('imprimantes'), this.structureService.countByEquipmentsKey('imprimantes', 'Imprimantes'),
]); ]);
// Return a concat of all arrays // Return a concat of all arrays
return data.reduce((a, b) => [...a, ...b]); return data.reduce((a, b) => [...a, ...b]);
......
...@@ -2,22 +2,46 @@ import { Injectable } from '@nestjs/common'; ...@@ -2,22 +2,46 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { CreateStructureDto } from './dto/create-structure.dto'; import { CreateStructureDto } from './dto/create-structure.dto';
import { Filter } from './schemas/filter.schema';
import { Structure, StructureDocument } from './schemas/structure.schema'; import { Structure, StructureDocument } from './schemas/structure.schema';
@Injectable() @Injectable()
export class StructuresService { export class StructuresService {
constructor(@InjectModel(Structure.name) private structureModel: Model<StructureDocument>) {} constructor(@InjectModel(Structure.name) private structureModel: Model<StructureDocument>) {}
async create(createStructrureDto: CreateStructureDto): Promise<Structure> { public async create(createStructrureDto: CreateStructureDto): Promise<Structure> {
const createdStructure = new this.structureModel(createStructrureDto); const createdStructure = new this.structureModel(createStructrureDto);
return createdStructure.save(); return createdStructure.save();
} }
async search(searchString: string): Promise<Structure[]> { public async search(searchString: string, filters?: Array<any>): Promise<Structure[]> {
return this.structureModel.find({ $text: { $search: searchString } }).exec(); let query: any;
if (searchString && filters) {
query = [...this.parseFilter(filters), { $text: { $search: searchString } }];
} else if (filters) {
query = this.parseFilter(filters);
} else {
query = [{ $text: { $search: searchString } }];
}
return this.structureModel.find({ $and: query }).exec();
} }
async findAll(): Promise<Structure[]> { /**
* Parse filter value from string to boolean
* @param filters
*/
private parseFilter(filters: Array<any>): Array<any> {
return filters.map((filter) => {
const key = Object.keys(filter)[0];
if (filter[key] === 'True') {
return { [key]: true };
} else {
return filter;
}
});
}
public async findAll(): Promise<Structure[]> {
return this.structureModel.find().exec(); return this.structureModel.find().exec();
} }
...@@ -26,7 +50,7 @@ export class StructuresService { ...@@ -26,7 +50,7 @@ export class StructuresService {
* @param key structure key * @param key structure key
* @return [{id: 'key', count: 'value'}] * @return [{id: 'key', count: 'value'}]
*/ */
async countByStructureKey(key: string): Promise<any> { public async countByStructureKey(key: string): Promise<any> {
const uniqueElements = await this.structureModel.distinct(key).exec(); const uniqueElements = await this.structureModel.distinct(key).exec();
return await Promise.all( return await Promise.all(
uniqueElements.map(async (value) => { uniqueElements.map(async (value) => {
...@@ -38,7 +62,7 @@ export class StructuresService { ...@@ -38,7 +62,7 @@ export class StructuresService {
); );
} }
async countByEquipmentsKey(key: string): Promise<any> { public async countByEquipmentsKey(key: string, displayKey: string): Promise<any> {
return [{ id: key, count: await this.structureModel.countDocuments({ [key]: true }).exec() }]; return [{ id: displayKey, count: await this.structureModel.countDocuments({ [key]: true }).exec() }];
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment