Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Technical Task. #264

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions src/api/controllers/Country.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Get, JsonController} from 'routing-controllers';
import {ResponseSchema} from 'routing-controllers-openapi';

export class Country {
public name: string;
public currency: string;
}

@ResponseSchema(Country, { isArray: true })
class CountryListResponse {
public countries: Country[];
}

@JsonController('/countries')
export class CountryController {

@Get()
@ResponseSchema(CountryListResponse)
public async getCountries(): Promise<CountryListResponse> {
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) => {
const currencyCode = Object.keys(country.currencies)[0];
const currency = `${country.currencies[currencyCode].name} (${currencyCode})`;
return {
name: country.name.official,
currency,
};
});
} 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 @@ -67,6 +67,12 @@ export class UserController {
return this.userService.findOne(id);
}

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

@Post()
@ResponseSchema(UserResponse)
public create(@Body() body: CreateUserBody): Promise<User> {
Expand Down
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 search(query: string): Promise<User[]> {
this.log.info('Search users with pattern');
const lowerQuery = query.toLowerCase();
return this.userRepository
.createQueryBuilder('user')
.where('LOWER(user.firstName) LIKE :query', { query: `%${lowerQuery}%` })
.orWhere('LOWER(user.lastName) LIKE :query', { query: `%${lowerQuery}%` })
.getMany();
}

}