diff options
-rw-r--r-- | dichroism/src/dtos/photo_set_get.rs | 2 | ||||
-rw-r--r-- | iridescence/src/api/dichroism.js | 122 | ||||
-rw-r--r-- | iridescence/src/api/error.js | 6 | ||||
-rw-r--r-- | iridescence/src/models/photo_set.js | 10 | ||||
-rw-r--r-- | iridescence/src/models/product.js | 16 | ||||
-rw-r--r-- | iridescence/src/store/index.js | 6 |
6 files changed, 77 insertions, 85 deletions
diff --git a/dichroism/src/dtos/photo_set_get.rs b/dichroism/src/dtos/photo_set_get.rs index 0736617..b2e8d7b 100644 --- a/dichroism/src/dtos/photo_set_get.rs +++ b/dichroism/src/dtos/photo_set_get.rs @@ -3,7 +3,6 @@ use crate::models::PhotoSet; #[derive(Debug, Serialize)] pub struct PhotoSetGet { pub id: i32, - pub original: String, pub fullsize: String, pub base: String, pub thumbnail: String, @@ -13,7 +12,6 @@ impl From<PhotoSet> for PhotoSetGet { fn from(p: PhotoSet) -> Self { Self { id: p.id.unwrap_or(-1), - original: p.original.id, fullsize: p.fullsize.id, base: p.base.id, thumbnail: p.thumbnail.id, diff --git a/iridescence/src/api/dichroism.js b/iridescence/src/api/dichroism.js index 7821eb3..e3b08a8 100644 --- a/iridescence/src/api/dichroism.js +++ b/iridescence/src/api/dichroism.js @@ -1,93 +1,55 @@ +import Product from "../models/product"; +import PhotoSet from "../models/photo_set"; +import ApiError from "./error"; + export default class Dichroism { - constructor() { - this.products = [ - { - id: 1, - name: "Beach Box", - quantity: 0, - cents: 1100, - imgPath: "/beach_box.jpg", - description: "This is a beach box.", - featured: false, - categories: ["Fused Glass", "Beachy"] - }, - { - id: 2, - name: "Wind Chime", - quantity: 0, - cents: 4500, - imgPath: "/wind-chime.jpg", - description: "Makes noise when the wind blows.", - featured: false, - categories: ["Fused Glass", "Beachy"] - }, - { - id: 3, - name: "Beach Box", - quantity: 5, - cents: 1100, - imgPath: "/beach_box.jpg", - description: "This is a beach box.", - featured: false, - categories: ["Stained Glass", "Christmas"] - }, - { - id: 4, - name: "Wind Chime", - quantity: 2, - cents: 4500, - imgPath: "/wind-chime.jpg", - description: "Makes noise when the wind blows.", - featured: false, - categories: ["Fused Glass", "Kiln-y"] - }, - { - id: 5, - name: "Beach Box (New!)", - quantity: 5, - cents: 1100, - imgPath: "/beach_box.jpg", - description: "This is a beach box.", - featured: true, - categories: ["Stained Glass", "Christmas"] - } - ]; + _base_addr = "http://localhost:8000/"; + + async createPhoto(file) { + const fd = new FormData(); + fd.append(file.name, file); + + const options = { + method: "POST", + body: fd + }; + + const photos = await self._sendRequest("photos", options); + return photos.map(p => new PhotoSet(p)); } + async getProducts() { + const products = await self._sendRequest("products", null); + return products.map(p => new Product(p)); + } - async uploadPhoto(file) { - const fd = new FormData(); - fd.append(file.name, file); + async updateProduct(fieldDiff) { + const options = { + method: "PATCH", + body: fieldDiff + }; - return fetch("http://localhost:8000/photos", { - method: "POST", - body: fd - }); - } + const product = await self._sendRequest("products", options); + return new Product(product); + } + async createProduct(newProduct) { + const options = { + method: "POST", + body: newProduct + }; - getProducts() { - return this.products.slice(0); + const product = await self._sendRequest("products", options); + return new Product(product); } - updateProduct(newProduct) { - if (!newProduct) { - return; - } + async _sendRequest(endpoint, options) { + const response = await fetch(self.base_addr + endpoint, options); - for (let i = 0; i < this.products.length; i++) { - if (newProduct.id == this.products[i].id) { - let currentProduct = this.products[i]; - currentProduct.id = newProduct.id; - currentProduct.name = newProduct.name; - currentProduct.quantity = newProduct.quantity; - currentProduct.cents = newProduct.cents; - currentProduct.imgPath = newProduct.imgPath; - currentProduct.description = newProduct.description; - currentProduct.featured = newProduct.featured; - currentProduct.categories = newProduct.categories.slice(0); - return; - } + if (response.ok) { + return await response.json(); + } else { + return new ApiError(await response.text()); } } } diff --git a/iridescence/src/api/error.js b/iridescence/src/api/error.js new file mode 100644 index 0000000..7c9320d --- /dev/null +++ b/iridescence/src/api/error.js @@ -0,0 +1,6 @@ +export default class ApiError extends Error { + constructor(message) { + super(message); + this.name = "ApiError"; + } +} diff --git a/iridescence/src/models/photo_set.js b/iridescence/src/models/photo_set.js new file mode 100644 index 0000000..7d7213c --- /dev/null +++ b/iridescence/src/models/photo_set.js @@ -0,0 +1,10 @@ +export default class PhotoSet { + id = 0; + fullsize = ""; + thumbnail = ""; + base = ""; + + constructor(json) { + Object.assign(this, json); + } +} diff --git a/iridescence/src/models/product.js b/iridescence/src/models/product.js new file mode 100644 index 0000000..c826073 --- /dev/null +++ b/iridescence/src/models/product.js @@ -0,0 +1,16 @@ +export default class Product { + id = 0; + name = ""; + description = ""; + cents = 0; + quantity = 0; + featured = false; + photo_base = ""; + photo_fullsize = ""; + photo_thumbnail = ""; + category = ""; + + constructor(json) { + Object.assign(this, json); + } +} diff --git a/iridescence/src/store/index.js b/iridescence/src/store/index.js index 1a83c01..2b24816 100644 --- a/iridescence/src/store/index.js +++ b/iridescence/src/store/index.js @@ -4,7 +4,7 @@ import Dichroism from "@/api/dichroism.js"; Vue.use(Vuex); -let dichroismApi = new Dichroism(); +let dichroism = new Dichroism(); export default new Vuex.Store({ state: { @@ -34,10 +34,10 @@ export default new Vuex.Store({ }, actions: { refreshProducts(context) { - context.commit("setProducts", dichroismApi.getProducts()); + context.commit("setProducts", dichroism.getProducts()); }, updateProduct(context, product) { - dichroismApi.updateProduct(product); + dichroism.updateProduct(product); context.dispatch("refreshProducts"); } }, |