|
|
@@ -9,6 +9,7 @@ import {useProfileAccessStore} from "~/store/profile/access";
|
|
|
import ApiResource from "~/models/ApiResource";
|
|
|
import {MyProfile} from "~/models/Access/MyProfile";
|
|
|
import { v4 as uuid4 } from 'uuid';
|
|
|
+import {useOrmStore} from "~/store/orm";
|
|
|
|
|
|
/**
|
|
|
* Entity manager: make operations on the models defined with the Pinia-Orm library
|
|
|
@@ -16,12 +17,10 @@ import { v4 as uuid4 } from 'uuid';
|
|
|
* @see https://pinia-orm.codedredd.de/
|
|
|
*/
|
|
|
class EntityManager {
|
|
|
- private initialState: Map<string, Map<number, ApiResource>>;
|
|
|
private apiRequestService: ApiRequestService;
|
|
|
|
|
|
public constructor(apiRequestService: ApiRequestService) {
|
|
|
this.apiRequestService = apiRequestService
|
|
|
- this.initialState = new Map<string, Map<number, ApiResource>>()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -33,6 +32,35 @@ class EntityManager {
|
|
|
return useRepo(model)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Create a new instance of the given model
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @param properties
|
|
|
+ */
|
|
|
+ public new(model: typeof ApiResource, properties: object = {}) {
|
|
|
+ const repository = this.getRepository(model)
|
|
|
+
|
|
|
+ const entity = repository.make(properties)
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ if (!properties.hasOwnProperty('id') || !properties.id) {
|
|
|
+ // Object has no id yet, we give him a temporary one
|
|
|
+ entity.id = 'tmp' + uuid4()
|
|
|
+ }
|
|
|
+ repository.save(entity)
|
|
|
+
|
|
|
+ EntityManager.saveInitialState(model, entity)
|
|
|
+
|
|
|
+ return entity
|
|
|
+ }
|
|
|
+
|
|
|
+ private update(entity: ApiResource, newEntity: ApiResource) {
|
|
|
+ // On met à jour l'entité par référence, pour maintenir la réactivité lorsque l'entité est réactive
|
|
|
+ // @see http://underscorejs.org/#extend
|
|
|
+ useExtend(entity, newEntity)
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Fetch one Entity / ApiResource by its id, save it to the store and returns it
|
|
|
*
|
|
|
@@ -48,7 +76,6 @@ class EntityManager {
|
|
|
// If the entity is already in the store and forceRefresh is false, return the object in store
|
|
|
if (!forceRefresh) {
|
|
|
const item = repository.find(id)
|
|
|
- console.log(model, id, ' => item : ', item)
|
|
|
if (item && typeof item !== 'undefined') {
|
|
|
return item
|
|
|
}
|
|
|
@@ -80,7 +107,6 @@ class EntityManager {
|
|
|
* @param entity
|
|
|
*/
|
|
|
public async persist(model: typeof ApiModel, entity: ApiModel) {
|
|
|
-
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
let url = UrlBuilder.join('api', model.entity)
|
|
|
@@ -92,13 +118,14 @@ class EntityManager {
|
|
|
url = UrlBuilder.join(url, String(entity.id))
|
|
|
response = await this.apiRequestService.put(url, data)
|
|
|
} else {
|
|
|
+ delete data.id
|
|
|
response = await this.apiRequestService.post(url, data)
|
|
|
}
|
|
|
|
|
|
- const attributes = await HydraDenormalizer.denormalize(response)
|
|
|
- const returnedEntity = this.new(model, attributes)
|
|
|
+ const hydraResponse = await HydraDenormalizer.denormalize(response)
|
|
|
+ const returnedEntity = this.new(model, hydraResponse.data)
|
|
|
|
|
|
- this.storeInitialState(returnedEntity)
|
|
|
+ EntityManager.saveInitialState(model, returnedEntity)
|
|
|
|
|
|
// Save data into the store
|
|
|
repository.save(returnedEntity)
|
|
|
@@ -107,69 +134,46 @@ class EntityManager {
|
|
|
await this.refreshProfile()
|
|
|
}
|
|
|
|
|
|
- return returnedEntity
|
|
|
+ this.update(entity, returnedEntity)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delete the entity from the datasource via the API
|
|
|
*
|
|
|
* @param model
|
|
|
- * @param id
|
|
|
+ * @param entity
|
|
|
*/
|
|
|
- public async delete(model: typeof ApiModel, id: number) {
|
|
|
+ public async delete(model: typeof ApiModel, entity: ApiResource) {
|
|
|
const repository = this.getRepository(model)
|
|
|
- const entity = repository.find(id) as ApiModel
|
|
|
-
|
|
|
- if (!entity || typeof entity === 'undefined') {
|
|
|
- throw new Error(model + ' ' + id + ' does not exists in store')
|
|
|
- }
|
|
|
|
|
|
// If object has been persisted to the datasource, send a delete request
|
|
|
if (!entity.isNew()) {
|
|
|
- const url = UrlBuilder.join('api', model.entity, String(id))
|
|
|
+ const url = UrlBuilder.join('api', model.entity, String(entity.id))
|
|
|
await this.apiRequestService.delete(url)
|
|
|
}
|
|
|
|
|
|
// update the store
|
|
|
- repository.destroy(id)
|
|
|
+ repository.destroy(entity.id)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Create a new instance of the given model
|
|
|
+ * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API)
|
|
|
*
|
|
|
* @param model
|
|
|
- * @param properties
|
|
|
+ * @param entity
|
|
|
*/
|
|
|
- public new(model: typeof ApiResource, properties: object = {}) {
|
|
|
- const repository = this.getRepository(model)
|
|
|
-
|
|
|
- const entity = repository.make(properties)
|
|
|
-
|
|
|
- if (!entity.id) {
|
|
|
- // Object has no id yet, we give him a temporary one
|
|
|
- entity.id = 'tmp' + uuid4()
|
|
|
+ public reset(model: typeof ApiResource, entity: ApiResource) {
|
|
|
+ const initialEntity = EntityManager.getInitialStateOf(model, entity.id)
|
|
|
+ if (initialEntity === null) {
|
|
|
+ throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']')
|
|
|
}
|
|
|
- repository.save(entity)
|
|
|
|
|
|
- this.storeInitialState(entity)
|
|
|
+ EntityManager.saveInitialState(model, initialEntity)
|
|
|
|
|
|
- return entity
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API)
|
|
|
- *
|
|
|
- * @param entity
|
|
|
- */
|
|
|
- public reset(entity: ApiResource) {
|
|
|
- const state = this.getInitialStateOf(entity)
|
|
|
- if (state === null) {
|
|
|
- console.log('no initial state recorded for this object - abort', entity)
|
|
|
- return
|
|
|
- }
|
|
|
+ const repository = this.getRepository(model)
|
|
|
+ repository.save(initialEntity)
|
|
|
|
|
|
- const repository = this.getRepository(EntityManager.getModelOf(entity))
|
|
|
- repository.save(state)
|
|
|
+ this.update(entity, initialEntity)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -179,9 +183,9 @@ class EntityManager {
|
|
|
const response = await this.apiRequestService.get('api/my_profile')
|
|
|
|
|
|
// deserialize the response
|
|
|
- const attributes = await HydraDenormalizer.denormalize(response).data
|
|
|
+ const hydraResponse = await HydraDenormalizer.denormalize(response)
|
|
|
|
|
|
- const profile = this.new(MyProfile, attributes)
|
|
|
+ const profile = this.new(MyProfile, hydraResponse.data)
|
|
|
|
|
|
const profileAccessStore = useProfileAccessStore()
|
|
|
profileAccessStore.setProfile(profile)
|
|
|
@@ -197,41 +201,52 @@ class EntityManager {
|
|
|
repository.flush()
|
|
|
}
|
|
|
|
|
|
- private storeInitialState(entity: ApiResource) {
|
|
|
- const model = EntityManager.getModelOf(entity)
|
|
|
+ /**
|
|
|
+ * Is the entity a new one, or does it already exist in the data source (=API)
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ public isNewEntity(model: typeof ApiModel, id: number | string): boolean {
|
|
|
+ const repository = this.getRepository(model)
|
|
|
|
|
|
- if (!this.initialState.has(model.entity)) {
|
|
|
- this.initialState.set(model.entity, new Map())
|
|
|
+ const item = repository.find(id)
|
|
|
+ if (!item || typeof item === 'undefined') {
|
|
|
+ console.error(model.entity + '/' + id, ' does not exist!')
|
|
|
+ return false
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore
|
|
|
- this.initialState.get(model.entity).set(entity.id, useCloneDeep(entity))
|
|
|
-
|
|
|
- // TODO: voir si structuredClone est compatible avec les navigateurs supportés, et si ça vaut le coup
|
|
|
- // de l'utiliser à la place du clonedeep de lodash
|
|
|
- // >> https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
|
|
|
+ return item.isNew()
|
|
|
}
|
|
|
|
|
|
- private getInitialStateOf(entity: ApiResource): ApiResource | null {
|
|
|
- const model = EntityManager.getModelOf(entity)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- if (!this.initialState.has(model.entity) || !this.initialState.get(model.entity).has(entity.id)) {
|
|
|
- return null
|
|
|
- }
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- return this.initialState.get(model.entity).get(entity.id)
|
|
|
+ /**
|
|
|
+ * Save the state of the entity in the store, so this state could be be restored later
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @param entity
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+ private static saveInitialState(model: typeof ApiResource, entity: ApiResource) {
|
|
|
+ useOrmStore().storeInitialValue(model, entity)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Return the model (=class) of the given entity
|
|
|
+ * Return the saved state of the entity from the store
|
|
|
*
|
|
|
- * @param entity
|
|
|
+ * @param model
|
|
|
+ * @param id
|
|
|
* @private
|
|
|
*/
|
|
|
- private static getModelOf(entity: ApiResource): typeof ApiResource {
|
|
|
- return Object.getPrototypeOf(entity)
|
|
|
+ private static getInitialStateOf(model: typeof ApiResource, id: number): ApiResource | null {
|
|
|
+ const ormStore = useOrmStore()
|
|
|
+
|
|
|
+ //@ts-ignore
|
|
|
+ if (!ormStore.initialValues.has(model.entity) || !ormStore.initialValues.get(model.entity).has(id)) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ //@ts-ignore
|
|
|
+ return ormStore.initialValues.get(model.entity).get(id)
|
|
|
}
|
|
|
}
|
|
|
|