init
This commit is contained in:
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