From a71f1dc0d36a55b48676b7c24e67c2f84c0e0be7 Mon Sep 17 00:00:00 2001 From: chandnitin333 Date: Tue, 2 May 2023 15:01:54 +0530 Subject: [PATCH 1/3] Create new Controller for get countries list --- src/api/controllers/Country.ts | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) 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..70edea39 --- /dev/null +++ b/src/api/controllers/Country.ts @@ -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 { + 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) => { + + return { + name: country.name.official, + currency: country.currencies, + }; + }); + + } catch (error) { + console.error('Error fetching countries:', error); + throw error; + } + } + + +} From a7fb7e4462641879bef3a8034e81d60742fb4f5f Mon Sep 17 00:00:00 2001 From: chandnitin333 Date: Tue, 2 May 2023 15:03:56 +0530 Subject: [PATCH 2/3] Added new Function for search user --- src/api/controllers/UserController.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api/controllers/UserController.ts b/src/api/controllers/UserController.ts index eadbc3ea..eb5b1c39 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'; @@ -96,5 +96,11 @@ export class UserController { public delete(@Param('id') id: string): Promise { return this.userService.delete(id); } + + @Get('/search') + @ResponseSchema(UserResponse, { isArray: true }) + public search(@QueryParam('searchText') searchText: string): Promise { + return this.userService.getUser(searchText); + } } From 075e4704f0909bcd9267be5f776eb17e29340249 Mon Sep 17 00:00:00 2001 From: chandnitin333 Date: Tue, 2 May 2023 15:05:10 +0530 Subject: [PATCH 3/3] Added Service Function --- src/api/services/UserService.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/api/services/UserService.ts b/src/api/services/UserService.ts index 702b7351..2e937f8d 100644 --- a/src/api/services/UserService.ts +++ b/src/api/services/UserService.ts @@ -47,4 +47,14 @@ export class UserService { return; } + public getUser(query: string): Promise { + 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(); + } }