Georg Spar
1 year ago
5 changed files with 356 additions and 0 deletions
@ -0,0 +1,53 @@
|
||||
package main |
||||
|
||||
// struct für das Einbinden von Items, die gekauft wurden
|
||||
type Itemb struct { |
||||
Id uint64 `json:"id"` |
||||
Bezeichnung string `json:"bezeichnung"` |
||||
Kurzbeschreibung string `json:"kurzbeschreibung"` |
||||
Groesse string `json:"groesse"` |
||||
Farbe string `json:"farbe"` |
||||
TypId uint `json:"typid"` |
||||
Material string `json:"material"` |
||||
AbmessungenId uint64 `json:"abmessungenid"` |
||||
} |
||||
|
||||
// struct für die Fotos, die im Filesystem abgelegt werden sollen
|
||||
type Photos struct { |
||||
Id uint64 `json:"id"` |
||||
Pfad string `json:"pfad"` |
||||
ItembId uint64 `json:"itembid"` |
||||
ItemextId uint64 `json:"itemextid"` |
||||
} |
||||
|
||||
// struct für die Typen von Kleidung
|
||||
type Typ struct { |
||||
Id uint `json:"id"` |
||||
Bezeichnung string `json:"bezeichnung"` |
||||
} |
||||
|
||||
// struct für die Groessenangaben der Kleidungsstücke
|
||||
type Measure struct { |
||||
Id uint64 `json:"id"` |
||||
Brust float32 `json:"brust"` |
||||
LaengeT float32 `json:"laenget"` |
||||
Schulterbreite float32 `json:"schulterbreite"` |
||||
TaillenumfangT float32 `json:"taillenumfangt"` |
||||
Hueftumfang float32 `json:"hueftumfang"` |
||||
Innennaht float32 `json:"innennaht"` |
||||
LaengeB float32 `json:"laengeb"` |
||||
TaillenumfangB float32 `json:"taillenumfangb"` |
||||
} |
||||
|
||||
// struct für die Erweiterung des Items für die Benutzung
|
||||
// zusätzliche Felder für Bewertung und Favoriten
|
||||
type ItemExt struct { |
||||
Id uint64 `json:"id"` |
||||
Zuhause bool `json:"zuhause"` |
||||
Draussen bool `json:"draussen"` |
||||
Genehmigt bool `json:"genehmigt"` |
||||
Bemerkung string `json:"bemerkung"` |
||||
Bewertung int `json:"bewertung"` |
||||
Favorit bool `json:"favorit"` |
||||
ItemBId uint64 `json:"itembid"` |
||||
} |
@ -0,0 +1,15 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= |
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= |
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= |
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= |
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= |
||||
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= |
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= |
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
@ -0,0 +1,181 @@
|
||||
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 |
||||
} |
||||
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 |
||||
} |
||||
typId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
||||
log.Info("Deleting Instance 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) |
||||
} |
@ -0,0 +1,104 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"database/sql" |
||||
"log" |
||||
|
||||
_ "github.com/go-sql-driver/mysql" |
||||
) |
||||
|
||||
var ( |
||||
dbConnection = "wdmdbuser:wdmdbpass@tcp(ubodroid-2:3300)/wdmdb" |
||||
dbType = "mysql" |
||||
) |
||||
|
||||
// Tool Funktionen
|
||||
func CreateTyp(typ Typ) (resTyp Typ, err error) { |
||||
conn, err := sql.Open(dbType, dbConnection) |
||||
if err != nil { |
||||
log.Println("Error while connecting DB: ", err) |
||||
} |
||||
log.Println("Verbindung hergestellt") |
||||
log.Println("Name = ", typ.Bezeichnung) |
||||
defer conn.Close() |
||||
res, err := conn.Exec("INSERT INTO typ VALUES(?,?)", typ.Id, typ.Bezeichnung) |
||||
if err != nil { |
||||
log.Println("Error while executing insert statement", err) |
||||
} |
||||
lastId, err := res.LastInsertId() |
||||
typ.Id = uint(lastId) |
||||
resTyp = typ |
||||
return resTyp, err |
||||
} |
||||
|
||||
func DeleteTyp(typId uint) (result, err error) { |
||||
conn, err := sql.Open(dbType, dbConnection) |
||||
if err != nil { |
||||
log.Println("Error while connecting DB: ", err) |
||||
} |
||||
log.Println("DB Verbindung hergestellt") |
||||
defer conn.Close() |
||||
_, err = conn.Exec("DELETE FROM ncinstanz WHERE id = ?", typId) |
||||
if err != nil { |
||||
log.Fatal("Error while executing DELETE statement", err) |
||||
} |
||||
return result, err |
||||
} |
||||
|
||||
func ShowTyp() (typArray []Typ, err error) { |
||||
conn, err := sql.Open(dbType, dbConnection) |
||||
if err != nil { |
||||
log.Println("Error while connecting DB: ", err) |
||||
} |
||||
log.Println("DB Verbindung hergestellt") |
||||
defer conn.Close() |
||||
results, err := conn.Query("SELECT * FROM typ") |
||||
if err != nil { |
||||
log.Fatal("Error while executing SELECt statement", err) |
||||
} |
||||
var typ Typ |
||||
for results.Next() { |
||||
err = results.Scan(&typ.Id, typ.Bezeichnung) |
||||
if err != nil { |
||||
log.Fatal("Error: ", err) |
||||
} |
||||
typArray = append(typArray, typ) |
||||
} |
||||
return typArray, err |
||||
} |
||||
|
||||
func GetTyp(id uint) (typ Typ, err error) { |
||||
conn, err := sql.Open(dbType, dbConnection) |
||||
if err != nil { |
||||
log.Println("Error while connecting DB: ", err) |
||||
} |
||||
log.Println("DB Verbindung hergestellt") |
||||
defer conn.Close() |
||||
results, err := conn.Query("SELECT * FROM typ WHERE id=?", id) |
||||
if err != nil { |
||||
log.Fatal("Error while executing SELECt statement", err) |
||||
} |
||||
for results.Next() { |
||||
err = results.Scan(&typ.Id, &typ.Bezeichnung) |
||||
if err != nil { |
||||
log.Fatal("Error: ", err) |
||||
} |
||||
} |
||||
return typ, err |
||||
} |
||||
|
||||
func UpdateTyp(typ Typ) (resTyp Typ, err error) { |
||||
conn, err := sql.Open(dbType, dbConnection) |
||||
if err != nil { |
||||
log.Println("Error while connecting DB: ", err) |
||||
} |
||||
log.Println("Verbindung hergestellt") |
||||
log.Println("Name = ", typ.Bezeichnung) |
||||
defer conn.Close() |
||||
_, err = conn.Exec("UPDATE typ SET bezeichnung=? WHERE id=?", typ.Bezeichnung, typ.Id) |
||||
if err != nil { |
||||
log.Println("Error while executing insert statement", err) |
||||
} |
||||
resTyp = typ |
||||
return resTyp, err |
||||
} |
Loading…
Reference in new issue