From 006c28961b7a3bc3171109655b9998431486c5c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:48:57 -0600 Subject: [PATCH 1/2] renombrando dto --- .../place/dto/{update-place.dto.ts => update-place.req.dto.ts} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename backend/src/place/dto/{update-place.dto.ts => update-place.req.dto.ts} (59%) diff --git a/backend/src/place/dto/update-place.dto.ts b/backend/src/place/dto/update-place.req.dto.ts similarity index 59% rename from backend/src/place/dto/update-place.dto.ts rename to backend/src/place/dto/update-place.req.dto.ts index 5db344e6..0aa9aa55 100644 --- a/backend/src/place/dto/update-place.dto.ts +++ b/backend/src/place/dto/update-place.req.dto.ts @@ -1,4 +1,4 @@ import { PartialType } from '@nestjs/swagger'; import { CreatePlaceDateTradDto } from './create-place-date.dto'; -export class UpdatePlaceDto extends PartialType(CreatePlaceDateTradDto) {} +export class UpdatePlaceReqDto extends PartialType(CreatePlaceDateTradDto) {} -- GitLab From 362cc820a8f66bc94f4fdc4440bfd6534d488855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:49:15 -0600 Subject: [PATCH 2/2] agregando endpoint para actualizar un place --- backend/src/place/place.controller.ts | 32 +++++++++++++++++++-- backend/src/place/place.service.ts | 40 ++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/backend/src/place/place.controller.ts b/backend/src/place/place.controller.ts index 3a2eedb0..82e5edeb 100644 --- a/backend/src/place/place.controller.ts +++ b/backend/src/place/place.controller.ts @@ -9,6 +9,7 @@ import { Query, UnauthorizedException, Req, + Patch, } from '@nestjs/common'; import { PlaceService } from './place.service'; import { CreatePlaceDateTradDto } from './dto/create-place-date.dto'; @@ -19,6 +20,7 @@ import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor'; import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe'; import { LANGUAGES } from 'src/shared/enum/languages.enum'; import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq'; +import { UpdatePlaceReqDto } from './dto/update-place.req.dto'; @Controller('place') @ApiTags('Place') @@ -59,7 +61,33 @@ export class PlaceController { } @Get(':idPlace') - findOne(@Param('idPlace') idPlace: number) { - return this.placeService.findOneAndTradAndAvailable(idPlace); + @ApiQuery({ name: 'lang', type: String }) + findOne(@Param('idPlace') idPlace: number, @Query('lang') lang: string) { + return this.placeService.findOneAndTradAndAvailable(idPlace, lang as LANGUAGES); + } + + @Patch(':idPlace') + @Roles([ALL_ROLES.ADMIN]) + @ApiBearerAuth('jwt') + @ApiConsumes('multipart/form-data') + @ApiBody({ type: CreatePlaceDateTradDto }) + @UseInterceptors(fileInterceptor('image', 'static/places/', ['.jpg', '.jpeg', '.png'])) + async update( + @UploadedFile(new FileValidationPipe()) file, + @Body() updatePlaceReqDto: UpdatePlaceReqDto, + @Param('idPlace') idPlace: number, + @Req() req: CustomAdminRequest, + ) { + try { + const place = await this.placeService.findOne(idPlace); + console.log({ place }); + if (req.admin.idTown.townId != updatePlaceReqDto.idTown) { + throw new UnauthorizedException('This is not your assigned town'); + } + updatePlaceReqDto.image = file; + return await this.placeService.update(idPlace, updatePlaceReqDto); + } catch (e) { + throw e; + } } } diff --git a/backend/src/place/place.service.ts b/backend/src/place/place.service.ts index 86ba9854..214c40d0 100644 --- a/backend/src/place/place.service.ts +++ b/backend/src/place/place.service.ts @@ -12,6 +12,7 @@ import { Town } from 'src/town/entities/town.entity'; import { GetPlaceDto } from './dto/get-place.dto'; import { ServerConstants } from 'src/constants/server.contants'; import { Available } from 'src/pointOfInterest/enum/available.enum'; +import { UpdatePlaceReqDto } from './dto/update-place.req.dto'; @Injectable() export class PlaceService { @@ -112,7 +113,7 @@ export class PlaceService { return await this.placeRepository.findOneBy({ idPlace: id }); } - async findOneAndTradAndAvailable(idPlace: number) { + async findOneAndTradAndAvailable(idPlace: number, lang: LANGUAGES) { const place: GetPlaceDto = await this.dataSource .getRepository(PlaceTraduction) .createQueryBuilder('placeTrad') @@ -134,6 +135,7 @@ export class PlaceService { 'place.available AS available', ]) .where('place.idPlace = :idPlace', { idPlace: idPlace }) + .andWhere('placeTrad.language = :language', { language: lang }) .getRawOne(); return { @@ -151,4 +153,40 @@ export class PlaceService { endDate: place.endDate, }; } + + async update(idPlace: number, updatePlaceReqDto: UpdatePlaceReqDto) { + const place: Place = await this.placeRepository.findOneBy({ idPlace }); + const createPlaceDto: CreatePlaceDto = { + available: updatePlaceReqDto.available, + closeAt: updatePlaceReqDto.closeAt, + latitude: updatePlaceReqDto.latitude, + longitude: updatePlaceReqDto.longitude, + name: updatePlaceReqDto.name, + openAt: updatePlaceReqDto.openAt, + imageName: updatePlaceReqDto.image.filename, + idTown: place.idTown, + }; + + await this.placeRepository.update({ idPlace }, createPlaceDto); + await this.placeTraductionRepository + .createQueryBuilder() + .update(PlaceTraduction) + .set({ description: updatePlaceReqDto.descriptionEN }) + .where('idPlace = :idPlace AND language = :language', { idPlace, language: LANGUAGES.EN }) + .execute(); + + await this.placeTraductionRepository + .createQueryBuilder() + .update(PlaceTraduction) + .set({ description: updatePlaceReqDto.descriptionES }) + .where('idPlace = :idPlace AND language = :language', { idPlace, language: LANGUAGES.ES }) + .execute(); + + if (updatePlaceReqDto.available === Available.CUSTOM) { + await this.availableDateRepository.update( + { idPlace: place }, + { startDate: updatePlaceReqDto.startDate, endDate: updatePlaceReqDto.endDate }, + ); + } + } } -- GitLab