1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-12-05 23:18:43 +02:00

fixing sharing manager, imeplementing tests

This commit is contained in:
Patrik J. Braun
2018-02-03 15:07:50 -05:00
parent fe1b31feea
commit 7896b5ae28
3 changed files with 168 additions and 19 deletions

View File

@@ -7,29 +7,37 @@ import {PasswordHelper} from "../PasswordHelper";
export class SharingManager implements ISharingManager {
private static async removeExpiredLink() {
const connection = await SQLConnection.getConnection();
return connection
.getRepository(SharingEntity)
.createQueryBuilder("share")
.where("expires < :now", {now: Date.now()})
.delete()
.execute();
}
async findOne(filter: any): Promise<SharingDTO> {
await this.removeExpiredLink();
await SharingManager.removeExpiredLink();
const connection = await SQLConnection.getConnection();
return await connection.getRepository(SharingEntity).findOne(filter);
}
async createSharing(sharing: SharingDTO): Promise<SharingDTO> {
await this.removeExpiredLink();
await SharingManager.removeExpiredLink();
const connection = await SQLConnection.getConnection();
if (sharing.password) {
sharing.password = PasswordHelper.cryptPassword(sharing.password);
}
return await connection.getRepository(SharingEntity).save(sharing);
}
async updateSharing(inSharing: SharingDTO): Promise<SharingDTO> {
const connection = await SQLConnection.getConnection();
let sharing = await connection.getRepository(SharingEntity).findOne({
const sharing = await connection.getRepository(SharingEntity).findOne({
id: inSharing.id,
creator: inSharing.creator,
creator: <any>inSharing.creator.id,
path: inSharing.path
});
@@ -44,15 +52,5 @@ export class SharingManager implements ISharingManager {
return await connection.getRepository(SharingEntity).save(sharing);
}
private async removeExpiredLink() {
const connection = await SQLConnection.getConnection();
return connection
.getRepository(SharingEntity)
.createQueryBuilder("share")
.where("expires < :now", {now: Date.now()})
.delete()
.execute();
}
}