|
@@ -27,8 +27,6 @@ class EntityManager {
|
|
|
this.apiRequestService = apiRequestService
|
|
this.apiRequestService = apiRequestService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // TODO: renommer les variables entity en 'instance'
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* Return the repository for the model
|
|
* Return the repository for the model
|
|
|
*
|
|
*
|
|
@@ -44,12 +42,12 @@ class EntityManager {
|
|
|
* This in used internally to ensure the object is recognized as an ApiResource
|
|
* This in used internally to ensure the object is recognized as an ApiResource
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
* @protected
|
|
* @protected
|
|
|
*/
|
|
*/
|
|
|
// noinspection JSMethodCanBeStatic
|
|
// noinspection JSMethodCanBeStatic
|
|
|
- protected cast(model: typeof ApiResource, entity: ApiResource): ApiResource {
|
|
|
|
|
- return new model(entity)
|
|
|
|
|
|
|
+ protected cast(model: typeof ApiResource, instance: ApiResource): ApiResource {
|
|
|
|
|
+ return new model(instance)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -83,7 +81,7 @@ class EntityManager {
|
|
|
public newInstance(model: typeof ApiResource, properties: object = {}): ApiResource {
|
|
public newInstance(model: typeof ApiResource, properties: object = {}): ApiResource {
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
|
|
- let entity = repository.make(properties)
|
|
|
|
|
|
|
+ let instance = repository.make(properties)
|
|
|
|
|
|
|
|
// Keep track of the entity's model
|
|
// Keep track of the entity's model
|
|
|
// TODO : attendre de voir si utile ou non
|
|
// TODO : attendre de voir si utile ou non
|
|
@@ -92,28 +90,27 @@ class EntityManager {
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
if (!properties.hasOwnProperty('id') || !properties.id) {
|
|
if (!properties.hasOwnProperty('id') || !properties.id) {
|
|
|
// Object has no id yet, we give him a temporary one
|
|
// Object has no id yet, we give him a temporary one
|
|
|
- entity.id = 'tmp' + uuid4()
|
|
|
|
|
|
|
+ instance.id = 'tmp' + uuid4()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- entity = repository.save(entity)
|
|
|
|
|
|
|
+ instance = repository.save(instance)
|
|
|
|
|
|
|
|
- this.saveInitialState(model, entity)
|
|
|
|
|
- return entity
|
|
|
|
|
|
|
+ this.saveInitialState(model, instance)
|
|
|
|
|
+ return instance
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Save the entity into the store
|
|
|
|
|
|
|
+ * Save the model instance into the store
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
*/
|
|
*/
|
|
|
- public save(model: typeof ApiResource, entity: ApiResource): ApiResource {
|
|
|
|
|
- this.saveInitialState(model, entity)
|
|
|
|
|
- return this.getRepository(model).save(entity)
|
|
|
|
|
|
|
+ public save(model: typeof ApiResource, instance: ApiResource): ApiResource {
|
|
|
|
|
+ return this.getRepository(model).save(instance)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Find the entity into the store
|
|
|
|
|
|
|
+ * Find the model instance in the store
|
|
|
* TODO: comment réagit la fonction si l'id n'existe pas?
|
|
* TODO: comment réagit la fonction si l'id n'existe pas?
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
@@ -135,7 +132,7 @@ class EntityManager {
|
|
|
*/
|
|
*/
|
|
|
public async fetch(model: typeof ApiResource, id: number, forceRefresh: boolean = false): Promise<ApiResource> {
|
|
public async fetch(model: typeof ApiResource, id: number, forceRefresh: boolean = false): Promise<ApiResource> {
|
|
|
|
|
|
|
|
- // If the entity is already in the store and forceRefresh is false, return the object in store
|
|
|
|
|
|
|
+ // If the model instance is already in the store and forceRefresh is false, return the object in store
|
|
|
if (!forceRefresh) {
|
|
if (!forceRefresh) {
|
|
|
const item = this.find(model, id)
|
|
const item = this.find(model, id)
|
|
|
if (item && typeof item !== 'undefined') {
|
|
if (item && typeof item !== 'undefined') {
|
|
@@ -153,7 +150,7 @@ class EntityManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Fetch a collection of entity
|
|
|
|
|
|
|
+ * Fetch a collection of model instances
|
|
|
* The content of `query` is converted into a query-string in the request URL
|
|
* The content of `query` is converted into a query-string in the request URL
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
@@ -191,34 +188,12 @@ 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
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Persist the entity as it is in the store into the data source via the API
|
|
|
|
|
|
|
+ * Persist the model instance as it is in the store into the data source via the API
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
*/
|
|
*/
|
|
|
- public async persist(model: typeof ApiModel, entity: ApiModel) {
|
|
|
|
|
|
|
+ public async persist(model: typeof ApiModel, instance: ApiModel) {
|
|
|
// Recast in case class definition has been "lost"
|
|
// Recast in case class definition has been "lost"
|
|
|
// TODO: attendre de voir si cette ligne est nécessaire
|
|
// TODO: attendre de voir si cette ligne est nécessaire
|
|
|
// entity = this.cast(model, entity)
|
|
// entity = this.cast(model, entity)
|
|
@@ -226,10 +201,10 @@ class EntityManager {
|
|
|
let url = UrlUtils.join('api', model.entity)
|
|
let url = UrlUtils.join('api', model.entity)
|
|
|
let response
|
|
let response
|
|
|
|
|
|
|
|
- const data: any = entity.$toJson()
|
|
|
|
|
|
|
+ const data: any = instance.$toJson()
|
|
|
|
|
|
|
|
- if (!entity.isNew()) {
|
|
|
|
|
- url = UrlUtils.join(url, String(entity.id))
|
|
|
|
|
|
|
+ if (!instance.isNew()) {
|
|
|
|
|
+ url = UrlUtils.join(url, String(instance.id))
|
|
|
response = await this.apiRequestService.put(url, data)
|
|
response = await this.apiRequestService.put(url, data)
|
|
|
} else {
|
|
} else {
|
|
|
delete data.id
|
|
delete data.id
|
|
@@ -238,15 +213,15 @@ class EntityManager {
|
|
|
|
|
|
|
|
const createdEntity = this.saveResponseAsEntity(model, response)
|
|
const createdEntity = this.saveResponseAsEntity(model, response)
|
|
|
|
|
|
|
|
- if (entity.isNew()) {
|
|
|
|
|
- this.removeTempAfterPersist(model, entity.id)
|
|
|
|
|
|
|
+ if (instance.isNew()) {
|
|
|
|
|
+ this.removeTempAfterPersist(model, instance.id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return createdEntity
|
|
return createdEntity
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Send an update request (PUT) to the API with the given data on an existing entity
|
|
|
|
|
|
|
+ * Send an update request (PUT) to the API with the given data on an existing model instance
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
* @param id
|
|
* @param id
|
|
@@ -262,34 +237,34 @@ class EntityManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Delete the entity from the datasource via the API
|
|
|
|
|
|
|
+ * Delete the model instance from the datasource via the API
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
*/
|
|
*/
|
|
|
- public async delete(model: typeof ApiModel, entity: ApiResource) {
|
|
|
|
|
|
|
+ public async delete(model: typeof ApiModel, instance: ApiResource) {
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
|
|
// If object has been persisted to the datasource, send a delete request
|
|
// If object has been persisted to the datasource, send a delete request
|
|
|
- if (!entity.isNew()) {
|
|
|
|
|
- const url = UrlUtils.join('api', model.entity, String(entity.id))
|
|
|
|
|
|
|
+ if (!instance.isNew()) {
|
|
|
|
|
+ const url = UrlUtils.join('api', model.entity, String(instance.id))
|
|
|
await this.apiRequestService.delete(url)
|
|
await this.apiRequestService.delete(url)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// reactiveUpdate the store
|
|
// reactiveUpdate the store
|
|
|
- repository.destroy(entity.id)
|
|
|
|
|
|
|
+ repository.destroy(instance.id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API)
|
|
|
|
|
|
|
+ * Reset the model instance to its initial state (i.e. the state it had when it was fetched from the API)
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
*/
|
|
*/
|
|
|
- public reset(model: typeof ApiResource, entity: ApiResource) {
|
|
|
|
|
- const initialEntity = this.getInitialStateOf(model, entity.id)
|
|
|
|
|
|
|
+ public reset(model: typeof ApiResource, instance: ApiResource) {
|
|
|
|
|
+ const initialEntity = this.getInitialStateOf(model, instance.id)
|
|
|
if (initialEntity === null) {
|
|
if (initialEntity === null) {
|
|
|
- throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']')
|
|
|
|
|
|
|
+ throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + instance.id + ']')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
@@ -324,9 +299,9 @@ class EntityManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Is the entity a new one, or does it already exist in the data source (=API)
|
|
|
|
|
|
|
+ * Is the model instance a new one, or does it already exist in the data source (=API)
|
|
|
*
|
|
*
|
|
|
- * This is a convenient way of testing an entity you did not already fetch, else prefer the use of the
|
|
|
|
|
|
|
+ * This is a convenient way of testing a model instance you did not already fetch, else prefer the use of the
|
|
|
* isNew() method of ApiResource
|
|
* isNew() method of ApiResource
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
@@ -346,24 +321,46 @@ class EntityManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Save the state of the entity in the store, so this state could be be restored later
|
|
|
|
|
|
|
+ * 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
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param entity
|
|
|
|
|
|
|
+ * @param instance
|
|
|
* @private
|
|
* @private
|
|
|
*/
|
|
*/
|
|
|
- protected saveInitialState(model: typeof ApiResource, entity: ApiResource) {
|
|
|
|
|
|
|
+ protected saveInitialState(model: typeof ApiResource, instance: ApiResource) {
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
|
|
// Clone and prefix id
|
|
// Clone and prefix id
|
|
|
- const clone = _.cloneDeep(entity)
|
|
|
|
|
|
|
+ const clone = _.cloneDeep(instance)
|
|
|
clone.id = this.CLONE_PREFIX + clone.id
|
|
clone.id = this.CLONE_PREFIX + clone.id
|
|
|
|
|
|
|
|
repository.save(clone)
|
|
repository.save(clone)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Return the saved state of the entity from the store
|
|
|
|
|
|
|
+ * Return the saved state of the model instance from the store
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
* @param id
|
|
* @param id
|
|
@@ -373,39 +370,39 @@ class EntityManager {
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
|
|
// Find the clone by id
|
|
// Find the clone by id
|
|
|
- const entity = repository.find(this.CLONE_PREFIX + id)
|
|
|
|
|
- if (entity === null) {
|
|
|
|
|
|
|
+ const instance = repository.find(this.CLONE_PREFIX + id)
|
|
|
|
|
+ if (instance === null) {
|
|
|
return null
|
|
return null
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Restore the initial id
|
|
// Restore the initial id
|
|
|
- entity.id = id
|
|
|
|
|
- return entity
|
|
|
|
|
|
|
+ instance.id = id
|
|
|
|
|
+ return instance
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Delete the temporary entity 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 entity
|
|
|
* that has been returned by the api with is definitive id.
|
|
* that has been returned by the api with is definitive id.
|
|
|
*
|
|
*
|
|
|
* @param model
|
|
* @param model
|
|
|
- * @param tempEntityId
|
|
|
|
|
|
|
+ * @param tempInstanceId
|
|
|
* @private
|
|
* @private
|
|
|
*/
|
|
*/
|
|
|
- protected removeTempAfterPersist(model: typeof ApiResource, tempEntityId: number | string) {
|
|
|
|
|
|
|
+ protected removeTempAfterPersist(model: typeof ApiResource, tempInstanceId: number | string) {
|
|
|
const repository = this.getRepository(model)
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
|
|
- const entity = repository.find(tempEntityId)
|
|
|
|
|
- if (!entity || typeof entity === 'undefined') {
|
|
|
|
|
|
|
+ const instance = repository.find(tempInstanceId)
|
|
|
|
|
+ if (!instance || typeof instance === 'undefined') {
|
|
|
// TODO: il vaudrait peut-être mieux lever une erreur ici?
|
|
// TODO: il vaudrait peut-être mieux lever une erreur ici?
|
|
|
- console.error(model.entity + '/' + tempEntityId + ' does not exist!')
|
|
|
|
|
|
|
+ console.error(model.entity + '/' + tempInstanceId + ' does not exist!')
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- if (!entity.isNew()) {
|
|
|
|
|
- throw new Error('Error: Can not remove a non-temporary entity')
|
|
|
|
|
|
|
+ if (!instance.isNew()) {
|
|
|
|
|
+ throw new Error('Error: Can not remove a non-temporary model instance')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- repository.destroy(tempEntityId)
|
|
|
|
|
- repository.destroy(this.CLONE_PREFIX + tempEntityId)
|
|
|
|
|
|
|
+ repository.destroy(tempInstanceId)
|
|
|
|
|
+ repository.destroy(this.CLONE_PREFIX + tempInstanceId)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|