Skip to content
Snippets Groups Projects
Unverified Commit 3d56a818 authored by Ivan Alglave's avatar Ivan Alglave Committed by GitHub
Browse files

Merge pull request #3 from ivanalglave/get-people

Get people
parents e18a7bfb 4f9f8952
No related branches found
No related tags found
No related merge requests found
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose'; import { MongooseModule } from '@nestjs/mongoose';
import { mongodb } from './config'; import { mongodb } from './config';
import { PeopleModule } from './people/people.module';
import { GroupsModule } from './groups/groups.module'; import { GroupsModule } from './groups/groups.module';
@Module({ @Module({
imports: [GroupsModule, MongooseModule.forRoot(mongodb.uri)], imports: [PeopleModule, GroupsModule, MongooseModule.forRoot(mongodb.uri)],
}) })
export class AppModule {} export class AppModule {}
...@@ -13,4 +13,4 @@ async function bootstrap() { ...@@ -13,4 +13,4 @@ async function bootstrap() {
); );
await app.listen(server.port); await app.listen(server.port);
} }
bootstrap(); bootstrap();
\ No newline at end of file
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;
};
}
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
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
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
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);
}
}
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
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);
}
export type People = {
_id: any;
firstname: string,
lastname: string,
email: string,
passwordHash: string,
role: number,
};
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
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