Picsur/backend/src/collections/image-db/image-db.service.ts

133 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-02-21 21:17:44 +00:00
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
2022-07-04 15:11:42 +00:00
import { AsyncFailable, Fail, FT } from 'picsur-shared/dist/types';
2022-05-08 14:12:23 +00:00
import { FindResult } from 'picsur-shared/dist/types/find-result';
2022-05-04 19:50:11 +00:00
import { In, Repository } from 'typeorm';
import { EImageDerivativeBackend } from '../../database/entities/image-derivative.entity';
import { EImageFileBackend } from '../../database/entities/image-file.entity';
import { EImageBackend } from '../../database/entities/image.entity';
2022-02-21 21:17:44 +00:00
@Injectable()
2022-02-21 21:36:47 +00:00
export class ImageDBService {
2022-02-21 21:17:44 +00:00
constructor(
2022-03-01 21:05:59 +00:00
@InjectRepository(EImageBackend)
2022-06-27 15:37:37 +00:00
private readonly imageRepo: Repository<EImageBackend>,
@InjectRepository(EImageFileBackend)
2022-06-27 15:37:37 +00:00
private readonly imageFileRepo: Repository<EImageFileBackend>,
2022-04-21 15:08:37 +00:00
@InjectRepository(EImageDerivativeBackend)
2022-06-27 15:37:37 +00:00
private readonly imageDerivativeRepo: Repository<EImageDerivativeBackend>,
2022-02-21 21:17:44 +00:00
) {}
2022-05-02 13:03:01 +00:00
public async create(userid: string): AsyncFailable<EImageBackend> {
2022-03-01 21:05:59 +00:00
let imageEntity = new EImageBackend();
2022-05-02 13:03:01 +00:00
imageEntity.user_id = userid;
imageEntity.created = new Date();
2022-02-26 17:16:28 +00:00
2022-02-21 21:17:44 +00:00
try {
2022-05-02 13:03:01 +00:00
imageEntity = await this.imageRepo.save(imageEntity, { reload: true });
2022-04-18 14:43:27 +00:00
} catch (e) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Database, e);
2022-02-21 21:17:44 +00:00
}
2022-02-26 17:16:28 +00:00
2022-04-02 21:25:49 +00:00
return imageEntity;
2022-02-21 21:17:44 +00:00
}
2022-05-04 19:50:11 +00:00
public async findOne(
id: string,
userid: string | undefined,
): AsyncFailable<EImageBackend> {
2022-02-23 10:10:25 +00:00
try {
const found = await this.imageRepo.findOne({
2022-05-04 19:50:11 +00:00
where: { id, user_id: userid },
});
2022-07-04 15:11:42 +00:00
if (!found) return Fail(FT.NotFound, 'Image not found');
return found;
2022-04-18 14:43:27 +00:00
} catch (e) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Database, e);
2022-02-23 10:10:25 +00:00
}
}
public async findMany(
2022-03-27 21:56:25 +00:00
count: number,
page: number,
2022-05-04 19:50:11 +00:00
userid: string | undefined,
2022-05-08 14:12:23 +00:00
): AsyncFailable<FindResult<EImageBackend>> {
2022-07-04 15:11:42 +00:00
if (count < 1 || page < 0) return Fail(FT.UsrValidation, 'Invalid page');
if (count > 100) return Fail(FT.UsrValidation, 'Too many results');
2022-03-27 21:56:25 +00:00
2022-02-23 10:10:25 +00:00
try {
2022-05-08 14:12:23 +00:00
const [found, amount] = await this.imageRepo.findAndCount({
2022-03-27 21:56:25 +00:00
skip: count * page,
take: count,
2022-05-03 19:37:20 +00:00
where: {
2022-05-04 19:50:11 +00:00
user_id: userid,
2022-05-03 19:37:20 +00:00
},
2022-02-23 10:10:25 +00:00
});
2022-03-27 21:56:25 +00:00
2022-07-04 15:11:42 +00:00
if (found === undefined) return Fail(FT.NotFound, 'Images not found');
2022-06-05 10:20:16 +00:00
2022-05-08 14:12:23 +00:00
return {
results: found,
2022-06-27 13:23:06 +00:00
total: amount,
2022-05-08 14:12:23 +00:00
page,
pages: Math.ceil(amount / count),
};
2022-04-18 14:43:27 +00:00
} catch (e) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Database, e);
2022-02-23 10:10:25 +00:00
}
2022-02-21 21:17:44 +00:00
}
2022-06-27 14:23:23 +00:00
public async delete(
2022-05-04 19:50:11 +00:00
ids: string[],
userid: string | undefined,
): AsyncFailable<EImageBackend[]> {
if (ids.length === 0) return [];
2022-07-04 15:11:42 +00:00
if (ids.length > 500) return Fail(FT.UsrValidation, 'Too many results');
2022-05-04 19:50:11 +00:00
try {
2022-06-27 14:23:23 +00:00
const deletable_images = await this.imageRepo.find({
2022-05-04 19:50:11 +00:00
where: {
id: In(ids),
user_id: userid,
},
});
2022-06-27 14:23:23 +00:00
const available_ids = deletable_images.map((i) => i.id);
2022-05-04 19:50:11 +00:00
2022-07-04 15:11:42 +00:00
if (available_ids.length === 0) return Fail(FT.NotFound, 'Images not found');
2022-05-04 19:50:11 +00:00
2022-06-27 14:23:23 +00:00
await Promise.all([
this.imageDerivativeRepo.delete({
image_id: In(available_ids),
}),
this.imageFileRepo.delete({
image_id: In(available_ids),
}),
this.imageRepo.delete({ id: In(available_ids) }),
]);
return deletable_images;
2022-04-18 14:43:27 +00:00
} catch (e) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Database, e);
2022-02-21 21:17:44 +00:00
}
}
2022-03-27 21:56:25 +00:00
public async deleteAll(IAmSure: boolean): AsyncFailable<true> {
if (!IAmSure)
2022-07-04 15:11:42 +00:00
return Fail(FT.SysValidation, 'You must confirm that you want to delete all images');
2022-03-27 21:56:25 +00:00
2022-02-25 13:13:19 +00:00
try {
2022-04-21 15:08:37 +00:00
await this.imageDerivativeRepo.delete({});
await this.imageFileRepo.delete({});
await this.imageRepo.delete({});
2022-04-18 14:43:27 +00:00
} catch (e) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Database, e);
2022-02-25 13:13:19 +00:00
}
return true;
}
2022-02-21 21:17:44 +00:00
}