init
This commit is contained in:
22
src/app.module.ts
Normal file
22
src/app.module.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {Module} from '@nestjs/common';
|
||||
import {MongooseModule} from '@nestjs/mongoose';
|
||||
import {MONGO_URL} 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.forFeature([
|
||||
{name: Store.name, schema: StoreSchema},
|
||||
]),
|
||||
],
|
||||
controllers: [
|
||||
StoreController,
|
||||
],
|
||||
providers: [
|
||||
StoreService,
|
||||
]
|
||||
})
|
||||
export class AppModule {}
|
||||
2
src/consts.ts
Normal file
2
src/consts.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export const DB_NAME = '/store-service';
|
||||
export const MONGO_URL = `mongodb://localhost:27017${DB_NAME}`;
|
||||
20
src/main.ts
Normal file
20
src/main.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import {NestFactory} from '@nestjs/core';
|
||||
import {SwaggerModule, DocumentBuilder} from '@nestjs/swagger';
|
||||
import {AppModule} from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
const options = new DocumentBuilder()
|
||||
.setTitle('Store API')
|
||||
.setDescription('API работает по принципу LocalStorage в браузере.')
|
||||
.setVersion('1.0.0')
|
||||
.addTag('store')
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, options);
|
||||
SwaggerModule.setup('api', app, document);
|
||||
|
||||
await app.listen(4001);
|
||||
}
|
||||
bootstrap();
|
||||
25
src/store/store.controller.ts
Normal file
25
src/store/store.controller.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Controller, Get, Post, Body, Param } from '@nestjs/common';
|
||||
import {StoreService} from './store.service';
|
||||
import {Store, StoreParams} from './store.schema';
|
||||
|
||||
@Controller('store')
|
||||
export class StoreController {
|
||||
constructor(
|
||||
private readonly storeService: StoreService
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
async findAll(): Promise<Store[]> {
|
||||
return this.storeService.findAll();
|
||||
}
|
||||
|
||||
@Get(':key?')
|
||||
async findOne(@Param() {key}: {key: string}): Promise<Store> {
|
||||
return this.storeService.findOne(key);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() createStoreClass: StoreParams): Promise<Store> {
|
||||
return this.storeService.create(createStoreClass);
|
||||
}
|
||||
}
|
||||
25
src/store/store.schema.ts
Normal file
25
src/store/store.schema.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document } from 'mongoose';
|
||||
|
||||
export class StoreParams {
|
||||
key: string;
|
||||
value: any;
|
||||
}
|
||||
|
||||
@Schema()
|
||||
export class Store extends Document {
|
||||
@Prop({
|
||||
required: true,
|
||||
unique: true,
|
||||
type: String,
|
||||
})
|
||||
key: string;
|
||||
|
||||
@Prop({
|
||||
required: true,
|
||||
type: {}
|
||||
})
|
||||
value: any;
|
||||
}
|
||||
|
||||
export const StoreSchema = SchemaFactory.createForClass(Store);
|
||||
29
src/store/store.service.ts
Normal file
29
src/store/store.service.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {Model} from 'mongoose';
|
||||
import {Injectable} from '@nestjs/common';
|
||||
import {InjectModel} from '@nestjs/mongoose';
|
||||
import {Store, StoreParams} from './store.schema';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class StoreService {
|
||||
constructor(@InjectModel(Store.name) private storeModel: Model<Store>) {}
|
||||
|
||||
async findAll(): Promise<Store[]> {
|
||||
return this.storeModel.find().exec();
|
||||
}
|
||||
|
||||
async create(store: StoreParams): Promise<Store> {
|
||||
const searchStore = await this.findOne(store.key);
|
||||
|
||||
if (searchStore) {
|
||||
return searchStore.updateOne(store);
|
||||
} else {
|
||||
const createdStore = new this.storeModel(store);
|
||||
return createdStore.save();
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(key: string): Promise<Store> {
|
||||
return this.storeModel.findOne({key});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user