Loading backend/src/town/town.service.ts +14 −12 Original line number Diff line number Diff line import { Injectable } from '@nestjs/common'; import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateTownDto } from './dto/create-town.dto'; import { UpdateTownDto } from './dto/update-town.dto'; import { Repository } from 'typeorm'; import { Town } from './entities/town.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { StateService } from 'src/state/state.service'; @Injectable() export class TownService { create(createTownDto: CreateTownDto) { return 'This action adds a new town'; constructor( @InjectRepository(Town) private townRepository: Repository<Town>, private state: StateService, ) {} async create(createTownDto: CreateTownDto) { const state = await this.state.findOne(createTownDto.state); if (!state) throw new BadRequestException('State does not exist'); await this.townRepository.save(createTownDto); } findAll() { return `This action returns all town`; } findOne(id: number) { return `This action returns a #${id} town`; } update(id: number, updateTownDto: UpdateTownDto) { return `This action updates a #${id} town`; } remove(id: number) { return `This action removes a #${id} town`; } Loading Loading
backend/src/town/town.service.ts +14 −12 Original line number Diff line number Diff line import { Injectable } from '@nestjs/common'; import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateTownDto } from './dto/create-town.dto'; import { UpdateTownDto } from './dto/update-town.dto'; import { Repository } from 'typeorm'; import { Town } from './entities/town.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { StateService } from 'src/state/state.service'; @Injectable() export class TownService { create(createTownDto: CreateTownDto) { return 'This action adds a new town'; constructor( @InjectRepository(Town) private townRepository: Repository<Town>, private state: StateService, ) {} async create(createTownDto: CreateTownDto) { const state = await this.state.findOne(createTownDto.state); if (!state) throw new BadRequestException('State does not exist'); await this.townRepository.save(createTownDto); } findAll() { return `This action returns all town`; } findOne(id: number) { return `This action returns a #${id} town`; } update(id: number, updateTownDto: UpdateTownDto) { return `This action updates a #${id} town`; } remove(id: number) { return `This action removes a #${id} town`; } Loading