Browse Source

refactoring and minor fixes in entityManager

Olivier Massot 2 years ago
parent
commit
c6014277c3
1 changed files with 26 additions and 41 deletions
  1. 26 41
      services/data/entityManager.ts

+ 26 - 41
services/data/entityManager.ts

@@ -83,9 +83,9 @@ class EntityManager {
 
         let instance = repository.make(properties)
 
-        // Keep track of the entity's model
+        // Keep track of the model
         // TODO : attendre de voir si utile ou non
-        // entity.setModel(model)
+        // instance.setModel(model)
 
         // @ts-ignore
         if (!properties.hasOwnProperty('id') || !properties.id) {
@@ -93,10 +93,7 @@ class EntityManager {
             instance.id = 'tmp' + uuid4()
         }
 
-        instance = repository.save(instance)
-
-        this.saveInitialState(model, instance)
-        return instance
+        return this.save(model, instance, true)
     }
 
     /**
@@ -104,8 +101,13 @@ class EntityManager {
      *
      * @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): ApiResource {
+    public save(model: typeof ApiResource, instance: ApiResource, permanent: boolean = false): ApiResource {
+        if (permanent) {
+            this.saveInitialState(model, instance)
+        }
         return this.getRepository(model).save(instance)
     }
 
@@ -123,7 +125,7 @@ class EntityManager {
     }
 
     /**
-     * Fetch an Entity / ApiResource by its id, save it to the store and returns it
+     * Fetch an ApiModel / ApiResource by its id, save it to the store and returns it
      *
      * @param model  Model of the object to fetch
      * @param id   Id of the object to fetch
@@ -196,7 +198,7 @@ class EntityManager {
     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
-        // entity = this.cast(model, entity)
+        // instance = this.cast(model, instance)
 
         let url = UrlUtils.join('api', model.entity)
         let response
@@ -211,13 +213,17 @@ class EntityManager {
             response = await this.apiRequestService.post(url, data)
         }
 
-        const createdEntity = this.saveResponseAsEntity(model, response)
+        const hydraResponse = await HydraDenormalizer.denormalize(response)
+
+        const newInstance = this.newInstance(model, hydraResponse.data)
 
+        // Si l'instance était nouvelle avant d'être persistée, elle vient désormais de recevoir un id définitif. On
+        // peut donc supprimer l'instance temporaire.
         if (instance.isNew()) {
             this.removeTempAfterPersist(model, instance.id)
         }
 
-        return createdEntity
+        return newInstance
     }
 
     /**
@@ -233,7 +239,8 @@ class EntityManager {
         const body = JSON.stringify(data)
         const response = await this.apiRequestService.put(url, body)
 
-        return this.saveResponseAsEntity(model, response)
+        const hydraResponse = await HydraDenormalizer.denormalize(response)
+        return this.newInstance(model, hydraResponse.data)
     }
 
     /**
@@ -262,15 +269,15 @@ class EntityManager {
      * @param instance
      */
     public reset(model: typeof ApiResource, instance: ApiResource) {
-        const initialEntity = this.getInitialStateOf(model, instance.id)
-        if (initialEntity === null) {
+        const initialInstance = this.getInitialStateOf(model, instance.id)
+        if (initialInstance === null) {
             throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + instance.id + ']')
         }
 
         const repository = this.getRepository(model)
-        repository.save(initialEntity)
+        repository.save(initialInstance)
 
-        return initialEntity
+        return initialInstance
     }
 
     /**
@@ -307,7 +314,7 @@ class EntityManager {
      * @param model
      * @param id
      */
-    public isNewEntity(model: typeof ApiModel, id: number | string): boolean {
+    public isNewInstance(model: typeof ApiModel, id: number | string): boolean {
         const repository = this.getRepository(model)
 
         const item = repository.find(id)
@@ -321,29 +328,7 @@ class EntityManager {
     }
 
     /**
-     * Créé une entité à partir d'une réponse de l'api au format Hydra, l'enregistre
-     * dans le store et la retourne
-     *
-     * @param model
-     * @param response
-     * @protected
-     */
-    protected async saveResponseAsEntity(model: typeof ApiModel, response: Response) {
-        const repository = this.getRepository(model)
-
-        const hydraResponse = await HydraDenormalizer.denormalize(response)
-        const returnedEntity = this.newInstance(model, hydraResponse.data)
-
-        this.saveInitialState(model, returnedEntity)
-
-        // Save data into the store
-        repository.save(returnedEntity)
-
-        return returnedEntity
-    }
-
-    /**
-     * Save the state of the model instance in the store, so this state could be be restored later
+     * Save the state of the model instance in the store, so this state could be restored later
      *
      * @param model
      * @param instance
@@ -381,7 +366,7 @@ class EntityManager {
     }
 
     /**
-     * Delete the temporary model instance from the repo after it was persisted via the api, replaced by the entity
+     * Delete the temporary model instance from the repo after it was persisted via the api, replaced by the instance
      * that has been returned by the api with is definitive id.
      *
      * @param model