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

agregando metodo findOne

parent d50b8cde
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -4,20 +4,19 @@ import { InjectRepository } from '@nestjs/typeorm';
import { State } from './entities/state.entity';
import { Repository } from 'typeorm';
import { ServerConstants } from 'src/constants/server.contants';
import { StateResponseDto } from './dto/state-res.dto';

@Injectable()
export class StateService {
  constructor(
    @InjectRepository(State) private stateRepository: Repository<State>,
  ) {}
  constructor(@InjectRepository(State) private stateRepository: Repository<State>) {}

  async create(createStateDto: CreateStateDto) {
    await this.stateRepository.save(createStateDto);
  }

  async findAll() {
  async findAll(): Promise<Array<StateResponseDto>> {
    const states = await this.stateRepository.find();
    const statesResponse = states.map(({ imageName, name, stateId }) => {
    const statesResponse: Array<StateResponseDto> = states.map(({ imageName, name, stateId }) => {
      return {
        stateId,
        name,
@@ -27,4 +26,14 @@ export class StateService {

    return statesResponse;
  }

  async findOne(id: number) {
    const state = await this.stateRepository.findOneBy({ stateId: id });
    if (!state) return null;
    return {
      stateId: state.stateId,
      name: state.name,
      imageURL: `${ServerConstants.HOST}/states/${state.imageName}`,
    };
  }
}