diff --git a/src/groups/dao/groups.dao.ts b/src/groups/dao/groups.dao.ts index e4e557f361aedaaff8dd190cf9ab53781e0f49f7..885b2f5acb163b7710d70e1cba7629743a453a55 100644 --- a/src/groups/dao/groups.dao.ts +++ b/src/groups/dao/groups.dao.ts @@ -1,7 +1,6 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; -import { from, map, Observable } from 'rxjs'; import { CreateGroupDto } from '../dto/create-group.dto'; import { UpdateGroupDto } from '../dto/update-group.dto'; import { Group } from '../schemas/group.schema'; @@ -21,23 +20,46 @@ export class GroupsDao { }); }); - findById = (id: string): Observable<Group | void> => - from(this._groupModel.findById(id)); + findById = (id: string): Promise<Group | void> => + new Promise((resolve, reject) => { + this._groupModel.findOne({ id: id }, {}, {}, (err, value) => { + if (err) reject(err.message); + resolve(value); + }); + }); - save = (group: CreateGroupDto): Observable<Group> => - from(new this._groupModel(group).save()); + save = (group: CreateGroupDto): Promise<Group> => + new Promise((resolve, reject) => { + new this._groupModel(group).save((err, value) => { + if (err) reject(err.message); + resolve(value); + }); + }); findByIdAndUpdate = ( id: string, group: UpdateGroupDto, - ): Observable<Group | void> => - from( - this._groupModel.findByIdAndUpdate(id, group, { - new: true, - runValidators: true, - }), - ); + ): Promise<Group | void> => + new Promise((resolve, reject) => { + this._groupModel.updateOne( + { id: id }, + group, + { + new: true, + runValidators: true, + }, + (err, value) => { + if (err) reject(err.message); + resolve(value); + }, + ); + }); - findByIdAndRemove = (id: string): Observable<Group | void> => - from(this._groupModel.findByIdAndRemove(id)); + findByIdAndRemove = (id: string): Promise<Group | void> => + new Promise((resolve, reject) => { + this._groupModel.deleteOne({ id: id }, {}, (err) => { + if (err) reject(err.message); + resolve(); + }); + }); } diff --git a/src/groups/dto/create-group.dto.ts b/src/groups/dto/create-group.dto.ts index 44a55e1f531a6c8ac1f90d1ef932e5030105473d..2444a73d19142155a47349b19c99240fd0cd34be 100644 --- a/src/groups/dto/create-group.dto.ts +++ b/src/groups/dto/create-group.dto.ts @@ -1,4 +1,4 @@ -import { IsBoolean, IsString, IsNotEmpty, IsMongoId } from 'class-validator'; +import { IsBoolean, IsString, IsNotEmpty } from 'class-validator'; export class CreateGroupDto { @IsString() @@ -8,7 +8,7 @@ export class CreateGroupDto { @IsBoolean() final: boolean; - @IsMongoId() + @IsString() @IsNotEmpty() parent: any; } diff --git a/src/groups/dto/update-group.dto.ts b/src/groups/dto/update-group.dto.ts index 954e8525bd1ae8bfec355690699cc1c47617395d..6b7f72557fad39fd2b38285b30ede6cc721a720f 100644 --- a/src/groups/dto/update-group.dto.ts +++ b/src/groups/dto/update-group.dto.ts @@ -1,14 +1,15 @@ -import { IsBoolean, IsString, IsNotEmpty, IsMongoId } from 'class-validator'; +import { IsArray, IsOptional } from 'class-validator'; export class UpdateGroupDto { - @IsString() - @IsNotEmpty() - id: string; + @IsArray() + @IsOptional() + responsibles: string[]; - @IsBoolean() - final: boolean; + @IsArray() + @IsOptional() + secretaries: string[]; - @IsMongoId() - @IsNotEmpty() - parent: string; + @IsArray() + @IsOptional() + students: string[]; } diff --git a/src/groups/groups.controller.ts b/src/groups/groups.controller.ts index 5eff7b8b3453ca71a19625a70adca3cf6114f7d3..25314334873cee15f51c693e8720b26165a3be97 100644 --- a/src/groups/groups.controller.ts +++ b/src/groups/groups.controller.ts @@ -26,12 +26,12 @@ export class GroupsController { } @Get(':id') - findOne(@Param() params: { id: string }): Observable<GroupEntity> { + findOne(@Param() params: { id: string }): Promise<GroupEntity | void> { return this._groupsService.findOne(params.id); } @Post() - create(@Body() createGroupDto: CreateGroupDto): Observable<GroupEntity> { + create(@Body() createGroupDto: CreateGroupDto): Promise<GroupEntity> { return this._groupsService.create(createGroupDto); } @@ -39,12 +39,12 @@ export class GroupsController { update( @Param() params: { id: string }, @Body() updateGroupDto: UpdateGroupDto, - ): Observable<GroupEntity> { + ): Promise<GroupEntity | void> { return this._groupsService.update(params.id, updateGroupDto); } @Delete(':id') - delete(@Param() params: { id: string }): Observable<void> { + delete(@Param() params: { id: string }): Promise<GroupEntity | void> { return this._groupsService.delete(params.id); } } diff --git a/src/groups/groups.service.ts b/src/groups/groups.service.ts index dc8cf5da08d3ec25c435cdc282ac1210b37ab621..7410d687762ce84cde9923e6b14abd8ca29be307 100644 --- a/src/groups/groups.service.ts +++ b/src/groups/groups.service.ts @@ -1,19 +1,4 @@ -import { - Injectable, - UnprocessableEntityException, - NotFoundException, - ConflictException, -} from '@nestjs/common'; -import { - Observable, - of, - filter, - map, - mergeMap, - defaultIfEmpty, - catchError, - throwError, -} from 'rxjs'; +import { Injectable } from '@nestjs/common'; import { GroupsDao } from './dao/groups.dao'; import { CreateGroupDto } from './dto/create-group.dto'; import { UpdateGroupDto } from './dto/update-group.dto'; @@ -25,80 +10,15 @@ export class GroupsService { findAll = (): Promise<GroupEntity[] | void> => this._groupsDao.find(); - findOne = (id: string): Observable<GroupEntity> => - this._groupsDao.findById(id).pipe( - catchError((e) => - throwError(() => new UnprocessableEntityException(e.message)), - ), - mergeMap((group) => - !!group - ? of(new GroupEntity(group)) - : throwError( - () => new NotFoundException(`Group with id ${id} not found`), - ), - ), - ); + findOne = (id: string): Promise<GroupEntity | void> => + this._groupsDao.findById(id); - create = (group: CreateGroupDto): Observable<GroupEntity> => - this._prepareNewGroup(group).pipe( - mergeMap((newPreparedGroup: CreateGroupDto) => - this._groupsDao.save(newPreparedGroup), - ), - catchError((e) => - e.code === 11000 - ? throwError( - () => - new ConflictException( - `Group with id ${group.id} already exists`, - ), - ) - : throwError(() => new UnprocessableEntityException(e.message)), - ), - map((groupCreated) => new GroupEntity(groupCreated)), - ); + create = (group: CreateGroupDto): Promise<GroupEntity> => + this._groupsDao.save(group); - update = (id: string, group: UpdateGroupDto): Observable<GroupEntity> => - this._groupsDao.findByIdAndUpdate(id, group).pipe( - catchError((e) => - e.code === 11000 - ? throwError( - () => - new ConflictException( - `Group with id ${group.id} already exists`, - ), - ) - : throwError(() => new UnprocessableEntityException(e.message)), - ), - mergeMap((groupUpdated) => - !!groupUpdated - ? of(new GroupEntity(groupUpdated)) - : throwError( - () => new NotFoundException(`Group with id '${id}' not found`), - ), - ), - ); + update = (id: string, group: UpdateGroupDto): Promise<GroupEntity | void> => + this._groupsDao.findByIdAndUpdate(id, group); - delete = (id: string): Observable<void> => - this._groupsDao.findByIdAndRemove(id).pipe( - catchError((e) => - throwError(() => new UnprocessableEntityException(e.message)), - ), - mergeMap((groupDeleted) => - !!groupDeleted - ? of(undefined) - : throwError( - () => new NotFoundException(`Group with id '${id}' not found`), - ), - ), - ); - - private _prepareNewGroup = ( - group: CreateGroupDto, - ): Observable<CreateGroupDto> => - of({ - ...group, - responsibles: [], - secretaries: [], - students: [], - }); + delete = (id: string): Promise<GroupEntity | void> => + this._groupsDao.findByIdAndRemove(id); } diff --git a/src/groups/schemas/group.schema.ts b/src/groups/schemas/group.schema.ts index c8ee51ecb15cf7834bcc8a66a1f351ae626aa78c..93698e96bb616bd8c44b0c7e517e654f721cdcc4 100644 --- a/src/groups/schemas/group.schema.ts +++ b/src/groups/schemas/group.schema.ts @@ -1,6 +1,6 @@ import * as mongoose from 'mongoose'; import { Document } from 'mongoose'; -import { Prop, raw, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; export type GroupDocument = Group & Document; @@ -34,28 +34,28 @@ export class Group { final: boolean; @Prop({ - type: [mongoose.Schema.Types.ObjectId], + type: [String], required: true, trim: true, }) responsibles: string[]; @Prop({ - type: [mongoose.Schema.Types.ObjectId], + type: [String], required: true, trim: true, }) secretaries: string[]; @Prop({ - type: [mongoose.Schema.Types.ObjectId], + type: [String], required: true, trim: true, }) students: string[]; @Prop({ - type: mongoose.Schema.Types.ObjectId, + type: String, required: true, trim: true, }) diff --git a/src/main.ts b/src/main.ts index a946614e106906a5e7958b5f284df1e53e6ad116..3f2238f78398235ed9cd48408ee551a7df802e81 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,16 @@ +import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { server } from './config'; async function bootstrap() { const app = await NestFactory.create(AppModule); + await app.useGlobalPipes( + new ValidationPipe({ + whitelist: true, + forbidNonWhitelisted: true, + }), + ); await app.listen(server.port); } bootstrap();