From 7c1c12331af13ef7c21f76cb26d73e4d4c1b9dae Mon Sep 17 00:00:00 2001 From: Suraj Rana Date: Wed, 26 Apr 2023 12:26:55 +0530 Subject: [PATCH] Implemented Country controller for retrieving county lists and introduced a user search endpoint in UserController. --- src/api/controllers/Country.ts | 41 +++++++++++++++++++++++++++ src/api/controllers/UserController.ts | 8 +++++- src/api/services/UserService.ts | 10 +++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/api/controllers/Country.ts diff --git a/src/api/controllers/Country.ts b/src/api/controllers/Country.ts new file mode 100644 index 00000000..87056162 --- /dev/null +++ b/src/api/controllers/Country.ts @@ -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 { + const countries: Country[] = await this.fetchCountries(); + return {countries}; + } + + private async fetchCountries(): Promise { + 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; + } + } +} diff --git a/src/api/controllers/UserController.ts b/src/api/controllers/UserController.ts index eadbc3ea..92026f43 100644 --- a/src/api/controllers/UserController.ts +++ b/src/api/controllers/UserController.ts @@ -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'; @@ -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 { + return this.userService.search(query); + } + @Post() @ResponseSchema(UserResponse) public create(@Body() body: CreateUserBody): Promise { diff --git a/src/api/services/UserService.ts b/src/api/services/UserService.ts index 702b7351..18c18788 100644 --- a/src/api/services/UserService.ts +++ b/src/api/services/UserService.ts @@ -47,4 +47,14 @@ export class UserService { return; } + public search(query: string): Promise { + 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(); + } + }