Skip to content
Snippets Groups Projects
Select Git revision
  • 8823
  • develop default
  • stream_docs
  • 8824_incomplete_blobs_transfer
  • upstreaming_blobs
  • streaming_blobs
  • 9001_isolation
  • fix_couch_url_tests
  • test_tac
  • 8961_delete_propagation
  • 8970_refactor_blobs
  • fix/154
  • crash_recovery/blobs_sync
  • master
  • fix_144
  • fix_141_review
  • bug/8945_sql_concurrency
  • 8924
  • 8867
  • fix_tests_eb958baf
  • 8928
  • 0.8.1
  • 0.8.0
  • 0.7.4
  • 0.7.3
  • 0.7.2
  • 0.7.1
  • 0.7.0
  • 0.6.5
  • 0.6.4
  • 0.6.3
  • 0.6.2
  • 0.6.1
  • 0.6.0
  • 0.5.2
  • 0.5.1
  • 0.5.0
  • 0.4.5
  • 0.4.4
  • 0.4.3
  • 0.4.2
41 results

versioneer.py

Blame
  • Forked from leap / soledad
    Source project has a limited visibility.
    inventary.go 3.03 KiB
    package api
    
    import (
    	"encoding/json"
    	"log"
    	"net/http"
    	"strconv"
    
    	"0xacab.org/meskio/cicer/api/db"
    	"github.com/gorilla/mux"
    )
    
    func (a *api) AddSupplier(w http.ResponseWriter, req *http.Request) {
    	var supplier db.Supplier
    	err := json.NewDecoder(req.Body).Decode(&supplier)
    	if err != nil {
    		log.Printf("Can't decode supplier: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    	err = a.db.AddSupplier(&supplier)
    	if err != nil {
    		log.Printf("Can't create supplier: %v\n%v", err, supplier)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    
    	w.Header().Set("Content-Type", "application/json")
    	w.WriteHeader(http.StatusCreated)
    	err = json.NewEncoder(w).Encode(supplier)
    	if err != nil {
    		log.Printf("Can't encode added supplier: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    }
    
    func (a *api) ListSuppliers(w http.ResponseWriter, req *http.Request) {
    	suppliers, err := a.db.ListSuppliers()
    	if err != nil {
    		log.Printf("Can't list suppliers: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    	w.Header().Set("Content-Type", "application/json")
    	w.WriteHeader(http.StatusOK)
    	err = json.NewEncoder(w).Encode(suppliers)
    	if err != nil {
    		log.Printf("Can't encode suppliers: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    }
    
    func (a *api) AddInventary(num int, w http.ResponseWriter, req *http.Request) {
    	var inventary db.Inventary
    	err := json.NewDecoder(req.Body).Decode(&inventary)
    	if err != nil {
    		log.Printf("Can't decode inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    	err = a.db.AddInventary(num, &inventary)
    	if err != nil {
    		log.Printf("Can't create inventary: %v\n%v", err, inventary)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    
    	w.Header().Set("Content-Type", "application/json")
    	w.WriteHeader(http.StatusCreated)
    	err = json.NewEncoder(w).Encode(inventary)
    	if err != nil {
    		log.Printf("Can't encode added inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    }
    
    func (a *api) GetInventary(w http.ResponseWriter, req *http.Request) {
    	vars := mux.Vars(req)
    	id, _ := strconv.Atoi(vars["id"])
    	inventary, err := a.db.GetInventary(id)
    	if err != nil {
    		log.Printf("Can't get inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    	w.Header().Set("Content-Type", "application/json")
    	w.WriteHeader(http.StatusOK)
    	err = json.NewEncoder(w).Encode(inventary)
    	if err != nil {
    		log.Printf("Can't encode inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    }
    
    func (a *api) ListInventary(w http.ResponseWriter, req *http.Request) {
    	inventary, err := a.db.ListInventary()
    	if err != nil {
    		log.Printf("Can't list inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    	w.Header().Set("Content-Type", "application/json")
    	w.WriteHeader(http.StatusOK)
    	err = json.NewEncoder(w).Encode(inventary)
    	if err != nil {
    		log.Printf("Can't encode inventary: %v", err)
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    }