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

Amit Assignment : NodeJs : countryList Api and userSearch #267

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ MONITOR_ENABLED=true
MONITOR_ROUTE=/monitor
MONITOR_USERNAME=admin
MONITOR_PASSWORD=1234

# Countrylist EndPoint
#
COUNTRY_DATA_URL= 'https://restcountries.com/v3.1/all'
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
],
"dependencies": {
"bcrypt": "3.0.1",
"bcryptjs": "^2.4.3",
"chalk": "^2.4.1",
"class-validator": "^0.9.1",
"class-validator-jsonschema": "^1.3.0",
Expand All @@ -49,7 +49,7 @@
"dataloader": "^1.3.0",
"dotenv": "6.0.0",
"event-dispatch": "^0.4.1",
"express": "^4.16.2",
"express": "^4.18.2",
"express-basic-auth": "^1.1.3",
"express-graphql": "^0.6.11",
"express-status-monitor": "^1.0.1",
Expand All @@ -72,14 +72,14 @@
"serve-favicon": "^2.4.5",
"supertest": "^3.0.0",
"swagger-ui-express": "4.0.1",
"ts-node": "7.0.1",
"ts-node": "^10.9.1",
"tslint": "^5.8.0",
"type-graphql": "^0.15.0",
"typedi": "0.8.0",
"typeorm": "^0.2.5",
"typeorm-seeding": "^1.0.0-beta.6",
"typeorm-seeding": "1.0.0-beta.6",
"typeorm-typedi-extensions": "^0.2.1",
"typescript": "^3.6.3",
"typescript": "^5.0.4",
"uuid": "^3.3.2",
"winston": "3.1.0"
},
Expand All @@ -102,20 +102,20 @@
},
"license": "MIT",
"devDependencies": {
"@types/bcrypt": "^2.0.0",
"@types/bcryptjs": "^2.4.2",
"@types/bluebird": "^3.5.18",
"@types/chalk": "^2.2.0",
"@types/commander": "^2.11.0",
"@types/cors": "^2.8.1",
"@types/dotenv": "^4.0.2",
"@types/express": "^4.0.39",
"@types/express": "^4.17.17",
"@types/faker": "^4.1.2",
"@types/figlet": "^1.2.0",
"@types/helmet": "0.0.41",
"@types/jest": "23.3.2",
"@types/morgan": "^1.7.35",
"@types/nock": "^9.1.3",
"@types/node": "^12.7.5",
"@types/node": "^20.1.0",
"@types/reflect-metadata": "0.1.0",
"@types/serve-favicon": "^2.2.29",
"@types/supertest": "^2.0.4",
Expand Down
29 changes: 29 additions & 0 deletions src/api/controllers/CountryController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IsNotEmpty } from 'class-validator';
import { Get, JsonController} from 'routing-controllers';
import {ResponseSchema } from 'routing-controllers-openapi';

import { CountryService } from '../services/CountryService';
import { ICountries } from '../types/Country';

export class CountryResponse {
@IsNotEmpty()
public name: string;

@IsNotEmpty()
public currencies: any;

}

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

constructor(
private countryService: CountryService
) { }

@Get()
@ResponseSchema(CountryResponse, { isArray: true })
public find(): Promise<ICountries[]> {
return this.countryService.countryList();
}
}
7 changes: 7 additions & 0 deletions src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ export class UserController {
return this.userService.delete(id);
}

@Get('search/:searchTerm')
@OnUndefined(UserNotFoundError)
@ResponseSchema(UserResponse)
public search(@Param('searchTerm') searchTerm: string): Promise<User[] | undefined> {
return this.userService.searchUser(searchTerm);
}

}
2 changes: 1 addition & 1 deletion src/api/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as bcrypt from 'bcrypt';
import * as bcrypt from 'bcryptjs';
import { Exclude } from 'class-transformer';
import { IsNotEmpty } from 'class-validator';
import { BeforeInsert, Column, Entity, OneToMany, PrimaryColumn } from 'typeorm';
Expand Down
42 changes: 42 additions & 0 deletions src/api/services/CountryService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Service } from 'typedi';

import { Logger, LoggerInterface } from '../../decorators/Logger';
import axios from 'axios';
import { ICountries } from '../types/Country';
import { env } from '../../env';

@Service()
export class CountryService {

constructor(
@Logger(__filename) private log: LoggerInterface
) { }

public async countryList(): Promise<ICountries[]> {
this.log.info('Get Country List');
const url = env.external.countryDataUrl;
const headers = {
'Content-Type': 'application/json',
};
const countriesList: ICountries[] = [];
try {
const response = await axios({
method: 'GET',
url: url,
headers: headers,
});
if (response.status== 200) {
response.data.forEach((country: { name: any; currencies: any }) => {
countriesList.push({
name: country.name.common,
currencies: country.currencies,
});
});
this.log.info(`countriesList: ${JSON.stringify(countriesList)}`);
}
} catch (err) {
this.log.error(`exception caught while getting countriesList : ${JSON.stringify(err)}`);
}
return countriesList;
}
}
10 changes: 10 additions & 0 deletions src/api/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export class UserService {
return this.userRepository.findOne({ id });
}

public searchUser(searchTerm: string): Promise<User[]> {
this.log.info('Search users : ', {searchTerm});
const result = this.userRepository
.createQueryBuilder('u')
.where('u.firstName ILIKE :searchTerm', {searchTerm: `%${searchTerm}%`})
.orWhere('u.lastName ILIKE :searchTerm', {searchTerm: `%${searchTerm}%`})
.getMany();
return result;
}

public async create(user: User): Promise<User> {
this.log.info('Create a new user => ', user.toString());
user.id = uuid.v1();
Expand Down
5 changes: 5 additions & 0 deletions src/api/types/Country.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export type ICountries = {
name: string;
currencies: any;
}
4 changes: 4 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ export const env = {
username: getOsEnv('MONITOR_USERNAME'),
password: getOsEnv('MONITOR_PASSWORD'),
},
external:{
countryDataUrl: getOsEnv('COUNTRY_DATA_URL')
}

};
Loading