|
|
@@ -1,6 +1,6 @@
|
|
|
import ApiRequestService from "./apiRequestService";
|
|
|
import {Repository, useRepo} from "pinia-orm";
|
|
|
-import UrlBuilder from "~/services/utils/urlBuilder";
|
|
|
+import Url from "~/services/utils/url";
|
|
|
import ModelNormalizer from "./serializer/normalizer/modelNormalizer";
|
|
|
import HydraDenormalizer from "./serializer/denormalizer/hydraDenormalizer";
|
|
|
import ApiModel from "~/models/ApiModel";
|
|
|
@@ -8,7 +8,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 {AssociativeArray} from "~/types/data.d";
|
|
|
+import {AssociativeArray, Collection} from "~/types/data.d";
|
|
|
|
|
|
/**
|
|
|
* Entity manager: make operations on the models defined with the Pinia-Orm library
|
|
|
@@ -56,14 +56,41 @@ class EntityManager {
|
|
|
return entity
|
|
|
}
|
|
|
|
|
|
- private reactiveUpdate(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
|
|
|
+ /**
|
|
|
+ * On met à jour directement l'entité par référence ou la liste d'entités,
|
|
|
+ * pour maintenir la réactivité lorsque l'entité ou l'array est déclarée comme réactive
|
|
|
+ *
|
|
|
+ * Attention à ce que le sujet et la nouvelle valeur soient des objets de même type
|
|
|
+ *
|
|
|
+ * @see http://underscorejs.org/#extend
|
|
|
+ * @param subject
|
|
|
+ * @param newValue
|
|
|
+ */
|
|
|
+ public reactiveUpdate(subject: ApiResource | Array<ApiResource>, newValue: ApiResource | Array<ApiResource>) {
|
|
|
+ if (typeof subject !== typeof newValue) { // TODO: remplacer par des règles typescript
|
|
|
+ console.log('Error : subject and new value have to share the same type')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (Array.isArray(subject)) {
|
|
|
+ this.reactiveUpdateArray(subject as Array<ApiResource>, newValue as Array<ApiResource>)
|
|
|
+ } else {
|
|
|
+ this.reactiveUpdateItem(subject as ApiResource, newValue as ApiResource)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private reactiveUpdateItem(entity: ApiResource, newEntity: ApiResource) {
|
|
|
useExtend(entity, newEntity)
|
|
|
}
|
|
|
|
|
|
+ private reactiveUpdateArray(items: Array<ApiResource>, newItems: Array<ApiResource>) {
|
|
|
+ items.length = 0
|
|
|
+ newItems.forEach((f: ApiResource) => {
|
|
|
+ items.push(f)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * Fetch one Entity / ApiResource by its id, save it to the store and returns it
|
|
|
+ * Fetch an Entity / 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
|
|
|
@@ -83,7 +110,7 @@ class EntityManager {
|
|
|
}
|
|
|
|
|
|
// Else, get the object from the API
|
|
|
- const url = UrlBuilder.join('api', model.entity, String(id))
|
|
|
+ const url = Url.join('api', model.entity, String(id))
|
|
|
|
|
|
const response = await this.apiRequestService.get(url)
|
|
|
|
|
|
@@ -97,8 +124,31 @@ class EntityManager {
|
|
|
// TODO: implement
|
|
|
}
|
|
|
|
|
|
- public fetchAll(model: typeof ApiResource) {
|
|
|
- // TODO: implement
|
|
|
+ public async fetchAll(model: typeof ApiResource, page: number = 1): Promise<Collection> {
|
|
|
+ let url = Url.join('api', model.entity)
|
|
|
+
|
|
|
+ if (page !== 1) {
|
|
|
+ url = Url.join(url, '?page=' + page)
|
|
|
+ }
|
|
|
+ console.log(url)
|
|
|
+
|
|
|
+ const response = await this.apiRequestService.get(url)
|
|
|
+
|
|
|
+ // deserialize the response
|
|
|
+ const collection = HydraDenormalizer.denormalize(response)
|
|
|
+
|
|
|
+ const items = collection.data.map((attributes: object) => {
|
|
|
+ return this.new(model, attributes)
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ items,
|
|
|
+ totalItems: collection.metadata.totalItems,
|
|
|
+ firstPage: collection.metadata.firstPage,
|
|
|
+ lastPage: collection.metadata.lastPage,
|
|
|
+ nextPage: collection.metadata.nextPage,
|
|
|
+ previousPage: collection.metadata.previousPage,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -110,13 +160,13 @@ class EntityManager {
|
|
|
public async persist(model: typeof ApiModel, entity: ApiModel) {
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
- let url = UrlBuilder.join('api', model.entity)
|
|
|
+ let url = Url.join('api', model.entity)
|
|
|
let response
|
|
|
|
|
|
const data = ModelNormalizer.normalize(entity)
|
|
|
|
|
|
if (!entity.isNew()) {
|
|
|
- url = UrlBuilder.join(url, String(entity.id))
|
|
|
+ url = Url.join(url, String(entity.id))
|
|
|
response = await this.apiRequestService.put(url, data)
|
|
|
} else {
|
|
|
delete data.id
|
|
|
@@ -149,7 +199,7 @@ class EntityManager {
|
|
|
|
|
|
// If object has been persisted to the datasource, send a delete request
|
|
|
if (!entity.isNew()) {
|
|
|
- const url = UrlBuilder.join('api', model.entity, String(entity.id))
|
|
|
+ const url = Url.join('api', model.entity, String(entity.id))
|
|
|
await this.apiRequestService.delete(url)
|
|
|
}
|
|
|
|