diff --git a/src/app.module.ts b/src/app.module.ts index 2c1d559b9754cb0c4a18024daacc5881f1fc13c0..83454b396fbe455e342ac10495c8d9d035e20455 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { mongodb } from './config'; +import { PeopleModule } from './people/people.module'; import { GroupsModule } from './groups/groups.module'; @Module({ - imports: [GroupsModule, MongooseModule.forRoot(mongodb.uri)], + imports: [PeopleModule, GroupsModule, MongooseModule.forRoot(mongodb.uri)], }) export class AppModule {} diff --git a/src/main.ts b/src/main.ts index 3f2238f78398235ed9cd48408ee551a7df802e81..bf7f9ff096122e86e38387baf5d7b80092794f7b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,4 +13,4 @@ async function bootstrap() { ); await app.listen(server.port); } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/src/people/dao/people.dao.ts b/src/people/dao/people.dao.ts new file mode 100644 index 0000000000000000000000000000000000000000..7669cf6e0b137d221111d717fb4cff44a9182f61 --- /dev/null +++ b/src/people/dao/people.dao.ts @@ -0,0 +1,90 @@ +import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { randomInt } from 'crypto'; +import { Model } from 'mongoose'; +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 _peopleModel: Model<People>, + ) {} + + find = (): Promise<People[]> => + new Promise((resolve, reject) => { + this._peopleModel.find({}, {}, {}, (err, value) => { + if (err) reject(err.message); + if (!value) reject('No values'); + resolve(value); + }); + }); + + findById = (id: string): Promise<People | void> => + new Promise((resolve, reject) => { + this._peopleModel.findOne({ id: id }, {}, {}, (err, value) => { + if (err) reject(err.message); + if (!value) reject(new NotFoundException()); + resolve(value); + }); + }); + + save (people: CreatePeopleDto): Promise<People> { + var password = this.secret(); + people.passwordHash = password; + return new Promise((resolve, reject) => { + new this._peopleModel(people).save((err, value) => { + if (err) reject(err.message); + if (!value) reject(new InternalServerErrorException()); + resolve(value); + }); + }); + } + + findByIdAndUpdate = ( + id: string, + people: UpdatePeopleDto, + ): Promise<People | void> => + new Promise((resolve, reject) => { + this._peopleModel.updateOne( + { id: id }, + people, + { + new: true, + runValidators: true, + }, + (err, value) => { + if (err) reject(err.message); + if (value.matchedCount === 0) reject(new NotFoundException()); + resolve(value); + }, + ); + }); + + findByIdAndRemove = (id: string): Promise<People | void> => + new Promise((resolve, reject) => { + this._peopleModel.deleteOne({ id: id }, {}, (err) => { + if (err) reject(err.message); + resolve(); + }); + }); + + secret = (length = 10) => { + const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const lowerCase = "abcdefghijklmnopqrstuvwxyz"; + const digits = "0123456789"; + const minus = "-"; + const underLine = "_"; + const special = "!\"#$%&'*+,./:;=?@\\^`|~"; + const brackets = "[]{}()<>"; + const alphabet = + upperCase + lowerCase + digits + minus + underLine + special + brackets; + let secret = ""; + for (let index = 0; index < length; index++) + secret += alphabet.charAt(randomInt(alphabet.length)); + return secret; + }; + +} diff --git a/src/people/dto/create-people.dto.ts b/src/people/dto/create-people.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..c26bd35e1b55a7a6fd77eaa3b9049e358d7204e5 --- /dev/null +++ b/src/people/dto/create-people.dto.ts @@ -0,0 +1,23 @@ +import { IsBoolean, IsString, IsNotEmpty, IsOptional } from 'class-validator'; + +export class CreatePeopleDto { + + @IsString() + @IsNotEmpty() + firstname: string; + + @IsString() + @IsNotEmpty() + lastname: string; + + @IsOptional() + passwordHash: string; + + @IsString() + @IsNotEmpty() + email: string; + + @IsNotEmpty() + role: number; + +} \ No newline at end of file diff --git a/src/people/dto/update-people.dto.ts b/src/people/dto/update-people.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..4097f1e2b7647a6f1f8253e6f3690b404d60ab10 --- /dev/null +++ b/src/people/dto/update-people.dto.ts @@ -0,0 +1,23 @@ +import { IsOptional, IsString, IsNotEmpty } from 'class-validator'; + +export class UpdatePeopleDto { + @IsString() + @IsOptional() + firstname: string; + + @IsString() + @IsOptional() + lastname: string; + + @IsString() + @IsOptional() + email: string; + + @IsString() + @IsOptional() + passwordHash: string; + + @IsOptional() + role: number; + +} \ No newline at end of file diff --git a/src/people/entities/people.entity.ts b/src/people/entities/people.entity.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f8bf24a74e9c15f0f14d0e2e20e87fd75b746da --- /dev/null +++ b/src/people/entities/people.entity.ts @@ -0,0 +1,14 @@ +import { People } from '../schemas/people.schema'; + +export class PeopleEntity { + _id: string; + firstname: string; + lastname: string; + email: string; + passwordHash: string; + role: number; + + 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..8d40c28a36275093db88ba1b57c7a03ca670e677 --- /dev/null +++ b/src/people/people.controller.ts @@ -0,0 +1,50 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Param, + Body, + UseInterceptors, +} from '@nestjs/common'; +import { CreatePeopleDto } from './dto/create-people.dto'; +import { UpdatePeopleDto } from './dto/update-people.dto'; +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(): Promise<PeopleEntity[] | void> { + return this._peopleService.findAll(); + } + @Get(':id') + findOne(@Param() params: { id: string }): Promise<PeopleEntity | void> { + return this._peopleService.findOne(params.id); + } + + @Post() + create(@Body() createPeopleDto: CreatePeopleDto): Promise<PeopleEntity> { + return this._peopleService.create(createPeopleDto); + } + + @Put(':id') + update( + @Param() params: { id: string }, + @Body() updateGroupDto: UpdatePeopleDto, + ): Promise<PeopleEntity | void> { + return this._peopleService.update(params.id, updateGroupDto); + } + + @Delete(':id') + delete(@Param() params: { id: string }): Promise<PeopleEntity | void> { + return this._peopleService.delete(params.id); + } +} diff --git a/src/people/people.module.ts b/src/people/people.module.ts new file mode 100644 index 0000000000000000000000000000000000000000..e11d14169bdb3af7eedc37dc2419c5dad35a2849 --- /dev/null +++ b/src/people/people.module.ts @@ -0,0 +1,16 @@ +import { Logger, Module } from '@nestjs/common'; +import { MongooseModule } from '@nestjs/mongoose'; +import { PeopleDao } from './dao/people.dao'; +import { PeopleController } from './people.controller'; +import { PeopleService } from './people.service'; +import { People, PeopleSchema } from './schemas/people.schema'; + +@Module({ + imports: [ + MongooseModule.forFeature([{ name: People.name, schema: PeopleSchema }]), + ], + controllers: [PeopleController], + providers: [PeopleService, PeopleDao, Logger], + }) + + export class PeopleModule {} \ 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..e97344a5d049104065cd802bd97b5ba6e7bb5b4b --- /dev/null +++ b/src/people/people.service.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@nestjs/common'; +import { PeopleDao } from './dao/people.dao'; +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 = (): Promise<PeopleEntity[] | void> => this._peopleDao.find(); + + findOne = (id: string): Promise<PeopleEntity | void> => + this._peopleDao.findById(id); + + create = (people: CreatePeopleDto): Promise<PeopleEntity> => + this._peopleDao.save(people); + + update = ( + id: string, + people: UpdatePeopleDto, + ): Promise<PeopleEntity | void> => + this._peopleDao.findByIdAndUpdate(id, people); + + delete = (id: string): Promise<PeopleEntity | void> => + this._peopleDao.findByIdAndRemove(id); +} diff --git a/src/people/people.types.ts b/src/people/people.types.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b37d300d033287d21e09e613bc2e897683fd5d1 --- /dev/null +++ b/src/people/people.types.ts @@ -0,0 +1,8 @@ +export type People = { + _id: any; + firstname: string, + lastname: string, + email: string, + passwordHash: string, + role: number, + }; diff --git a/src/people/schemas/people.schema.ts b/src/people/schemas/people.schema.ts new file mode 100644 index 0000000000000000000000000000000000000000..09d9a5f8a4178a5f5d2050222b031acf016bb5fa --- /dev/null +++ b/src/people/schemas/people.schema.ts @@ -0,0 +1,58 @@ +import * as mongoose from 'mongoose'; +import { Document } from 'mongoose'; +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; + +export type PeopleDocument = People & Document; + +@Schema({ + toJSON: { + virtuals: true, + transform: (doc: any, ret: any) => { + delete ret._id; + }, + }, +}) +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, + trim: true, + minlength: 2, + }) passwordHash: string; + + @Prop({ + type: Number, + required: true, + }) role: number; + +} + +export const PeopleSchema = SchemaFactory.createForClass(People); + +PeopleSchema.index({}); \ No newline at end of file