HM-45. Добавлена возможность использования тестового api с помощью заголовка

This commit is contained in:
vigdorov
2020-07-17 20:59:52 +03:00
parent 6a0ea4a438
commit 8beb84567a
5 changed files with 81 additions and 42 deletions

View File

@ -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<Store>) {}
constructor(
@InjectConnection(DB_NAME) private dbConnection: Connection,
@InjectConnection(DB_TEST_NAME) private dbTestConnection: Connection,
) {}
async findAll(): Promise<Store[]> {
return this.storeModel.find().exec();
storeModel(api: string): Model<Store> {
if (api === DB_TEST_NAME) {
return this.dbTestConnection.model<Store>(COOLECTION_STORE, StoreSchema);
}
return this.dbConnection.model<Store>(COOLECTION_STORE, StoreSchema);
}
async create(store: StoreRequest): Promise<Store> {
const searchStore = await this.findOne(store.key);
async findAll(api: string): Promise<Store[]> {
return this.storeModel(api).find().exec();
}
async create(api: string, store: StoreRequest): Promise<Store> {
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<Store> {
const searchStore = await this.findOne(omitProps.key);
async update(api: string, {author, ...omitProps}: StoreRequest): Promise<Store> {
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<Store> {
return this.storeModel.findOne({key})
async findOne(api: string, key: string): Promise<Store> {
return this.storeModel(api).findOne({key})
}
async removeOne(key: string): Promise<Store> {
const searchStore = await this.findOne(key);
async removeOne(api: string, key: string): Promise<Store> {
const searchStore = await this.findOne(api, key);
if (searchStore) {
await this.storeModel.deleteOne({key});
await this.storeModel(api).deleteOne({key});
return searchStore;
}