Beispiel #1
0
func productsHandler(api *apidb.Api) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setHeaders(w, r)
		if r.Method == "GET" {
			js, err := json.Marshal(api.GetAllProducts())
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			w.Header().Set("Content-Type", "application/json")
			w.Write(js)

		} else if r.Method == "PUT" {
			decoder := json.NewDecoder(r.Body)
			var product apidb.Product
			err := decoder.Decode(&product)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			js, err := json.Marshal(api.AddProduct(product))
			w.Header().Set("Content-Type", "application/json")
			w.Write(js)
		}
	}
}
Beispiel #2
0
func storesHandler(api *apidb.Api) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setHeaders(w, r)
		js, err := json.Marshal(api.GetAllStores())
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		w.Write(js)
	}
}
Beispiel #3
0
func receiptUploadsHandler(api *apidb.Api) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setHeaders(w, r)
		if r.Method == "PUT" {
			decoder := json.NewDecoder(r.Body)
			var receiptUpload apidb.ReceiptUpload
			err := decoder.Decode(&receiptUpload)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			js, err := json.Marshal(api.AddReceiptUpload(receiptUpload))
			w.Header().Set("Content-Type", "application/json")
			w.Write(js)
		}
	}
}
Beispiel #4
0
func productsHandler(api *apidb.Api) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setHeaders(w, r)
		path := r.URL.Path
		path_arr := strings.Split(path, "/")
		if len(path_arr) == 3 {
			js, err := json.Marshal(api.GetAllProducts())
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			w.Header().Set("Content-Type", "application/json")
			w.Write(js)
		} else {
			category := strings.ToLower(path_arr[2])
			var sub_category string
			if len(path_arr) == 5 {
				sub_category = strings.ToLower(path_arr[3])
			}
			allProducts := api.GetAllProducts()
			requestedProducts := make(map[int64]map[int64]map[string][]float64)
			for _, product := range allProducts {
				if strings.ToLower(product.Category) == category {
					if sub_category == "" || strings.ToLower(product.Sub_category) == sub_category {
						requestedProducts[product.Id] = make(map[int64]map[string][]float64)
					}
				}
			}
			if len(requestedProducts) == 0 {
				http.Error(w, fmt.Sprintf("found no products matching: '%[1] %[2]'", category, sub_category), 400)
				return
			}
			allPurchases := api.GetAllPurchases()
			allReceipts := api.GetAllReceipts()
			for _, purchase := range allPurchases {
				product_id := purchase.Product_id
				unitCostsByStore, ok := requestedProducts[product_id]
				if ok {
					var receipt apidb.Receipt
					for _, receiptItr := range allReceipts {
						if receiptItr.Id == purchase.Receipt_id {
							receipt = receiptItr
						}
					}
					if unitCostsByStore[receipt.Store_id] == nil {
						unitCostsByStore[receipt.Store_id] = make(map[string][]float64)
					}
					unitCost := unitCostsByStore[receipt.Store_id][purchase.Unit]
					unitCost = append(unitCost, float64(purchase.Cost)/float64(purchase.Quantity))
					requestedProducts[product_id][receipt.Store_id][purchase.Unit] = unitCost
				}
			}
			costedProducts := make(map[string]map[string]map[string]float64)
			for product_id, unitCostsByStore := range requestedProducts {
				costedProducts[strconv.FormatInt(product_id, 10)] = make(map[string]map[string]float64)
				for store_id, unitCosts := range unitCostsByStore {
					costedProducts[strconv.FormatInt(product_id, 10)][strconv.FormatInt(store_id, 10)] = make(map[string]float64)
					for unit, costs := range unitCosts {
						var averagedCost float64 = 0
						for _, cost := range costs {
							averagedCost += cost
						}
						averagedCost = averagedCost / float64(len(costs))
						costedProducts[strconv.FormatInt(product_id, 10)][strconv.FormatInt(store_id, 10)][unit] = averagedCost
					}
				}
			}
			js, err := json.Marshal(costedProducts)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			w.Header().Set("Content-Type", "application/json")
			w.Write(js)
		}
	}
}
Beispiel #5
0
func purchasesHandler(api *apidb.Api) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setHeaders(w, r)
		vars := mux.Vars(r)
		productId, err := strconv.ParseInt(vars["productId"], 10, 64)
		storeId, err := strconv.ParseInt(vars["storeId"], 10, 64)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		stores := api.GetAllStores()
		var foundStore apidb.Store
		for _, store := range stores {
			if store.Id == storeId {
				foundStore = store
				break
			}
		}

		products := api.GetAllProducts()
		var foundProduct apidb.Product
		for _, product := range products {
			if product.Id == productId {
				foundProduct = product
				break
			}
		}

		if foundStore.Id != storeId || foundProduct.Id != productId {
			http.Error(w, "cannot find store or product", http.StatusInternalServerError)
			return
		}

		receipts := api.GetAllReceipts()
		var foundReceipts []apidb.Receipt = make([]apidb.Receipt, 0)
		for _, receipt := range receipts {
			if receipt.Store_id == foundStore.Id {
				foundReceipts = append(foundReceipts, receipt)
			}
		}

		purchases := api.GetAllPurchases()
		var foundPurchases []apidb.Purchase = make([]apidb.Purchase, 0)
		for _, purchase := range purchases {
			if purchase.Product_id == foundProduct.Id {
				for _, receipt := range foundReceipts {
					if purchase.Receipt_id == receipt.Id {
						foundPurchases = append(foundPurchases, purchase)
						break
					}
				}
			}
		}

		w.Header().Set("Content-Type", "application/json")
		js, err := json.Marshal(foundPurchases)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Write(js)
	}
}