Skip to content
Open
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
45 changes: 45 additions & 0 deletions src/api/controllers/Country.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import { Authorized, Get, JsonController } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';

export class Country {
public name: string;
public currency: string;
}
@ResponseSchema(Country, { isArray: true })
export class CountryResponse {
public countries: Country[];
}

@Authorized()
@JsonController('/countries')
@OpenAPI({ security: [{ basicAuth: [] }] })
export class PetController {

@Get()
@ResponseSchema(CountryResponse, { isArray: true })
public async getCountries(): Promise<CountryResponse> {
const countries: Country[] = await this.fetchCountries();
return {countries}
}

private async fetchCountries(): Promise<Country[]> {
try {
const response = await fetch('https://restcountries.com/v3.1/all');
const data = await response.json();
return data.map((country: any) => {

return {
name: country.name.official,
currency: country.currencies,
};
});

} catch (error) {
console.error('Error fetching countries:', error);
throw error;
}
}


}
8 changes: 7 additions & 1 deletion src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from 'class-transformer';
import { IsEmail, IsNotEmpty, IsUUID, ValidateNested } from 'class-validator';
import {
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Req
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam,Req
} from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';

Expand Down Expand Up @@ -96,5 +96,11 @@ export class UserController {
public delete(@Param('id') id: string): Promise<void> {
return this.userService.delete(id);
}

@Get('/search')
@ResponseSchema(UserResponse, { isArray: true })
public search(@QueryParam('searchText') searchText: string): Promise<User[]> {
return this.userService.getUser(searchText);
}

}
10 changes: 10 additions & 0 deletions src/api/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ export class UserService {
return;
}

public getUser(query: string): Promise<User[]> {
this.log.info('Pattern to search user');
const searchPater = query.toLowerCase();
return this.userRepository
.createQueryBuilder('user')
.where('LOWER(user.firstName) LIKE :searchText', { searchText: `%${searchPater}%` })
.orWhere('LOWER(user.lastName) LIKE :searchText', { searchText: `%${searchPater}%` })
.orWhere('LOWER(user.username) LIKE :searchText', { searchText: `%${searchPater}%` })
.getMany();
}
}