diff --git a/src/app.module.ts b/src/app.module.ts index b0557a3..3d9162a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,16 +1,24 @@ import {Module} from '@nestjs/common'; import {MongooseModule} from '@nestjs/mongoose'; -import {MONGO_URL} from './consts'; +import {MONGO_URL, DB_NAME, DB_TEST_NAME} from './consts'; import {StoreService} from './store/store.service'; import {Store, StoreSchema} from './store/store.schema'; import {StoreController} from './store/store.controller'; @Module({ imports: [ - MongooseModule.forRoot(MONGO_URL), + MongooseModule.forRoot(`${MONGO_URL}/${DB_NAME}`, { + connectionName: DB_NAME, + }), + MongooseModule.forRoot(`${MONGO_URL}/${DB_TEST_NAME}`, { + connectionName: DB_TEST_NAME, + }), MongooseModule.forFeature([ {name: Store.name, schema: StoreSchema}, - ]), + ], DB_NAME), + MongooseModule.forFeature([ + {name: Store.name, schema: StoreSchema}, + ], DB_TEST_NAME), ], controllers: [ StoreController, diff --git a/src/consts.ts b/src/consts.ts index 3fd987a..c6c4e91 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -1,7 +1,9 @@ -export const DB_NAME = '/store-service'; -export const MONGO_URL = `mongodb://localhost:27017${DB_NAME}`; +export const DB_NAME = 'store-service'; +export const DB_TEST_NAME = 'store-service-test'; +export const MONGO_URL = 'mongodb://localhost:27017'; +export const COOLECTION_STORE = 'store'; export const ALLOW_ORIGIN_ALL: [string, string] = ['Access-Control-Allow-Origin', '*']; export const ALLOW_CREDENTIALS: [string, string] = ['Access-Control-Allow-Credentials', 'true']; export const CONTENT_LENGTH: [string, string] = ['Content-Length', '0']; export const ALLOW_METHOD: [string, string] = ['Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE']; -export const ALLOW_HEADERS: [string, string] = ['Access-Control-Allow-Headers', 'Version, Authorization, Content-Type']; +export const ALLOW_HEADERS: [string, string] = ['Access-Control-Allow-Headers', 'Version, Authorization, Content-Type, Api-Name']; diff --git a/src/store/store.controller.ts b/src/store/store.controller.ts index 17bed35..84ddb58 100644 --- a/src/store/store.controller.ts +++ b/src/store/store.controller.ts @@ -1,8 +1,9 @@ -import { Controller, Get, Post, Body, Param, Options, Header, Delete, HttpCode, Put } from '@nestjs/common'; +import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put} from '@nestjs/common'; import {StoreService} from './store.service'; import {Store, StoreRequest} from './store.schema'; import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger'; -import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS} from 'src/consts'; +import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, COOLECTION_STORE} from 'src/consts'; +import {Request} from 'express'; const prepareStoreToStoreRequest = ({ key, value, description, service_name, author @@ -10,8 +11,13 @@ const prepareStoreToStoreRequest = ({ key, value, description, service_name, author, }); -@Controller('store') -@ApiTags('store') +const makeApiHeader = (request: Request): string => { + const apiHeader = request.headers?.['api-name']; + return typeof apiHeader === 'string' ? apiHeader : ''; +}; + +@Controller(COOLECTION_STORE) +@ApiTags(COOLECTION_STORE) export class StoreController { constructor( private readonly storeService: StoreService @@ -20,12 +26,13 @@ export class StoreController { @Get() @Header(...ALLOW_ORIGIN_ALL) @ApiResponse({ - status: 200, - description: 'Список всех пар ключ-значение', + status: 200, + description: 'Список всех пар ключ-значение', type: [StoreRequest], }) - async findAll(): Promise { - const storeList = await this.storeService.findAll(); + async findAll(@Req() request: Request): Promise { + const api = makeApiHeader(request); + const storeList = await this.storeService.findAll(api); return storeList.map(prepareStoreToStoreRequest); } @@ -40,8 +47,10 @@ export class StoreController { name: 'key', description: 'Уникальный ключ для получения api', }) - async findOne(@Param() {key}: {key: string}): Promise { - const store = await this.storeService.findOne(key); + async findOne(@Req() request: Request<{key: string}>): Promise { + const {key} = request.params; + const api = makeApiHeader(request); + const store = await this.storeService.findOne(api, key); return prepareStoreToStoreRequest(store); } @@ -56,8 +65,9 @@ export class StoreController { type: StoreRequest, description: 'Принимает объект для создания api' }) - async create(@Body() createStoreClass: StoreRequest): Promise { - const store = await this.storeService.create(createStoreClass); + async create(@Req() request: Request): Promise { + const api = makeApiHeader(request); + const store = await this.storeService.create(api, request.body); return prepareStoreToStoreRequest(store); } @@ -72,8 +82,9 @@ export class StoreController { type: StoreRequest, description: 'Принимает объект для обновления api' }) - async update(@Body() updateStoreClass: StoreRequest): Promise { - const store = await this.storeService.update(updateStoreClass); + async update(@Req() request: Request): Promise { + const api = makeApiHeader(request); + const store = await this.storeService.update(api, request.body); return prepareStoreToStoreRequest(store); } @@ -88,8 +99,10 @@ export class StoreController { name: 'key', description: 'Уникальный ключ для удаления api', }) - async removeOne(@Param() {key}: {key: string}): Promise { - const store = await this.storeService.removeOne(key); + async removeOne(@Req() request: Request<{key: string}>): Promise { + const {key} = request.params; + const api = makeApiHeader(request); + const store = await this.storeService.removeOne(api, key); return prepareStoreToStoreRequest(store); } diff --git a/src/store/store.service.ts b/src/store/store.service.ts index 703df0e..3411ef6 100644 --- a/src/store/store.service.ts +++ b/src/store/store.service.ts @@ -1,7 +1,8 @@ -import {Model} from 'mongoose'; +import {Model, Connection} from 'mongoose'; import {Injectable, NotFoundException, BadGatewayException} from '@nestjs/common'; -import {InjectModel} from '@nestjs/mongoose'; -import {Store, StoreRequest} from './store.schema'; +import {InjectConnection} from '@nestjs/mongoose'; +import {Store, StoreRequest, StoreSchema} from './store.schema'; +import {DB_TEST_NAME, DB_NAME, COOLECTION_STORE} from 'src/consts'; const validateModel = async (store: Store) => { try { @@ -13,20 +14,30 @@ const validateModel = async (store: Store) => { @Injectable() export class StoreService { - constructor(@InjectModel(Store.name) private storeModel: Model) {} + constructor( + @InjectConnection(DB_NAME) private dbConnection: Connection, + @InjectConnection(DB_TEST_NAME) private dbTestConnection: Connection, + ) {} - async findAll(): Promise { - return this.storeModel.find().exec(); + storeModel(api: string): Model { + if (api === DB_TEST_NAME) { + return this.dbTestConnection.model(COOLECTION_STORE, StoreSchema); + } + return this.dbConnection.model(COOLECTION_STORE, StoreSchema); } - async create(store: StoreRequest): Promise { - const searchStore = await this.findOne(store.key); + async findAll(api: string): Promise { + return this.storeModel(api).find().exec(); + } + + async create(api: string, store: StoreRequest): Promise { + const searchStore = await this.findOne(api, store.key); if (searchStore) { throw new NotFoundException(`Api key ${store.key} is already taken`); } - const createdStore = new this.storeModel(store); + const createdStore = new (this.storeModel(api))(store); await validateModel(createdStore); @@ -37,15 +48,15 @@ export class StoreService { return savedStore; } - async update({author, ...omitProps}: StoreRequest): Promise { - const searchStore = await this.findOne(omitProps.key); + async update(api: string, {author, ...omitProps}: StoreRequest): Promise { + const searchStore = await this.findOne(api, omitProps.key); if (searchStore) { const store = { ...omitProps, author: searchStore.author, }; - const updateStore = new this.storeModel(store); + const updateStore = new (this.storeModel(api))(store); await validateModel(updateStore); await searchStore.updateOne({ @@ -59,15 +70,15 @@ export class StoreService { throw new NotFoundException(`Not Found api key - ${omitProps.key}`); } - async findOne(key: string): Promise { - return this.storeModel.findOne({key}) + async findOne(api: string, key: string): Promise { + return this.storeModel(api).findOne({key}) } - async removeOne(key: string): Promise { - const searchStore = await this.findOne(key); + async removeOne(api: string, key: string): Promise { + const searchStore = await this.findOne(api, key); if (searchStore) { - await this.storeModel.deleteOne({key}); + await this.storeModel(api).deleteOne({key}); return searchStore; } diff --git a/store.http b/store.http index 17ae223..9f8a63d 100644 --- a/store.http +++ b/store.http @@ -1,13 +1,16 @@ GET http://localhost:4001/store HTTP/1.1 +Api-Name: store-service-test ### -GET http://localhost:4001/store/testApi22 HTTP/1.1 +GET http://localhost:4001/store/testApi-4 HTTP/1.1 +Api-Name: store-service-test ### POST http://localhost:4001/store HTTP/1.1 content-type: application/json +Api-Name: store-service-test { "key": "testApi-4", @@ -23,17 +26,19 @@ content-type: application/json PUT http://localhost:4001/store HTTP/1.1 content-type: application/json +Api-Name: store-service-test { "key": "testApi-4", "value": { }, - "description": "тестовое апи", + "description": "тестовое апи22", "service_name": "test-api", "author": "vigdorov23422" } ### -DELETE http://localhost:4001/store/testApi22 HTTP/1.1 \ No newline at end of file +DELETE http://localhost:4001/store/testApi-4 HTTP/1.1 +Api-Name: store-service-test \ No newline at end of file