53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { Inject, Injectable } from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
import { Repository } from "typeorm";
|
|
import { Project } from "./entities/project.entity";
|
|
import { RedisClient } from "src/redis/redis.service";
|
|
|
|
@Injectable()
|
|
export class ProjectService {
|
|
constructor(
|
|
@InjectRepository(Project)
|
|
private readonly projectRepository: Repository<Project>,
|
|
@Inject(RedisClient)
|
|
private readonly redisClient: RedisClient
|
|
) {}
|
|
|
|
create(name: string) {
|
|
const project = this.projectRepository.create({ name });
|
|
return this.projectRepository.save(project);
|
|
}
|
|
|
|
async findById(id: string) {
|
|
const cached = await this.redisClient.get(`project_${id}`);
|
|
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
const project = await this.projectRepository.findOne({ where: { id: id } });
|
|
|
|
if (project) {
|
|
await this.redisClient.set(`project_${id}`, project, 300);
|
|
}
|
|
|
|
return project;
|
|
}
|
|
|
|
async updateDatabase(projectId: string, databaseId: string) {
|
|
await this.redisClient.del(`project_${projectId}`);
|
|
|
|
return this.projectRepository.update(projectId, {
|
|
database: { id: databaseId },
|
|
});
|
|
}
|
|
|
|
async updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
|
|
await this.redisClient.del(`project_${projectId}`);
|
|
|
|
return this.projectRepository.update(projectId, {
|
|
redisNodes: redisNodeId,
|
|
});
|
|
}
|
|
}
|