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.
678 lines
20 KiB
678 lines
20 KiB
package main |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"io" |
|
"net/http" |
|
"os" |
|
"strconv" |
|
|
|
"github.com/gorilla/mux" |
|
log "github.com/sirupsen/logrus" |
|
) |
|
|
|
const ( |
|
Author string = "Georg Spar" |
|
Version string = "0.1.0" |
|
ReleaseDate string = "2023-06-01" |
|
) |
|
|
|
func main() { |
|
fmt.Println("This is WardrobeMaster Version " + Version) |
|
file, err := os.OpenFile("wdmapi.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer file.Close() |
|
log.SetOutput(file) |
|
log.SetLevel(log.InfoLevel) |
|
r := mux.NewRouter() |
|
|
|
r.HandleFunc("/api/v1/typ", createTypHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", deleteTypHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ", showTypHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", getTypHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", updateTypHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/pho", createPhotoHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", deletePhotoHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho", showPhotoHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", getPhotoHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", updatePhotoHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/meas", createMeasureHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", deleteMeasureHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas", showMeasureHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", getMeasureHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", updateMeasureHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/itb", createItemBHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", deleteItemBHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb", showItemBHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", getItemBHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", updateItemBHandler).Methods("PUT", "OPTIONS") |
|
/* |
|
r.HandleFunc("/api/v1/ite", createItemExtHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", deleteItemExtHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite", showItemExtHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", getItemExtHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", updateItemExtHandler).Methods("PUT", "OPTIONS") */ |
|
|
|
http.Handle("/", r) |
|
r.Use(mux.CORSMethodMiddleware(r)) |
|
http.ListenAndServe(":8081", r) |
|
log.Info("running on localhost, Port 8081") |
|
} |
|
|
|
// Typ Funktionen |
|
func createTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var typ Typ |
|
if req.Form["bezeichnung"] != nil { |
|
typ.Bezeichnung = req.FormValue("bezeichnung") |
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var typ Typ |
|
json.Unmarshal(body, &typ) |
|
} |
|
|
|
log.Println(typ) |
|
log.Println("Name= ", typ.Bezeichnung) |
|
log.Info("creating new Typ", typ.Bezeichnung) |
|
res, err := CreateTyp(typ) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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) |
|
} |
|
|
|
func deleteTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
typId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
log.Info("Deleting Typ with ID", typId) |
|
_, err = DeleteTyp(uint(typId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := ShowTyp() |
|
if err != nil { |
|
log.Fatal("Error: Typ can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
typId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
res, err := GetTyp(uint(typId)) |
|
if err != nil { |
|
log.Fatal("Error: Typ can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var typ Typ |
|
json.Unmarshal(body, &typ) |
|
log.Println("Name = ", typ.Bezeichnung) |
|
log.Info("updating Typ", typ.Bezeichnung) |
|
res, err := UpdateTyp(typ) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// Photo Funktionen |
|
func createPhotoHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var pho Photos |
|
if req.Form["pfad"] != nil { |
|
pho.ItembId, _ = strconv.ParseUint(req.FormValue("itembid"), 10, 0) |
|
pho.ItemextId, _ = strconv.ParseUint(req.FormValue("itemextid"), 10, 0) |
|
pho.Pfad = req.FormValue("pfad") |
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var pho Photos |
|
json.Unmarshal(body, &pho) |
|
} |
|
|
|
log.Println(pho) |
|
log.Println("Name= ", pho.Pfad) |
|
log.Info("creating new Photo", pho.Pfad) |
|
res, err := CreatePhoto(pho) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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) |
|
} |
|
|
|
func deletePhotoHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
phoId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
log.Info("Deleting Photo with ID", phoId) |
|
_, err = DeletePhoto(uint64(phoId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showPhotoHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := ShowPhoto() |
|
if err != nil { |
|
log.Fatal("Error: Photos can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getPhotoHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
phoId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
res, err := GetPhoto(uint64(phoId)) |
|
if err != nil { |
|
log.Fatal("Error: Typ can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updatePhotoHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var pho Photos |
|
json.Unmarshal(body, &pho) |
|
log.Println("Name = ", pho.Pfad) |
|
log.Info("updating Photo", pho.Pfad) |
|
res, err := UpdatePhoto(pho) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// Measure Funktionen |
|
func createMeasureHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var mea Measure |
|
if req.Form["kurz"] != nil { |
|
mea.Kurz = req.FormValue("kurz") |
|
mea.Brust, _ = strconv.ParseFloat(req.FormValue("brust"), 64) |
|
mea.LaengeT, _ = strconv.ParseFloat(req.FormValue("laenget"), 64) |
|
mea.Schulterbreite, _ = strconv.ParseFloat(req.FormValue("schulterbreite"), 64) |
|
mea.TaillenumfangT, _ = strconv.ParseFloat(req.FormValue("taillenumfangt"), 64) |
|
mea.Hueftumfang, _ = strconv.ParseFloat(req.FormValue("hueftumfang"), 64) |
|
mea.Innennaht, _ = strconv.ParseFloat(req.FormValue("innennaht"), 64) |
|
mea.LaengeB, _ = strconv.ParseFloat(req.FormValue("laengeb"), 64) |
|
mea.TaillenumfangB, _ = strconv.ParseFloat(req.FormValue("taillenumfangb"), 64) |
|
|
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var mea Measure |
|
json.Unmarshal(body, &mea) |
|
} |
|
|
|
log.Println(mea) |
|
log.Println("Name= ", mea.Kurz) |
|
log.Info("creating new Measure", mea.Kurz) |
|
res, err := CreateMeasure(mea) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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) |
|
} |
|
|
|
func deleteMeasureHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
meaId, _ := strconv.ParseUint(req.FormValue("id"), 10, 0) |
|
log.Info("Deleting Measure with ID", meaId) |
|
_, err = DeleteMeasure(uint64(meaId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showMeasureHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := ShowMeasure() |
|
if err != nil { |
|
log.Fatal("Error: Measure can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getMeasureHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
meaId, _ := strconv.ParseUint(req.FormValue("id"), 10, 0) |
|
res, err := GetMeasure(uint64(meaId)) |
|
if err != nil { |
|
log.Fatal("Error: Measure can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateMeasureHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var mea Measure |
|
if req.Form["kurz"] != nil { |
|
mea.Kurz = req.FormValue("kurz") |
|
mea.Brust, _ = strconv.ParseFloat(req.FormValue("brust"), 64) |
|
mea.LaengeT, _ = strconv.ParseFloat(req.FormValue("laenget"), 64) |
|
mea.Schulterbreite, _ = strconv.ParseFloat(req.FormValue("schulterbreite"), 64) |
|
mea.TaillenumfangT, _ = strconv.ParseFloat(req.FormValue("taillenumfangt"), 64) |
|
mea.Hueftumfang, _ = strconv.ParseFloat(req.FormValue("hueftumfang"), 64) |
|
mea.Innennaht, _ = strconv.ParseFloat(req.FormValue("innennaht"), 64) |
|
mea.LaengeB, _ = strconv.ParseFloat(req.FormValue("laengeb"), 64) |
|
mea.TaillenumfangB, _ = strconv.ParseFloat(req.FormValue("taillenumfangb"), 64) |
|
|
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var mea Measure |
|
json.Unmarshal(body, &mea) |
|
} |
|
|
|
log.Println("Name = ", mea.Kurz) |
|
log.Info("updating Measure", mea.Kurz) |
|
res, err := UpdateMeasure(mea) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// ItemsB Funktionen |
|
func createItemBHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var itemB Itemb |
|
if req.Form["bezeichnung"] != nil { |
|
itemB.Bezeichnung = req.FormValue("bezeichnung") |
|
itemB.Kurzbeschreibung = req.FormValue("kurzbeschreibung") |
|
itemB.Material = req.FormValue("material") |
|
itemB.Groesse = req.FormValue("groesse") |
|
itemB.Farbe = req.FormValue("farbe") |
|
itbtyp, _ := strconv.ParseUint(req.FormValue("typid"), 10, 0) |
|
itemB.Type.Id = uint(itbtyp) |
|
itemB.Abmessungen.Id, _ = strconv.ParseUint(req.FormValue("abmessungenid"), 10, 0) |
|
|
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var itemB Itemb |
|
json.Unmarshal(body, &itemB) |
|
} |
|
|
|
log.Println(itemB) |
|
log.Println("Name= ", itemB.Bezeichnung) |
|
log.Info("creating new Typ", itemB.Bezeichnung) |
|
res, err := CreateItemB(itemB) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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) |
|
} |
|
|
|
func deleteItemBHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
itemBId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
log.Info("Deleting Typ with ID", itemBId) |
|
_, err = DeleteItemB(uint64(itemBId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showItemBHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := ShowItemB() |
|
if err != nil { |
|
log.Fatal("Error: Typ can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getItemBHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
itemBId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
res, err := GetItemB(uint64(itemBId)) |
|
if err != nil { |
|
log.Fatal("Error: Typ can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateItemBHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
|
|
err := req.ParseMultipartForm(64 << 20) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
return |
|
} |
|
var itemB Itemb |
|
if req.Form["bezeichnung"] != nil { |
|
itemB.Bezeichnung = req.FormValue("bezeichnung") |
|
itemB.Kurzbeschreibung = req.FormValue("kurzbeschreibung") |
|
itemB.Material = req.FormValue("material") |
|
itemB.Groesse = req.FormValue("groesse") |
|
itemB.Farbe = req.FormValue("farbe") |
|
itbtyp, _ := strconv.ParseUint(req.FormValue("typid"), 10, 0) |
|
itemB.Type.Id = uint(itbtyp) |
|
itemB.Abmessungen.Id, _ = strconv.ParseUint(req.FormValue("abmessungenid"), 10, 0) |
|
|
|
for formKey, formValue := range req.Form { |
|
log.WithFields(log.Fields{formKey: formValue}).Info("parsing from Form: ") |
|
} |
|
|
|
} else { |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var itemB Itemb |
|
json.Unmarshal(body, &itemB) |
|
} |
|
|
|
log.Println("Name = ", itemB.Bezeichnung) |
|
log.Info("updating Typ", itemB.Bezeichnung) |
|
res, err := UpdateItemB(itemB) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("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.StatusOK) |
|
writer.Write(b) |
|
}
|
|
|