From 09bab29271bd5a99ee3eb5402156dd48acc7e8ca Mon Sep 17 00:00:00 2001 From: Alexandros SIDIRAS Date: Sun, 14 Apr 2024 12:38:26 +0200 Subject: [PATCH] modified functions --- src/collection/collection.repository.ts | 13 ++++++++++- src/collection/collection.service.ts | 30 ++++++++++++++----------- src/picto/picto.service.ts | 10 +++++++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/collection/collection.repository.ts b/src/collection/collection.repository.ts index 51658d9..27043e2 100644 --- a/src/collection/collection.repository.ts +++ b/src/collection/collection.repository.ts @@ -10,7 +10,7 @@ import { Picto } from 'src/entities/picto.entity'; import { User } from 'src/entities/user.entity'; import { parseNumberArray } from 'src/utilities/tools'; import { CustomRepository } from 'src/utilities/typeorm-ex.decorator'; -import { Repository } from 'typeorm'; +import { EntityManager, Repository } from 'typeorm'; import { createCollectionDto } from './dto/collection.create.dto'; import { modifyCollectionDto } from './dto/collection.modify.dto'; import { SearchCollectionDto } from './dto/collection.search.public.dto'; @@ -26,6 +26,7 @@ export class CollectionRepository extends Repository { createCollectionDto: createCollectionDto, user: User, filename: string, + manager?: EntityManager, ): Promise { let { meaning, speech, pictoIds, collectionIds, color, pictohubId } = createCollectionDto; @@ -65,7 +66,12 @@ export class CollectionRepository extends Repository { throw new NotFoundException(`filename not found`); } try { + if (!manager) { await collection.save(); + } + else { + await manager.save(collection); + } } catch (error) { throw new InternalServerErrorException(error); } @@ -77,6 +83,7 @@ export class CollectionRepository extends Repository { modifyCollectionDto: modifyCollectionDto, user: User, filename: string, + manager?: EntityManager ): Promise { let { meaning, @@ -124,7 +131,11 @@ export class CollectionRepository extends Repository { collection.color = color; } try { + if (!manager) { await collection.save(); + } else { + await manager.save(collection); + } } catch (error) { throw new InternalServerErrorException( `could not save collection properly`, diff --git a/src/collection/collection.service.ts b/src/collection/collection.service.ts index 229b454..70df1d0 100644 --- a/src/collection/collection.service.ts +++ b/src/collection/collection.service.ts @@ -34,9 +34,13 @@ export class CollectionService { return await this.collectionRepository.createQueryBuilder('collection').getCount() } - async getCollectionById(id: number, user: User): Promise { - const collection = await this.collectionRepository.findOne({ relations: ["pictos", "collections"], where: { id } }); - + async getCollectionById(id: number, user: User, manager?: EntityManager): Promise { + let collection: Collection; + if (!manager) { + collection = await this.collectionRepository.findOne({ relations: ["pictos", "collections"], where: { id } }); + } else { + collection = await manager.findOne(Collection, { relations: ["pictos", "collections"], where: { id } }); + } if (!collection) { throw new NotFoundException(`Collection with ID '${id}' not found`); } else { @@ -129,8 +133,8 @@ export class CollectionService { return this.collectionRepository.autoShare(collection, fatherCollection); } - async modifyCollection(id: number, user: User, modifyCollectionDto: modifyCollectionDto, filename: string): Promise { - const collection = await this.getCollectionById(id, user); + async modifyCollection(id: number, user: User, modifyCollectionDto: modifyCollectionDto, filename: string, manager?: EntityManager): Promise { + const collection = await this.getCollectionById(id, user, manager); const index = collection.editors.indexOf(user.username); if (collection.userId === user.id || index != -1) { modifyCollectionDto = await this.verifyOwnership(modifyCollectionDto, user); @@ -141,7 +145,7 @@ export class CollectionService { this.authService.pushNotification(admin, notification); }); } - return this.collectionRepository.modifyCollection(collection, modifyCollectionDto, user, filename); + return this.collectionRepository.modifyCollection(collection, modifyCollectionDto, user, filename, manager); } else { throw new UnauthorizedException(`User '${user.username}' is not authorized to modify this collection`); } @@ -233,11 +237,11 @@ export class CollectionService { return collection; } - async verifyOwnership(verificationDto: any, user: User) { + async verifyOwnership(verificationDto: any, user: User, manager: EntityManager = null): Promise { try { for (var i = 0; i < verificationDto.collectionIds.length; i++) { try { - const collection = await this.getCollectionById(verificationDto.collectionIds[i], user); + const collection = await this.getCollectionById(verificationDto.collectionIds[i], user, manager); } catch (error) { i = i - 1; verificationDto.collectionIds.splice(i, 1); @@ -247,7 +251,7 @@ export class CollectionService { try { for (var i = 0; i < verificationDto.pictoIds.length; i++) { try { - const picto = await this.pictoService.getPictoById(verificationDto.pictoIds[i], user); + const picto = await this.pictoService.getPictoById(verificationDto.pictoIds[i], user, manager); } catch (error) { i = i - 1; verificationDto.pictoIds.splice(i, 1); @@ -266,7 +270,7 @@ export class CollectionService { } async copyCollectionWithTransaction(fatherId: number, collectionId: number, user: User): Promise { return await this.collectionRepository.manager.transaction(async manager => { - const fatherCollection = await this.getCollectionById(fatherId, user); + const fatherCollection = await this.getCollectionById(fatherId, user, manager); const copiedId = await this.copyCollectionRecursive(fatherId, collectionId, user, manager); let fatherCollectionsIds = fatherCollection.collections.map(collection => { return collection.id; @@ -281,8 +285,8 @@ export class CollectionService { collectionIds: fatherCollectionsIds, pictohubId: null } - await this.modifyCollection(fatherId, user, modifyCollectionDto, null); - return this.getCollectionById(fatherId, user) + await this.modifyCollection(fatherId, user, modifyCollectionDto, null, manager); + return this.getCollectionById(fatherId, user, manager); }); } async copyCollectionRecursive(fatherId: number, collectionId: number, user: User, entityManager: EntityManager): Promise { @@ -304,7 +308,7 @@ export class CollectionService { userId: user.id, image : collection.image }; - const copiedCollection = await entityManager.save(Collection, createCollectionDto); + const copiedCollection = await this.collectionRepository.createCollection(createCollectionDto, user, collection.image, entityManager); return copiedCollection.id; } } catch (error) { diff --git a/src/picto/picto.service.ts b/src/picto/picto.service.ts index 10c91c0..a8648c3 100644 --- a/src/picto/picto.service.ts +++ b/src/picto/picto.service.ts @@ -12,6 +12,7 @@ import { CollectionService } from 'src/collection/collection.service'; import { deletePictoDto } from './dto/picto.delete.dto'; import { modifyCollectionDto } from 'src/collection/dto/collection.modify.dto'; import { Notif } from 'src/entities/notification.entity'; +import { EntityManager } from 'typeorm'; @Injectable() export class PictoService { @@ -24,8 +25,13 @@ export class PictoService { private collectionService : CollectionService, ) { } - async getPictoById(id: number, user : User): Promise{ - const picto = await this.pictoRepository.findOne({where : {id}}); + async getPictoById(id: number, user : User, manager?: EntityManager): Promise{ + let picto: Picto; + if (manager){ + picto = await this.pictoRepository.findOne({where : {id}}); + } else { + picto = await manager.findOne(Picto,{where : {id}, relations: ["collections"]}); + } if(!picto) { throw new NotFoundException(`Picto with ID '${id}' not found`); } else {