Pārlūkot izejas kodu

poc : remove model as 1st param of em.persist and em.save

Olivier Massot 1 gadu atpakaļ
vecāks
revīzija
009b93ec5d

+ 2 - 2
components/Layout/Header/Notification.vue

@@ -233,9 +233,9 @@ const markNotificationAsRead = (notification: Notification) => {
     isRead: true,
   })
 
-  em.persist(NotificationUsers, notificationUsers)
+  em.persist(notificationUsers)
   notification.notificationUsers = ['read']
-  em.save(Notification, notification)
+  em.save(notification)
 }
 
 /**

+ 2 - 2
components/Ui/Form.vue

@@ -232,7 +232,7 @@ const submit = async (next: string | null = null) => {
     usePageStore().loading = true
 
     // TODO: est-ce qu'il faut re-fetch l'entité après le persist?
-    const updatedEntity = await em.persist(props.model, props.entity)
+    const updatedEntity = await em.persist(props.entity)
 
     if (props.refreshProfile) {
       await refreshProfile()
@@ -362,7 +362,7 @@ const actions = computed(() => {
  */
 const onFormChange = async () => {
   if (isValid.value) {
-    em.save(props.model, props.entity)
+    em.save(props.entity)
     setIsDirty(true)
 
     if (props.onChanged) {

+ 1 - 1
components/Ui/Input/Image.vue

@@ -383,7 +383,7 @@ const saveExistingImage = async () => {
     width: cropperConfig.value.width,
   })
 
-  await em.persist(File, file.value)
+  await em.persist(file.value)
 }
 
 /**

+ 8 - 8
middleware/routing.global.ts

@@ -16,14 +16,14 @@ export default defineNuxtRouteMiddleware((to, _) => {
     const name: string = routeName?.toString() ?? ''
 
     // <<- TODO: remove after 2.5 release
-    const runtimeConfig = useRuntimeConfig()
-    if (
-      runtimeConfig.public.env === 'production' &&
-      (name === 'cmf_licence_page' || name === 'parameters_page')
-    ) {
-      const { redirectToHome } = useRedirect()
-      redirectToHome()
-    }
+    // const runtimeConfig = useRuntimeConfig()
+    // if (
+    //   runtimeConfig.public.env === 'production' &&
+    //   (name === 'cmf_licence_page' || name === 'parameters_page')
+    // ) {
+    //   const { redirectToHome } = useRedirect()
+    //   redirectToHome()
+    // }
     // ->>
 
     if (

+ 1 - 1
pages/cmf_licence_structure.vue

@@ -87,7 +87,7 @@ const submit = async () => {
 
   try {
     // Send the export request and get the receipt
-    const receipt = await em.persist(LicenceCmfOrganizationER, exportRequest)
+    const receipt = await em.persist(exportRequest)
     if (receipt.fileId === null) {
       throw new Error("Missing file's id, abort")
     }

+ 5 - 13
services/data/entityManager.ts

@@ -134,23 +134,18 @@ class EntityManager {
       instance.id = 'tmp' + uuid4()
     }
 
-    return this.save(model, instance, true)
+    return this.save(instance, true)
   }
 
   /**
    * Save the model instance into the store
    *
-   * @param model
    * @param instance
    * @param permanent Is the change already persisted in the datasource? If this is the case, the initial state of this
    *                  record is also updated.
    */
-  public save(
-    model: typeof ApiResource,
-    instance: ApiResource,
-    permanent: boolean = false,
-  ): ApiResource {
-    instance = this.cast(model, instance)
+  public save(instance: ApiResource, permanent: boolean = false): ApiResource {
+    const model = instance.constructor as typeof ApiResource
 
     if (permanent) {
       this.saveInitialState(model, instance)
@@ -270,13 +265,10 @@ class EntityManager {
   /**
    * Persist the model instance as it is in the store into the data source via the API
    *
-   * @param model
    * @param instance
    */
-  public async persist(model: typeof ApiModel, instance: ApiModel) {
-    // Recast in case class definition has been "lost"
-    // TODO: attendre de voir si cette ligne est nécessaire
-    instance = this.cast(model, instance)
+  public async persist(instance: ApiModel) {
+    const model = instance.constructor as typeof ApiModel
 
     let url = UrlUtils.join('api', model.entity)
     let response

+ 2 - 1
stores/sse.ts

@@ -12,11 +12,12 @@ export const useSseStore = defineStore('sse', () => {
     const { em } = useEntityManager()
 
     const model = await em.getModelFromIri(event.iri)
+    const instance = em.newInstance(model, JSON.parse(event.data))
 
     switch (event.operation) {
       case 'update':
       case 'create':
-        await em.save(model, JSON.parse(event.data), true)
+        em.save(instance, true)
         break
 
       case 'delete':