From a2d583d9743e871448b8620b404d7a55ea3c88dd Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Thu, 23 Sep 2021 20:36:26 +0200
Subject: [PATCH] add document model

---
 app/adapters/application.js               |  9 +++++++++
 app/models/document.js                    |  6 ++++++
 app/routes/document.js                    | 10 ++++------
 app/routes/index.js                       | 13 +++++--------
 app/serializers/application.js            |  5 +++++
 public/api/document/1.json                |  2 +-
 public/api/{nodes.json => documents.json} |  4 ++--
 7 files changed, 32 insertions(+), 17 deletions(-)
 create mode 100644 app/adapters/application.js
 create mode 100644 app/models/document.js
 create mode 100644 app/serializers/application.js
 rename public/api/{nodes.json => documents.json} (90%)

diff --git a/app/adapters/application.js b/app/adapters/application.js
new file mode 100644
index 0000000..9e22e85
--- /dev/null
+++ b/app/adapters/application.js
@@ -0,0 +1,9 @@
+import JSONAPIAdapter from '@ember-data/adapter/json-api';
+
+export default class ApplicationAdapter extends JSONAPIAdapter {
+  namespace = 'api';
+
+  buildURL(...args) {
+    return `${super.buildURL(...args)}.json`;
+  }
+}
\ No newline at end of file
diff --git a/app/models/document.js b/app/models/document.js
new file mode 100644
index 0000000..96df438
--- /dev/null
+++ b/app/models/document.js
@@ -0,0 +1,6 @@
+import Model, { attr } from '@ember-data/model';
+
+export default class DocumentModel extends Model {
+  @attr title;
+  @attr image;
+}
\ No newline at end of file
diff --git a/app/routes/document.js b/app/routes/document.js
index 3f69d45..84d7ae6 100644
--- a/app/routes/document.js
+++ b/app/routes/document.js
@@ -1,13 +1,11 @@
 import Route from '@ember/routing/route';
+import { inject as service } from '@ember/service';
 
 export default class RentalRoute extends Route {
-  async model(params) {
-    let response = await fetch(`/api/document/${params.document_id}.json`);
-    let { data } = await response.json();
 
-    let { attributes } = data;
-    let type = 'document';
+  @service store;
 
-    return { type, ...attributes };
+  async model(params) {
+    return this.store.findRecord('document', params.document_id);
   }
 }
diff --git a/app/routes/index.js b/app/routes/index.js
index d914bbc..d24bebe 100644
--- a/app/routes/index.js
+++ b/app/routes/index.js
@@ -1,15 +1,12 @@
 import Route from '@ember/routing/route';
+import { inject as service } from '@ember/service';
+
 
 export default class IndexRoute extends Route {
-  async model() {
-    let response = await fetch('/api/nodes.json');
-    let { data } = await response.json();
 
-    return data.map((model) => {
-      let { attributes } = model;
-      let type = 'document';
+  @service store;
 
-      return { type, ...attributes };
-    });
+  async model() {
+    return this.store.findAll('document');
   }
 }
diff --git a/app/serializers/application.js b/app/serializers/application.js
new file mode 100644
index 0000000..4756f2c
--- /dev/null
+++ b/app/serializers/application.js
@@ -0,0 +1,5 @@
+import JSONAPISerializer from '@ember-data/serializer/json-api';
+
+export default class ApplicationSerializer extends JSONAPISerializer {
+    
+}
\ No newline at end of file
diff --git a/public/api/document/1.json b/public/api/document/1.json
index 06a7334..f38f84a 100644
--- a/public/api/document/1.json
+++ b/public/api/document/1.json
@@ -1,8 +1,8 @@
 {
     "data": {
         "type": "document",
+        "id": 1,
         "attributes": {
-            "id": 1,
             "title": "Invoice 1.pdf",
             "image": "https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg"
         }
diff --git a/public/api/nodes.json b/public/api/documents.json
similarity index 90%
rename from public/api/nodes.json
rename to public/api/documents.json
index f161ba3..e3af1fe 100644
--- a/public/api/nodes.json
+++ b/public/api/documents.json
@@ -2,16 +2,16 @@
     "data": [
         {
             "type": "document",
+            "id": 1,
             "attributes": {
-                "id": 1,
                 "title": "Invoice 1.pdf",
                 "image": "https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg"
             }
         },
         {
             "type": "document",
+            "id": 2,
             "attributes": {
-                "id": 2,
                 "title": "Invoice 2.pdf",
                 "image": "https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg"
             }
-- 
GitLab