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

Nitin assignment - Node JS- Assignment- develop counties name and currency and search user to pattern #265

Open
wants to merge 3 commits 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.
Jump to
Jump to file
Failed to load files.
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();
}
}