diff options
| author | Adam T. Carpenter <atc@53hor.net> | 2020-11-07 20:31:03 -0500 | 
|---|---|---|
| committer | Adam T. Carpenter <atc@53hor.net> | 2020-11-07 20:31:03 -0500 | 
| commit | 7dd36c0e699a1154c7163f25bf488fbd63edeafe (patch) | |
| tree | 1a0d76539acba7b77f1abe9fb443ac98523ed7e5 /iridescence/src/api | |
| parent | 2c408648c70614a88c0412faf6d3a00d147379a2 (diff) | |
| download | theglassyladies-7dd36c0e699a1154c7163f25bf488fbd63edeafe.tar.xz theglassyladies-7dd36c0e699a1154c7163f25bf488fbd63edeafe.zip | |
updated photo sets to not return original, added models to front,
implemented front-to-back requests
Diffstat (limited to 'iridescence/src/api')
| -rw-r--r-- | iridescence/src/api/dichroism.js | 122 | ||||
| -rw-r--r-- | iridescence/src/api/error.js | 6 | 
2 files changed, 48 insertions, 80 deletions
| 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"; +  } +} |