Skip to content

Commit

Permalink
edit many paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Feb 23, 2022
1 parent ebeb3a6 commit 297582f
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 65 deletions.
9 changes: 7 additions & 2 deletions api/src/graphql/images.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const schema = gql`
image(id: String!): Image @requireAuth
}
type EditManyResult {
count: Int!
}
type DeleteManyResult {
count: Int!
}
Expand All @@ -35,8 +38,10 @@ export const schema = gql`
deleteManyImages(imageIds: [String!]!): DeleteManyResult! @requireAuth
deleteManyImagesWithFilter(filter: ImageFilters!): DeleteManyResult!
@requireAuth
editImageBasePath(imageId: String!, basePath: String!): Boolean!
@requireAuth
editImagesBasePath(
imageIds: [String!]!
basePath: String!
): EditManyResult! @requireAuth
}
input ImageFilters {
Expand Down
156 changes: 114 additions & 42 deletions api/src/services/images/images.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
deleteManyImages,
deleteManyImagesWithFilter,
editImageBasePath,
editImagesBasePath,
image,
images,
moreImages,
Expand Down Expand Up @@ -279,7 +279,9 @@ describe('images', () => {
const imagesToDelete = [scenario.image.p1, scenario.image.p3]

const result = await deleteManyImages({
imageIds: imagesToDelete.map((i) => i.id),
imageIds: ['not-existing']
.concat(imagesToDelete.map((i) => i.id))
.concat('neither'),
})

expect(result.count).toEqual(imagesToDelete.length)
Expand Down Expand Up @@ -343,53 +345,123 @@ describe('images', () => {
)
})
describe('edit', () => {
async function testEditImageBasePath(imageToEdit, basePath, newPath) {
await Buckets.photos.put(imageToEdit.path, imageToEdit.path)
await Buckets.miniatures.put(imageToEdit.path, imageToEdit.path)
async function testEditImagesBasePath(
imagesToEdit,
basePath,
newPaths,
count
) {
for (const i in imagesToEdit) {
const imageToEdit = imagesToEdit[i]
if (!imageToEdit) continue
await Buckets.photos.put(imageToEdit.path, imageToEdit.path)
await Buckets.miniatures.put(imageToEdit.path, imageToEdit.path)
}

const result = await editImageBasePath({
imageId: imageToEdit.id,
const result = await editImagesBasePath({
imageIds: imagesToEdit.map((i) => (i ? i.id : 'numbid')),
basePath,
})

expect(result).toBe(true)
expect(result.count).toBe(count)

const resImage = await image({ id: imageToEdit.id })
expect(resImage.path).toEqual(newPath)
for (const i in imagesToEdit) {
const imageToEdit = imagesToEdit[i]
if (!imageToEdit) continue
const newPath = newPaths[i]
const resImage = await image({ id: imageToEdit.id })
expect(resImage.path).toEqual(newPath)

expect((await Buckets.photos.get(newPath)).toString()).toEqual(
imageToEdit.path
)
expect((await Buckets.miniatures.get(newPath)).toString()).toEqual(
imageToEdit.path
)
if (imageToEdit.path !== newPath) {
expect(await Buckets.photos.get(imageToEdit.path)).toBeNull()
expect(await Buckets.miniatures.get(imageToEdit.path)).toBeNull()
expect((await Buckets.photos.get(newPath)).toString()).toEqual(
imageToEdit.path
)
expect((await Buckets.miniatures.get(newPath)).toString()).toEqual(
imageToEdit.path
)
if (imageToEdit.path !== newPath) {
expect(await Buckets.photos.get(imageToEdit.path)).toBeNull()
expect(await Buckets.miniatures.get(imageToEdit.path)).toBeNull()
}
}
}
scenario('editImageBasePath 1', async (scenario: StandardScenario) => {
await testEditImageBasePath(
scenario.image.p3,
'patate/labas',
'patate/labas/p3.jpg'
)
})
scenario('editImageBasePath 2', async (scenario: StandardScenario) => {
await testEditImageBasePath(
scenario.image.p5,
'caca/caca',
'caca/caca/p5.jpg'
)
})
scenario('editImageBasePath 3', async (scenario: StandardScenario) => {
await testEditImageBasePath(scenario.image.p5, '', 'p5.jpg')
})
scenario('editImageBasePath 4', async (scenario: StandardScenario) => {
await testEditImageBasePath(scenario.image.p4, 'ath3', 'ath3/p4.jpg')
})
scenario('editImageBasePath 5', async (scenario: StandardScenario) => {
await testEditImageBasePath(scenario.image.p4, '', 'p4.jpg')
})
scenario(
'editImagesBasePath image with path',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p3],
'patate/labas',
['patate/labas/p3.jpg'],
1
)
}
)
scenario(
'editImagesBasePath image without path',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p5],
'caca/caca',
['caca/caca/p5.jpg'],
1
)
}
)

scenario(
'editImagesBasePath image mixed path',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p3, scenario.image.p5],
'ici/labas',
['ici/labas/p3.jpg', 'ici/labas/p5.jpg'],
2
)
}
)
scenario(
'editImagesBasePath image one same path',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p4, scenario.image.p5],
'ath3',
['ath3/p4.jpg', 'ath3/p5.jpg'],
1
)
}
)
scenario(
'editImagesBasePath keep same path nopath',
async (scenario: StandardScenario) => {
await testEditImagesBasePath([scenario.image.p5], '', ['p5.jpg'], 0)
}
)
scenario(
'editImagesBasePath keep same path with path',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p4],
'ath3',
['ath3/p4.jpg'],
0
)
}
)
scenario(
'editImagesBasePath remove basepath',
async (scenario: StandardScenario) => {
await testEditImagesBasePath([scenario.image.p4], '', ['p4.jpg'], 1)
}
)
scenario(
'editImagesBasePath with not existing id',
async (scenario: StandardScenario) => {
await testEditImagesBasePath(
[scenario.image.p4, null],
'papapapa/papap',
['papapapa/papap/p4.jpg'],
1
)
}
)
})
})
50 changes: 29 additions & 21 deletions api/src/services/images/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
QueryimagesArgs,
MutationdeleteManyImagesArgs,
MutationdeleteManyImagesWithFilterArgs,
MutationeditImageBasePathArgs,
MutationeditImagesBasePathArgs,
Mutation,
} from 'types/graphql'

