2023-01-25 17:35:28 +01:00
|
|
|
import { ISharedLinkRepository } from '@app/domain';
|
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
import { SharedLinkEntity } from '../entities';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SharedLinkRepository implements ISharedLinkRepository {
|
|
|
|
readonly logger = new Logger(SharedLinkRepository.name);
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(SharedLinkEntity)
|
|
|
|
private readonly repository: Repository<SharedLinkEntity>,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
get(userId: string, id: string): Promise<SharedLinkEntity | null> {
|
|
|
|
return this.repository.findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
assets: {
|
|
|
|
exifInfo: true,
|
|
|
|
},
|
|
|
|
album: {
|
|
|
|
assets: {
|
2023-02-18 21:58:55 +01:00
|
|
|
exifInfo: true,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
2023-02-28 01:28:45 +01:00
|
|
|
owner: true,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
createdAt: 'DESC',
|
|
|
|
assets: {
|
2023-02-19 17:44:53 +01:00
|
|
|
fileCreatedAt: 'ASC',
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
album: {
|
|
|
|
assets: {
|
2023-02-19 17:44:53 +01:00
|
|
|
fileCreatedAt: 'ASC',
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll(userId: string): Promise<SharedLinkEntity[]> {
|
|
|
|
return this.repository.find({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
assets: true,
|
2023-02-28 01:28:45 +01:00
|
|
|
album: {
|
|
|
|
owner: true,
|
|
|
|
},
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
createdAt: 'DESC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-01 22:56:37 +02:00
|
|
|
async getByKey(key: Buffer): Promise<SharedLinkEntity | null> {
|
2023-01-25 17:35:28 +01:00
|
|
|
return await this.repository.findOne({
|
|
|
|
where: {
|
2023-06-01 22:56:37 +02:00
|
|
|
key,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
assets: true,
|
|
|
|
album: {
|
2023-02-18 21:58:55 +01:00
|
|
|
assets: true,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
2023-01-31 19:11:49 +01:00
|
|
|
user: true,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
createdAt: 'DESC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
create(entity: Omit<SharedLinkEntity, 'id'>): Promise<SharedLinkEntity> {
|
|
|
|
return this.repository.save(entity);
|
|
|
|
}
|
|
|
|
|
2023-05-15 19:30:53 +02:00
|
|
|
async remove(entity: SharedLinkEntity): Promise<void> {
|
|
|
|
await this.repository.remove(entity);
|
2023-01-25 17:35:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async save(entity: SharedLinkEntity): Promise<SharedLinkEntity> {
|
|
|
|
await this.repository.save(entity);
|
|
|
|
return this.repository.findOneOrFail({ where: { id: entity.id } });
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasAssetAccess(id: string, assetId: string): Promise<boolean> {
|
|
|
|
const count1 = await this.repository.count({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const count2 = await this.repository.count({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
album: {
|
|
|
|
assets: {
|
2023-02-18 21:58:55 +01:00
|
|
|
id: assetId,
|
2023-01-25 17:35:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return Boolean(count1 + count2);
|
|
|
|
}
|
|
|
|
}
|