Commit 16d1eba9 authored by Diego Iván's avatar Diego Iván
Browse files

pruebas para crear una ruta

parent 9ca10f4f
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
export class CreateRouteDto {}
import { Town } from 'src/town/entities/town.entity';
import { User } from 'src/user/entities/user.entity';

export class CreateRouteDto {
  user: User;
  town: Town;
  startDate: Date;
  endDate: Date;
}
+5 −1
Original line number Diff line number Diff line
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 } from 'typeorm';
import { PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Entity, OneToMany } from 'typeorm';

@Entity()
export class Route {
@@ -15,6 +16,9 @@ export class Route {
  @ManyToOne(() => Town, (town) => town.townId, { nullable: false })
  town: Town;

  @OneToMany(() => TravelPlace, (travelPlace) => travelPlace.route)
  travelPlace: TravelPlace[];

  @Column({ nullable: false })
  startDate: Date;
  @Column({ nullable: false })
+11 −7
Original line number Diff line number Diff line
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { RouteService } from './route.service';
import { CreateRouteDto } from './dto/create-route.dto';
import { UpdateRouteDto } from './dto/update-route.dto';
import { ApiConsumes, ApiTags } from '@nestjs/swagger';

@Controller('route')
@Controller('Route')
@ApiTags('testing')
export class RouteController {
  constructor(private readonly routeService: RouteService) {}

  @Post()
  create(@Body() createRouteDto: CreateRouteDto) {
    return this.routeService.create(createRouteDto);
  @ApiConsumes('multipart/form-data')
  // @UseGuards(AuthUserGuard)
  async create(@Body() createRouteDto: CreateRouteDto) {
    return await this.routeService.create();
  }

  @Get()
@@ -17,9 +21,9 @@ export class RouteController {
    return this.routeService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.routeService.findOne(+id);
  @Get(':idRoute')
  async findOne(@Param('idRoute') idRoute: number) {
    return await this.routeService.findOne(idRoute);
  }

  @Patch(':id')
+23 −1
Original line number Diff line number Diff line
import { Module } from '@nestjs/common';
import { RouteService } from './route.service';
import { RouteController } from './route.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Route } from './entities/route.entity';
import { AuthUserService } from 'src/auth/user/authUserservice';
import { UserService } from 'src/user/user.service';
import { JwtService } from '@nestjs/jwt';
import { EncryptionService } from 'src/auth/encryption/encryption.service';
import { User } from 'src/user/entities/user.entity';
import { CategoryService } from 'src/category/category.service';
import { Category } from 'src/category/entities/category.entity';
import { Town } from 'src/town/entities/town.entity';
import { TravelPlace } from 'src/travel-place/entities/travel-place.entity';
import { TravelPlaceService } from 'src/travel-place/travel-place.service';
import { Place } from 'src/place/entities/place.entity';

@Module({
  controllers: [RouteController],
  providers: [RouteService],
  providers: [
    RouteService,
    AuthUserService,
    UserService,
    JwtService,
    EncryptionService,
    CategoryService,
    TravelPlaceService,
  ],
  imports: [TypeOrmModule.forFeature([Route, User, Category, Town, TravelPlace, Place])],
})
export class RouteModule {}
+33 −4
Original line number Diff line number Diff line
import { Injectable } from '@nestjs/common';
import { CreateRouteDto } from './dto/create-route.dto';
import { UpdateRouteDto } from './dto/update-route.dto';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { Route } from './entities/route.entity';
import { DataSource, In, Repository } from 'typeorm';
import { User } from 'src/user/entities/user.entity';
import { Town } from 'src/town/entities/town.entity';
import { TravelPlaceService } from 'src/travel-place/travel-place.service';
import { TravelPlace } from 'src/travel-place/entities/travel-place.entity';

@Injectable()
export class RouteService {
  create(createRouteDto: CreateRouteDto) {
    return 'This action adds a new route';
  constructor(
    @InjectRepository(Route) private routeRepository: Repository<Route>,
    @InjectRepository(User) private userRepository: Repository<User>,
    @InjectRepository(Town) private townRepository: Repository<Town>,
    private readonly travelPlaceService: TravelPlaceService,
    @InjectDataSource() private dataSource: DataSource,
  ) {}
  async create() {
    const user: User = await this.userRepository.findOneBy({ userId: 1 });
    const town: Town = await this.townRepository.findOneBy({ townId: 1 });
    await this.routeRepository.save({ user, town, startDate: new Date(), endDate: new Date() });
    await this.travelPlaceService.create({
      idRoute: 1,
      idPlace: 1,
      startDate: new Date(),
      endDate: new Date(),
      done: true,
    });
  }

  findAll() {
    return `This action returns all route`;
  }

  findOne(id: number) {
    return `This action returns a #${id} route`;
  async findOne(id: number) {
    const res: any[] = await this.dataSource
      .getRepository(Route)
      .createQueryBuilder('route')
      .leftJoinAndSelect('route.travelPlace', 'travelPlace')
      .getMany();

    return res;
  }

  update(id: number, updateRouteDto: UpdateRouteDto) {