Commit 33b51e79 authored by Diego Iván's avatar Diego Iván
Browse files

agregando update Town endpoint

parent 2a22adbb
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
import { Controller, Get, Post, Param, UseInterceptors, UploadedFile, Body, Query } from '@nestjs/common';
import { Controller, Get, Post, Param, UseInterceptors, UploadedFile, Body, Query, Patch } from '@nestjs/common';
import { TownService } from './town.service';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe';
@@ -45,4 +45,30 @@ export class TownController {
      throw error;
    }
  }

  @Roles(SUPERADMIN_ROLES)
  @ApiBearerAuth('jwt')
  @ApiBody({ type: CreateTownReqDto })
  @ApiConsumes('multipart/form-data')
  @Patch('town/:idTown')
  @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png']))
  async update(
    @Param('idTown') idTown: number,
    @UploadedFile(new FileValidationPipe()) file,
    @Body() createTownReqDto: CreateTownReqDto,
  ) {
    try {
      const updateTownDto: CreateTownDto = {
        name: createTownReqDto.name,
        imageName: file.filename,
        descriptionEN: createTownReqDto.descriptionEN,
        descriptionES: createTownReqDto.descriptionES,
        state: createTownReqDto.state,
      };
      await this.townService.update(idTown, updateTownDto);
      return { message: 'Town created successfully' };
    } catch (error) {
      throw error;
    }
  }
}
+27 −3
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { DataSource } from 'typeorm';
import { ServerConstants } from 'src/constants/server.contants';
import { TownResDto } from './dto/town-res.dto';
import { UpdateTownDto } from './dto/update-town.dto';

@Injectable()
export class TownService {
@@ -24,18 +25,16 @@ export class TownService {
  async create(createTownDto: CreateTownDto) {
    const state = await this.state.findOne(createTownDto.state);
    if (!state) throw new BadRequestException('State does not exist');
    const town = await this.townRepository.save(createTownDto);
    const town = await this.townRepository.save({ ...createTownDto, state });
    const createTownTradDtoEN: CreateTownTraductionDto = {
      townId: town.townId,
      language: LANGUAGES.EN,
      name: createTownDto.name,
      description: createTownDto.descriptionEN,
    };

    const createTownTradDtoES: CreateTownTraductionDto = {
      townId: town.townId,
      language: LANGUAGES.ES,
      name: createTownDto.name,
      description: createTownDto.descriptionES,
    };

@@ -69,4 +68,29 @@ export class TownService {
      stateId: item.stateId,
    }));
  }

  async update(townId: number, updateTownDto: CreateTownDto) {
    const state = await this.state.findOne(updateTownDto.state);
    if (!state) throw new BadRequestException('State does not exist');
    const updateTown: UpdateTownDto = {
      name: updateTownDto.name,
      imageName: updateTownDto.imageName,
      state: state.stateId,
    };
    await this.townRepository.update({ townId }, { ...updateTown, state: state });
    const createTownTradDtoEN: CreateTownTraductionDto = {
      townId: townId,
      language: LANGUAGES.EN,
      description: updateTownDto.descriptionEN,
    };

    const createTownTradDtoES: CreateTownTraductionDto = {
      townId: townId,
      language: LANGUAGES.ES,
      description: updateTownDto.descriptionES,
    };

    await this.townTradRepository.update({ townId, language: LANGUAGES.ES }, createTownTradDtoES);
    await this.townTradRepository.update({ townId, language: LANGUAGES.EN }, createTownTradDtoEN);
  }
}