diff --git a/backend/src/route/dto/updateRouteStatus.dto.ts b/backend/src/route/dto/updateRouteStatus.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..fba15c88618330bddd6030c76074f5276359d16d --- /dev/null +++ b/backend/src/route/dto/updateRouteStatus.dto.ts @@ -0,0 +1,7 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { RouteStatus } from '../entities/route.entity'; + +export class UpdateRouteStatusDto { + @ApiProperty({ enum: RouteStatus, enumName: 'RouteStatus', description: 'accepted, pending, rejected' }) + status: RouteStatus; +} diff --git a/backend/src/route/entities/route.entity.ts b/backend/src/route/entities/route.entity.ts index 30f9a975a811357c9b4ec29d07c66574b000629f..566b3da51cf37fbd8f85f7cfc920a939f736c7b6 100644 --- a/backend/src/route/entities/route.entity.ts +++ b/backend/src/route/entities/route.entity.ts @@ -2,7 +2,7 @@ import { Town } from 'src/town/entities/town.entity'; import { TravelPlace } from 'src/travel-place/entities/travel-place.entity'; import { User } from 'src/user/entities/user.entity'; import { PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Entity, OneToMany } from 'typeorm'; -enum RouteStatus { +export enum RouteStatus { PENDING = 'pending', ACCEPTED = 'accepted', REJECTED = 'rejected', diff --git a/backend/src/route/route.controller.ts b/backend/src/route/route.controller.ts index 08591cb860c0ec0a4a6b893ecdf309aa518c14d2..7586762fdaa7f6d15f83b3e6114f920efed932c3 100644 --- a/backend/src/route/route.controller.ts +++ b/backend/src/route/route.controller.ts @@ -1,18 +1,19 @@ -import { Controller, Param, UseGuards, Req, Query, Body, Post, Get } from '@nestjs/common'; +import { Controller, Param, UseGuards, Req, Query, Body, Post, Get, Patch } from '@nestjs/common'; import { RouteService } from './route.service'; import { ApiBearerAuth, ApiBody, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; import { LANGUAGES } from 'src/shared/enum/languages.enum'; import { AuthUserGuard } from 'src/auth/user/authUser.guard'; import { CustomUserRequest } from 'src/auth/user/interface/customUserReq'; import { CreateRouteReq } from './dto/create-route-req'; +import { RouteStatus } from './entities/route.entity'; +import { UpdateRouteStatusDto } from './dto/updateRouteStatus.dto'; @Controller('route') @ApiTags('route') export class RouteController { constructor(private readonly routeService: RouteService) {} - @Post('recommend/:idTown') - @ApiQuery({ name: 'lang', type: String }) + @Post('/:idTown') @ApiParam({ name: 'idTown', type: Number }) @ApiBody({ type: CreateRouteReq }) @ApiBearerAuth('jwt') @@ -33,13 +34,30 @@ export class RouteController { ); } - @Get('recommend/:idTown') - @ApiQuery({ name: 'lang', type: String }) - @ApiParam({ name: 'idTown', type: Number }) + @Get('') + @ApiQuery({ name: 'routeStatus', type: String, schema: { enum: Object.values(RouteStatus) } }) + @ApiBearerAuth('jwt') + @UseGuards(AuthUserGuard) + async recommendRouteGet(@Req() req: CustomUserRequest, @Query('routeStatus') routeStatus: RouteStatus) { + const { email } = req.user; + return await this.routeService.getRouteAndPlacesByUser(email, routeStatus); + } + + @Get('/:idRoute') + @ApiParam({ name: 'idRoute', type: Number }) @ApiBearerAuth('jwt') @UseGuards(AuthUserGuard) - async recommendRouteGet(@Req() req: CustomUserRequest, @Query('lang') lang: string, @Param('idTown') idTown: number) { + async getRoute(@Param('idRoute') idRoute: number, @Req() req: CustomUserRequest) { const { email } = req.user; - return await this.routeService.getRouteAndPlacesByUser(email, lang as LANGUAGES, idTown); + return await this.routeService.getRouteById(idRoute, email); + } + + @Patch('/:idRoute') + @ApiParam({ name: 'idRoute', type: Number }) + @ApiBody({ type: UpdateRouteStatusDto, description: 'accepted, rejected, pending' }) + @ApiBearerAuth('jwt') + @UseGuards(AuthUserGuard) + async updateRoute(@Body() updateRouteStatusDto: UpdateRouteStatusDto, @Param('idRoute') idRoute: number) { + return await this.routeService.updateRoute(idRoute, updateRouteStatusDto.status); } } diff --git a/backend/src/route/route.service.ts b/backend/src/route/route.service.ts index 572a18384134f7a0756e4135ff7366e5f5a9b1d4..b4acebdcc4975bd645aa6b23940083dce5fc6a0e 100644 --- a/backend/src/route/route.service.ts +++ b/backend/src/route/route.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; -import { Route } from './entities/route.entity'; +import { Route, RouteStatus } from './entities/route.entity'; import { DataSource, Repository } from 'typeorm'; import { User } from 'src/user/entities/user.entity'; import { Town } from 'src/town/entities/town.entity'; @@ -14,6 +14,7 @@ import { Visited } from 'src/visited/entities/visited.entity'; import { RecommendPlace } from './dto/recommend-route.dto'; import { CreateRouteDto } from './dto/create-route.dto'; import { CreateTravelPlaceDto } from 'src/travel-place/dto/create-travel-place.dto'; +import { UserService } from 'src/user/user.service'; @Injectable() export class RouteService { @@ -25,6 +26,7 @@ export class RouteService { @InjectDataSource() private dataSource: DataSource, private readonly placeService: PlaceService, private readonly visitedService: VisitedService, + private readonly userService: UserService, ) {} async recommend(idTown: number, email: string, language: LANGUAGES, start, end) { @@ -87,12 +89,21 @@ export class RouteService { } } - async getRouteAndPlacesByUser(email: string, language: LANGUAGES, idTown: number) { - const res = await this.routeRepository.findOne({ + async getRouteAndPlacesByUser(email: string, routeStatus: RouteStatus) { + const res = await this.routeRepository.find({ relations: ['travelPlace'], - where: { user: { email }, town: { townId: idTown } }, + where: { user: { email }, status: routeStatus }, }); return res; } + + async updateRoute(idRoute: number, status: RouteStatus) { + await this.routeRepository.update({ idRoute }, { status }); + } + + async getRouteById(idRoute: number, email: string) { + const user: User = await this.userService.findOne(email); + return await this.routeRepository.findOneBy({ idRoute, user }); + } }