From b4a4d35291c65b68661e6019352a3c2ec03b3274 Mon Sep 17 00:00:00 2001 From: Georg Spar Date: Tue, 27 Apr 2021 19:19:24 +0200 Subject: [PATCH] Create Statements --- spargcom/gocart/database/database.go | 52 ++++++++++++++++++++++++--- spargcom/gocart/domain/domain.go | 19 +++++++--- spargcom/gocart/rester/rest.go | 54 ++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 8 deletions(-) diff --git a/spargcom/gocart/database/database.go b/spargcom/gocart/database/database.go index 775a13e..ba94783 100644 --- a/spargcom/gocart/database/database.go +++ b/spargcom/gocart/database/database.go @@ -4,6 +4,7 @@ import ( "database/sql" "fmt" "spargcom/gocart/domain" + "time" _ "github.com/go-sql-driver/mysql" ) @@ -27,7 +28,7 @@ func CreatePackage(pk domain.Package) (resPk domain.Package, err error) { fmt.Println("Error while executing insert statement", err) } lastId, err := res.LastInsertId() - pk.Id = int(lastId) + pk.Id = uint(lastId) resPk = pk return resPk, err @@ -46,7 +47,7 @@ func CreateUser(us domain.User) (resUs domain.User, err error) { fmt.Println("Error while executing insert statement", err) } lastId, err := res.LastInsertId() - us.Id = int(lastId) + us.Id = uint(lastId) resUs = us return resUs, err @@ -65,12 +66,55 @@ func CreateGroup(grp domain.Group) (resGrp domain.Group, err error) { fmt.Println("Error while executing insert statement", err) } lastId, err := res.LastInsertId() - grp.Id = int(lastId) + grp.Id = uint(lastId) resGrp = grp return resGrp, err } -func CreateAccount(us domain.User, grp domain.Group) { +func CreateAccount(us domain.User) (resAcc domain.Account, err error) { + + var acc domain.Account + fmt.Println("User ID:", us.Id) + fmt.Println("Group ID:", us.GroupId) + conn, err := sql.Open(dbType, dbConnection) + if err != nil { + fmt.Println("Error while connecting DB: ", err) + } + fmt.Println("Verbindung hergestellt") + defer conn.Close() + stmt, err := conn.Prepare("INSERT INTO `account` (`id`, `group_id`, `created`, `is_active`) VALUES(?,?,?,?)") + + res, err := stmt.Exec(acc.Id, us.GroupId, time.Now(), acc.IsActive) + if err != nil { + fmt.Println("Error while executing insert statement", err) + } + lastId, err := res.LastInsertId() + acc.Id = uint(lastId) + resAcc = acc + fmt.Println("Created") + return resAcc, err + +} + +func CreateTunnel(tun domain.Tunnel) (resTun domain.Tunnel, err error) { + fmt.Println("Adding tunnel to database...") + conn, err := sql.Open(dbType, dbConnection) + if err != nil { + fmt.Println("Error while connecting DB: ", err) + + } + fmt.Println("Verbindung hergestellt") + defer conn.Close() + stmt, err := conn.Prepare("INSERT INTO `tunnels` (`id`, `name`, `port`, `is_active`, `created`, `routed_name`, `routed_port`, `account_id`) VALUES(?,?,?,?,?,?,?,?)") + + res, err := stmt.Exec(tun.Id, tun.Name, tun.Port, tun.IsActive, time.Now(), tun.RoutedName, tun.RoutedPort, tun.AccountId) + if err != nil { + fmt.Println("Error while executing insert statement", err) + } + lastId, err := res.LastInsertId() + tun.Id = uint64(lastId) + resTun = tun fmt.Println("Created") + return resTun, err } diff --git a/spargcom/gocart/domain/domain.go b/spargcom/gocart/domain/domain.go index 993fc4a..0dc5529 100644 --- a/spargcom/gocart/domain/domain.go +++ b/spargcom/gocart/domain/domain.go @@ -1,7 +1,7 @@ package domain type Package struct { - Id int `json:"id"` + Id uint `json:"id"` Name string `json:"name"` Amount int `json:"amount"` Price float32 `json:"price"` @@ -9,7 +9,7 @@ type Package struct { } type Group struct { - Id int `json:"id"` + Id uint `json:"id"` Name string `json:"name"` BookedPackage int `json:"bookedPackage"` BillingAddress string `json:"billingAddress"` @@ -19,7 +19,7 @@ type Group struct { } type User struct { - Id int `json:"id"` + Id uint `json:"id"` Password string `json:"password"` IsAdmin bool `json:"isAdmin"` Username string `json:"username"` @@ -30,8 +30,19 @@ type User struct { } type Account struct { - Id int `json:"id"` + Id uint `json:"id"` GroupId int `json:"groupId"` Created string `json:"createDate"` IsActive bool `json:"isActive"` } + +type Tunnel struct { + Id uint64 `json:"id"` + Name string `json:"name"` + Port int `json:"port"` + IsActive bool `json:"isActive"` + Created string `json:"created"` + RoutedName string `json:"routedName"` + RoutedPort int `json:"routedPort"` + AccountId uint `json:"accountId"` +} diff --git a/spargcom/gocart/rester/rest.go b/spargcom/gocart/rester/rest.go index 78915a0..3610b90 100644 --- a/spargcom/gocart/rester/rest.go +++ b/spargcom/gocart/rester/rest.go @@ -19,6 +19,10 @@ func main() { 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") @@ -107,3 +111,53 @@ func createUserHandler(writer http.ResponseWriter, req *http.Request) { writer.WriteHeader(http.StatusCreated) writer.Write(b) } + +func createAccountHandler(writer http.ResponseWriter, req *http.Request) { + body, err := ioutil.ReadAll(req.Body) + if err != nil { + writer.WriteHeader(http.StatusBadRequest) + } + var us domain.User + json.Unmarshal(body, &us) + res, err := database.CreateAccount(us) + + 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) +} + +func createTunnelHandler(writer http.ResponseWriter, req *http.Request) { + body, err := ioutil.ReadAll(req.Body) + if err != nil { + writer.WriteHeader(http.StatusBadRequest) + } + var tun domain.Tunnel + json.Unmarshal(body, &tun) + res, err := database.CreateTunnel(tun) + + 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) +}