Expand Down Expand Up @@ -113,7 +113,7 @@ export const deleteManyImages = async ({
id: imageId,
},
})
if (!image) break
if (!image) continue
await Buckets.photos.delete(image.path)
await Buckets.miniatures.delete(image.path)
await db.image.delete({
Expand Down Expand Up @@ -141,25 +141,33 @@ export const deleteManyImagesWithFilter = async ({
return deleteManyImages({ imageIds: imagesToApply.map((i) => i.id) })
}

export const editImageBasePath = async ({
imageId,
export const editImagesBasePath = async ({
imageIds,
basePath,
}: MutationeditImageBasePathArgs): Promise<Mutation['editImageBasePath']> => {
const imageToEdit = await image({ id: imageId })
if (!imageToEdit) {
throw new Error('NOT_FOUND')
}: MutationeditImagesBasePathArgs): Promise<Mutation['editImagesBasePath']> => {
let count = 0
for (const i in imageIds) {
const imageId = imageIds[i]
const imageToEdit = await image({ id: imageId })
if (!imageToEdit) {
continue
}
const fileName = getFileName(imageToEdit.path)
const path = getPath(basePath, fileName)
if (path === imageToEdit.path) continue

await db.image.update({
where: { id: imageId },
data: {
path,
},
})
await Buckets.photos.editKey(imageToEdit.path, path)
await Buckets.miniatures.editKey(imageToEdit.path, path)

count++
}
return {
count,
}
const fileName = getFileName(imageToEdit.path)
const path = getPath(basePath, fileName)
if (path === imageToEdit.path) return true

await db.image.update({
where: { id: imageId },
data: {
path,
},
})
await Buckets.photos.editKey(imageToEdit.path, path)
await Buckets.miniatures.editKey(imageToEdit.path, path)
return true
}

0 comments on commit 297582f

Please sign in to comment.