Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/src/currency-hub/currency-hub.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SearchCurrencyDto } from './dto/search-currency.dto';
import {
Controller,
Get,
Expand All @@ -19,6 +20,11 @@ import { QueryCurrencyHubDto } from './dto/query-currency-hub.dto';
export class CurrencyHubController {
constructor(private readonly currencyHubService: CurrencyHubService) {}

@Get('search')
searchWithPagination(@Query() dto: SearchCurrencyDto) {
return this.currencyHubService.searchAndPaginateCurrencies(dto);
}

@Post()
create(@Body() createCurrencyHubDto: CreateCurrencyHubDto) {
return this.currencyHubService.create(createCurrencyHubDto);
Expand Down
9 changes: 9 additions & 0 deletions backend/src/currency-hub/currency-hub.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,13 @@ describe('CurrencyHubService', () => {
).rejects.toThrow(NotFoundException);
});
});

describe('searchAndPaginateCurrencies', () => {
it('returns filtered and paginated results', async () => {
const result = await service.searchAndPaginateCurrencies({ search: 'usd', page: 1, limit: 5 });
expect(result).toHaveProperty('data');
expect(result).toHaveProperty('total');
expect(Array.isArray(result.data)).toBe(true);
});
});
});
29 changes: 29 additions & 0 deletions backend/src/currency-hub/currency-hub.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// src/currency-hub/currency-hub.service.ts
import { SearchCurrencyDto } from './dto/search-currency.dto';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Between, FindOptionsWhere } from 'typeorm';
Expand Down Expand Up @@ -238,4 +239,32 @@ export class CurrencyHubService {
// For demonstration purposes, we'll just log a message
console.log('Updating exchange rates from external sources...');
}

// Added Filter + Pagination
async searchAndPaginateCurrencies(
dto: SearchCurrencyDto,
): Promise<{
data: CurrencyHub[];
total: number;
page: number;
limit: number;
}> {
const { search, page = 1, limit = 10 } = dto;

const qb = this.currencyHubRepository.createQueryBuilder('currency');

if (search) {
qb.where('currency.baseCurrencyCode ILIKE :search', { search: `%${search}%` })
.orWhere('currency.targetCurrencyCode ILIKE :search', { search: `%${search}%` })
.orWhere('currency.provider ILIKE :search', { search: `%${search}%` });
}

const [data, total] = await qb
.skip((page - 1) * limit)
.take(limit)
.orderBy('currency.createdAt', 'DESC')
.getManyAndCount();

return { data, total, page, limit };
}
}
20 changes: 20 additions & 0 deletions backend/src/currency-hub/dto/search-currency.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IsOptional, IsString, IsNumber, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class SearchCurrencyDto {
@IsOptional()
@IsString()
search?: string;

@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(1)
page?: number = 1;

@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(1)
limit?: number = 10;
}
Loading