You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"spargcom/senuma/database"
"spargcom/senuma/domain"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/artikel", createArtikelHandler).Methods("POST")
//r.HandleFunc("/groups", createGroupHandler).Methods("POST")
//r.HandleFunc("/users", createUserHandler).Methods("POST")
//r.HandleFunc("/accounts", createAccountHandler).Methods("POST")
//r.HandleFunc("/tunnels", createTunnelHandler).Methods("POST")
http.ListenAndServe(":8081", r)
fmt.Println("running on localhost, Port 8081")
}
func createArtikelHandler(writer http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
}
var art domain.Artikel
json.Unmarshal(body, &art)
fmt.Println("Bezeichnung = ", art.Bezeichnung)
fmt.Println("externe Art-Nr = ", art.ArtNrExt)
res, err := database.CreateArtikel(art)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
}
b, err := json.Marshal(res)
if err != nil {
fmt.Println("Error: ", err)
}
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id)
writer.Header().Set("Location:", location)
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusCreated)
writer.Write(b)
}