Skip to content

Commit

Permalink
modified functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratatinator97 committed Apr 14, 2024
1 parent 11d7fd0 commit 09bab29
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
13 changes: 12 additions & 1 deletion src/collection/collection.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,6 +26,7 @@ export class CollectionRepository extends Repository<Collection> {
createCollectionDto: createCollectionDto,
user: User,
filename: string,
manager?: EntityManager,
): Promise<Collection> {
let { meaning, speech, pictoIds, collectionIds, color, pictohubId } =
createCollectionDto;
Expand Down Expand Up @@ -65,7 +66,12 @@ export class CollectionRepository extends Repository<Collection> {
throw new NotFoundException(`filename not found`);
}
try {
if (!manager) {
await collection.save();
}
else {
await manager.save(collection);
}
} catch (error) {
throw new InternalServerErrorException(error);
}
Expand All @@ -77,6 +83,7 @@ export class CollectionRepository extends Repository<Collection> {
modifyCollectionDto: modifyCollectionDto,
user: User,
filename: string,
manager?: EntityManager
): Promise<Collection> {
let {
meaning,
Expand Down Expand Up @@ -124,7 +131,11 @@ export class CollectionRepository extends Repository<Collection> {
collection.color = color;
}
try {
if (!manager) {
await collection.save();
} else {
await manager.save(collection);
}
} catch (error) {
throw new InternalServerErrorException(
`could not save collection properly`,
Expand Down
30 changes: 17 additions & 13 deletions src/collection/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export class CollectionService {
return await this.collectionRepository.createQueryBuilder('collection').getCount()
}

async getCollectionById(id: number, user: User): Promise<Collection> {
const collection = await this.collectionRepository.findOne({ relations: ["pictos", "collections"], where: { id } });

async getCollectionById(id: number, user: User, manager?: EntityManager): Promise<Collection> {
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 {
Expand Down Expand Up @@ -129,8 +133,8 @@ export class CollectionService {
return this.collectionRepository.autoShare(collection, fatherCollection);
}

async modifyCollection(id: number, user: User, modifyCollectionDto: modifyCollectionDto, filename: string): Promise<Collection> {
const collection = await this.getCollectionById(id, user);
async modifyCollection(id: number, user: User, modifyCollectionDto: modifyCollectionDto, filename: string, manager?: EntityManager): Promise<Collection> {
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);
Expand All @@ -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`);
}
Expand Down Expand Up @@ -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<any> {
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);
Expand All @@ -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);
Expand All @@ -266,7 +270,7 @@ export class CollectionService {
}
async copyCollectionWithTransaction(fatherId: number, collectionId: number, user: User): Promise<Collection> {
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;
Expand All @@ -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<number> {
Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/picto/picto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,8 +25,13 @@ export class PictoService {
private collectionService : CollectionService,
) { }

async getPictoById(id: number, user : User): Promise<Picto>{
const picto = await this.pictoRepository.findOne({where : {id}});
async getPictoById(id: number, user : User, manager?: EntityManager): Promise<Picto>{
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 {
Expand Down

0 comments on commit 09bab29

Please sign in to comment.