From ddb5dc385e3a8d4374ee0020e517e8c0315d75a1 Mon Sep 17 00:00:00 2001 From: Georg Spar Date: Mon, 26 Jun 2023 10:29:55 +0200 Subject: [PATCH] GetPhotoForIte --- wdm.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- wdmdb.go | 22 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/wdm.go b/wdm.go index 575381a..bbec441 100644 --- a/wdm.go +++ b/wdm.go @@ -41,6 +41,7 @@ func main() { 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/itb/{id:[0-9]+}", getPhotoForItbHandler).Methods("GET", "OPTIONS") + r.HandleFunc("/api/v1/pho/itb/{id:[0-9]+}", getPhotoForIteHandler).Methods("GET", "OPTIONS") r.HandleFunc("/api/v1/pho/{id:[0-9]+}", updatePhotoHandler).Methods("PUT", "OPTIONS") r.HandleFunc("/api/v1/meas", createMeasureHandler).Methods("POST", "OPTIONS") @@ -313,8 +314,26 @@ func getPhotoHandler(writer http.ResponseWriter, req *http.Request) { if req.Method == http.MethodOptions { return } - phoId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) - res, err := GetPhoto(uint64(phoId)) + err := req.ParseMultipartForm(64 << 20) + if err != nil { + writer.WriteHeader(http.StatusBadRequest) + return + } + var pho Photos + if req.Form["id"] != nil { + pho.Id, _ = strconv.ParseUint(req.FormValue("id"), 10, 0) + 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 { + var pho Photos + pho.Id, _ = strconv.ParseUint(mux.Vars(req)["id"], 10, 0) + } + res, err := GetPhoto(uint64(pho.Id)) if err != nil { log.Fatal("Error: Typ can't be shown ", err) writer.WriteHeader(http.StatusInternalServerError) @@ -351,6 +370,27 @@ func getPhotoForItbHandler(writer http.ResponseWriter, req *http.Request) { writer.WriteHeader(http.StatusOK) writer.Write(b) } +func getPhotoForIteHandler(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 := GetPhotoForIte(uint64(phoId)) + if err != nil { + log.Fatal("Error: Photo 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", "*") diff --git a/wdmdb.go b/wdmdb.go index 04d1acd..fe0dccd 100644 --- a/wdmdb.go +++ b/wdmdb.go @@ -180,6 +180,28 @@ func GetPhotoForItb(itbid uint64) (phoArray []Photos, err error) { return phoArray, err } +func GetPhotoForIte(itbid uint64) (phoArray []Photos, err error) { + conn, err := sql.Open(dbType, dbConnection) + if err != nil { + log.Println("Error while connecting DB: ", err) + } + log.Println("DB Verbindung hergestellt (Photo Itb SHOW)") + defer conn.Close() + results, err := conn.Query("SELECT * FROM photos where itemextid=?", itbid) + if err != nil { + log.Fatal("Error while executing SELECt statement", err) + } + var pho Photos + for results.Next() { + err = results.Scan(&pho.Id, &pho.Pfad, &pho.ItembId, &pho.ItemextId) + if err != nil { + log.Fatal("Error: ", err) + } + phoArray = append(phoArray, pho) + } + return phoArray, err +} + func GetPhoto(id uint64) (pho Photos, err error) { conn, err := sql.Open(dbType, dbConnection) if err != nil {