Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transactions #21

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class AuthController {
await this.collectionService.createSider(user);
if (createUserDto.publicBundleId) {
const publicBundleCollection: Collection = await this.collectionService.getCollectionById(createUserDto.publicBundleId, user);
const pictoIdsFromBundle: number[] = await Promise.all(publicBundleCollection?.pictos.map(async (picto) => await this.collectionService.copyPicto(rootId, picto, user)));
const collectionIdsFromBundle: number[] = await Promise.all(publicBundleCollection?.collections.map(async (collection) => await this.collectionService.copyCollection(rootId, collection.id, user)))
const pictoIdsFromBundle: number[] = await Promise.all(publicBundleCollection?.pictos.map(async (picto) => this.collectionService.copyPicto(rootId, picto, user)));
const collectionIdsFromBundle: number[] = await Promise.all(publicBundleCollection?.collections.map(async (collection) => this.collectionService.copyCollectionWithTransaction(rootId, collection.id, user)));
const modifyCollectionDto : modifyCollectionDto = {
meaning : null,
speech : null,
Expand All @@ -52,7 +52,6 @@ export class AuthController {
const multipleShareCollectionDto: multipleShareCollectionDto = { access: 1, usernames: user.directSharers, role: 'editor'}
await this.collectionService.shareCollectionVerification(rootId, user, multipleShareCollectionDto);
}
console.log(await this.collectionService.getCollectionById(rootId, user))
return;
}

Expand Down
19 changes: 2 additions & 17 deletions src/collection/collection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,8 @@ export class CollectionController {
@UseGuards(AuthGuard())
@Post('copy')
@ApiOperation({summary : 'copy a collection with its ID'})
async copyCollection(@Body() copyCollectionDto: copyCollectionDto, @GetUser() user: User): Promise<Collection>{
const fatherCollection = await this.collectionService.getCollectionById(copyCollectionDto.fatherCollectionId, user);
const copiedId = await this.collectionService.copyCollection(copyCollectionDto.fatherCollectionId, copyCollectionDto.collectionId, user);
let fatherCollectionsIds = fatherCollection.collections.map(collection => {
return collection.id;
})
fatherCollectionsIds.push(copiedId);
const modifyCollectionDto : modifyCollectionDto = {
meaning : null,
speech : null,
pictoIds : null,
priority : 10,
color : null,
collectionIds : fatherCollectionsIds,
pictohubId: null
}
await this.collectionService.modifyCollection(copyCollectionDto.fatherCollectionId, user, modifyCollectionDto, null);
async copyCollection(@Body() copyCollectionDto: copyCollectionDto, @GetUser() user: User): Promise<Collection>{
await this.collectionService.copyCollectionWithTransaction(copyCollectionDto.fatherCollectionId, copyCollectionDto.collectionId, user);
return this.getCollectionById(copyCollectionDto.fatherCollectionId, user);
}

Expand Down
14 changes: 13 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, 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,8 +131,13 @@ export class CollectionRepository extends Repository<Collection> {
collection.color = color;
}
try {
if (!manager) {
await collection.save();
} else {
await manager.save(Collection, collection);
}
} catch (error) {
console.error(error);
throw new InternalServerErrorException(
`could not save collection properly`,
);
Expand Down
Loading
Loading