2024-07-15 00:30:33 +08:00
|
|
|
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
2023-01-29 13:44:19 +08:00
|
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2024-07-15 00:30:33 +08:00
|
|
|
import { BaseService } from '../../../basic/base-service.js';
|
|
|
|
|
import { StorageEntity } from '../entity/storage.js';
|
2023-01-29 13:44:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class StorageService extends BaseService<StorageEntity> {
|
|
|
|
|
@InjectEntityModel(StorageEntity)
|
|
|
|
|
repository: Repository<StorageEntity>;
|
|
|
|
|
|
|
|
|
|
getRepository() {
|
|
|
|
|
return this.repository;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 01:30:00 +08:00
|
|
|
async get(where: { scope: any; namespace: any; userId: number; version: string; key: string }) {
|
2023-01-29 13:44:19 +08:00
|
|
|
if (where.userId == null) {
|
|
|
|
|
throw new Error('userId 不能为空');
|
|
|
|
|
}
|
|
|
|
|
return await this.repository.findOne({
|
|
|
|
|
where,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 01:30:00 +08:00
|
|
|
async set(entity: { id?: any; scope: any; namespace: any; userId: number; version: string; value: string; key: string }) {
|
2023-01-29 13:44:19 +08:00
|
|
|
entity.id = null;
|
|
|
|
|
const query = { ...entity };
|
|
|
|
|
delete query.value;
|
|
|
|
|
const ret = await this.get(query);
|
|
|
|
|
if (ret != null) {
|
|
|
|
|
entity.id = ret.id;
|
|
|
|
|
if (ret.userId !== entity.userId) {
|
|
|
|
|
throw new Error('您没有权限修改此数据');
|
|
|
|
|
}
|
|
|
|
|
await this.repository.save(entity);
|
|
|
|
|
} else {
|
|
|
|
|
await this.repository.insert(entity);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|