-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: factories in repository use name as id
- Loading branch information
Showing
5 changed files
with
104 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
packages/app/pages/db/factories/[resourceName]/view/[factoryId]/index.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
<script lang="ts" setup> | ||
import type { ResourceFactory } from '@moquerie/core' | ||
const route = useRoute() | ||
const router = useRouter() | ||
const factoryStore = useFactoryStore() | ||
const factory = await factoryStore.fetchFactory(route.params.factoryId as string) | ||
const factory = shallowRef(await factoryStore.fetchFactory(route.params.factoryId as string)) | ||
function onComplete(data: ResourceFactory) { | ||
factory.value = data | ||
router.push({ | ||
name: 'db-factories-resourceName-view-factoryId', | ||
params: { | ||
resourceName: route.params.resourceName, | ||
factoryId: data.id, | ||
}, | ||
query: { | ||
...route.query, | ||
factoryLocation: data.location, | ||
}, | ||
}) | ||
} | ||
</script> | ||
|
||
<template> | ||
<FactoryForm | ||
v-if="factory" | ||
:resource-name="$route.params.resourceName as string" | ||
:factory="factory" | ||
@complete="factory = $event" | ||
@complete="onComplete" | ||
/> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
import { nanoid } from 'nanoid' | ||
import { getFactoryStorage } from '@moquerie/core' | ||
import type { ResourceFactory } from '@moquerie/core' | ||
|
||
export default defineEventHandler<{ body: ResourceFactory }>(async (event) => { | ||
const body = await readBody(event) | ||
const storage = await getFactoryStorage() | ||
|
||
const factory = await storage.findById(body.id) | ||
if (!factory) { | ||
throw new Error(`Factory ${body.id} not found`) | ||
} | ||
|
||
if (factory.location !== body.location) { | ||
if (body.location === 'local') { | ||
// Override id with random one | ||
await storage.remove(factory.id) | ||
body.id = factory.id = nanoid() | ||
} | ||
else if (body.location === 'repository') { | ||
// Use name as id | ||
const name = body.name ?? factory.name | ||
if (await storage.findById(name, 'repository')) { | ||
throw new Error(`Factyory with name "${name}" already exists in repository`) | ||
} | ||
await storage.remove(factory.id) | ||
body.id = factory.id = name | ||
} | ||
} | ||
Object.assign(factory, body) | ||
await storage.save(factory, factory.location) | ||
return body | ||
return factory | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters