2023-01-18 15:40:15 +01:00
|
|
|
import { IKeyRepository } from '@app/domain';
|
2023-01-02 21:22:33 +01:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
2023-01-18 15:40:15 +01:00
|
|
|
import { APIKeyEntity } from '../entities';
|
2023-01-02 21:22:33 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class APIKeyRepository implements IKeyRepository {
|
|
|
|
constructor(@InjectRepository(APIKeyEntity) private repository: Repository<APIKeyEntity>) {}
|
|
|
|
|
|
|
|
async create(dto: Partial<APIKeyEntity>): Promise<APIKeyEntity> {
|
|
|
|
return this.repository.save(dto);
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(userId: string, id: number, dto: Partial<APIKeyEntity>): Promise<APIKeyEntity> {
|
|
|
|
await this.repository.update({ userId, id }, dto);
|
|
|
|
return this.repository.findOneOrFail({ where: { id: dto.id } });
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(userId: string, id: number): Promise<void> {
|
|
|
|
await this.repository.delete({ userId, id });
|
|
|
|
}
|
|
|
|
|
|
|
|
getKey(id: number): Promise<APIKeyEntity | null> {
|
|
|
|
return this.repository.findOne({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
key: true,
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
where: { id },
|
|
|
|
relations: {
|
|
|
|
user: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getById(userId: string, id: number): Promise<APIKeyEntity | null> {
|
|
|
|
return this.repository.findOne({ where: { userId, id } });
|
|
|
|
}
|
|
|
|
|
|
|
|
getByUserId(userId: string): Promise<APIKeyEntity[]> {
|
|
|
|
return this.repository.find({ where: { userId }, order: { createdAt: 'DESC' } });
|
|
|
|
}
|
|
|
|
}
|