diff --git a/src/people/dao/people.dao.ts b/src/people/dao/people.dao.ts new file mode 100644 index 0000000000000000000000000000000000000000..273eb67f65ffb09c99cacc0cb9ec7d2bacfa63d3 --- /dev/null +++ b/src/people/dao/people.dao.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { from, map, Observable } from 'rxjs'; +/*import { CreatePeopleDto } from '../dto/create-people.dto'; +import { UpdatePeopleDto } from '../dto/update-people.dto';*/ +import { People } from '../schemas/people.schema'; + +@Injectable() +export class PeopleDao { + constructor( + @InjectModel(People.name) + private readonly _groupModel: Model<People>, + ) {} + + find = (): Observable<People[]> => + from(this._groupModel.find({})).pipe(map((people) => [].concat(people))); + +} \ No newline at end of file diff --git a/src/people/dto/create-people.dto.ts b/src/people/dto/create-people.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/people/entities/people.entity.ts b/src/people/entities/people.entity.ts new file mode 100644 index 0000000000000000000000000000000000000000..4b2a04f7d7290e4a2073b2ca8a6179d553180d18 --- /dev/null +++ b/src/people/entities/people.entity.ts @@ -0,0 +1,8 @@ +import { People } from '../schemas/people.schema'; + +export class PeopleEntity { + + constructor(partial: Partial<People>) { + Object.assign(this, partial); + } +} \ No newline at end of file diff --git a/src/people/people.controller.ts b/src/people/people.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..a24ccc5081c7ea9697acca9c0eca57b1670e7f89 --- /dev/null +++ b/src/people/people.controller.ts @@ -0,0 +1,29 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Param, + Body, + UseInterceptors, + } from '@nestjs/common'; + import { Observable } from 'rxjs'; +// import { HttpInterceptor } from '../interceptors/http.interceptor'; +// import { CreatePeopleDto } from './dto/create-people.dto'; +// import { UpdatePeopleDto } from './dto/update-people.dto'; + import { PeopleEntity } from './entities/people.entity'; + import { PeopleService } from './people.service'; + + + @Controller('people') +// @UseInterceptors(HttpInterceptor) + export class PeopleController { + constructor(private readonly _peopleService: PeopleService) {} + + @Get() + findAll(): Observable<PeopleEntity[] | void> { + return this._peopleService.findAll(); + } + + } \ No newline at end of file diff --git a/src/people/people.service.ts b/src/people/people.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..41a31db625ccf28f069f66f070628421af51c460 --- /dev/null +++ b/src/people/people.service.ts @@ -0,0 +1,34 @@ +import { + Injectable, + UnprocessableEntityException, + NotFoundException, + ConflictException, + } from '@nestjs/common'; + import { + Observable, + of, + filter, + map, + mergeMap, + defaultIfEmpty, + catchError, + throwError, + } from 'rxjs'; +import { PeopleDao } from './dao/people.dao'; +// import { HttpInterceptor } from '../interceptors/http.interceptor'; +// import { CreatePeopleDto } from './dto/create-people.dto'; +// import { UpdatePeopleDto } from './dto/update-people.dto'; +import { PeopleEntity } from './entities/people.entity'; + + + @Injectable() + export class PeopleService { + constructor(private readonly _peopleDao: PeopleDao) {} + + findAll = (): Observable<PeopleEntity[] | void> => + this._peopleDao.find().pipe( + filter(Boolean), + map((people) => (people || []).map((person) => new PeopleEntity(person))), + defaultIfEmpty(undefined), + ); +} \ No newline at end of file diff --git a/src/people/people.types.ts b/src/people/people.types.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e768bb6722e8a0cb548809b084fb630629737ee --- /dev/null +++ b/src/people/people.types.ts @@ -0,0 +1,9 @@ +export type Group = { + _id: any; + firstname: string, + lastname: string, + email: string, + passwordHash: string, + role: number, + groups: string[] + }; diff --git a/src/people/schemas/people.schema.ts b/src/people/schemas/people.schema.ts new file mode 100644 index 0000000000000000000000000000000000000000..fe0f91cddf0bf8c3f48fca994b10b4452f971ab2 --- /dev/null +++ b/src/people/schemas/people.schema.ts @@ -0,0 +1,64 @@ +import { + Prop, raw, Schema, SchemaFactory, +} from '@nestjs/mongoose'; +import * as mongoose from 'mongoose'; + +@Schema({ + toJSON: { + virtuals: true, + transform: (dpc: any, ret: any) => { + delete ret._id; + }, + }, + versionKey: false, + +}) +export class People { + @Prop({ + type: mongoose.Schema.Types.ObjectId, + auto: true, + }) _id: any; + + @Prop({ + type: String, + required: true, + trim: true, + minlength: 2, + }) firstname: string; + + @Prop({ + type: String, + required: true, + trim: true, + minlength: 2, + }) lastname: string; + + @Prop({ + type: String, + required: true, + trim: true, + minlength: 2, + }) email: string; + + @Prop({ + type: String, + required: true, + trim: true, + minlength: 2, + }) passwordHash: string; + + @Prop({ + type: Number, + required: true, + }) role: string; + + @Prop({ + type: [String], + required: true, + }) groups: string[]; + +} + +export const PeopleSchema = SchemaFactory.createForClass(People); + +PeopleSchema.index({ name: 1 }, { unique: true }); \ No newline at end of file