Skip to content

Commit

Permalink
feat(examples): adding a new http example (#8)
Browse files Browse the repository at this point in the history
adding a new http example
  • Loading branch information
Jacobbrewer1 authored Oct 6, 2024
1 parent da12d2d commit 4207664
Show file tree
Hide file tree
Showing 17 changed files with 3,141 additions and 0 deletions.
File renamed without changes.
82 changes: 82 additions & 0 deletions examples/http/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/Jacobbrewer1/patcher"
"github.com/gorilla/mux"
)

type Person struct {
Name string `db:"name"`
Age int `db:"age"`
Height *float64 `db:"height"`
}

type PersonWhere struct {
ID int `db:"id"`
}

func NewPersonWhere(id int) *PersonWhere {
return &PersonWhere{
ID: id,
}
}

func (p *PersonWhere) Where() (string, []any) {
return "id = ?", []any{p.ID}
}

func main() {
r := mux.NewRouter()

r.HandleFunc("/people/{id}", patch).Methods(http.MethodPatch)

if err := http.ListenAndServe(":8080", r); err != nil {
panic(err)
}
}

func patch(w http.ResponseWriter, r *http.Request) {
personIDStr := mux.Vars(r)["id"]
personID, err := strconv.Atoi(personIDStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// Get the person from the database
person := new(Person)
if err := json.NewDecoder(r.Body).Decode(person); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Create the where condition
condition := NewPersonWhere(personID)

// Generate the SQL
sqlStr, args, err := patcher.GenerateSQL(
person,
patcher.WithTable("people"),
patcher.WithWhere(condition),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

fmt.Println(sqlStr)
fmt.Println(args)

respStr := fmt.Sprintf("SQL:\n%s\n\nArgs:\n%v\n", sqlStr, args)

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
if _, err = w.Write([]byte(respStr)); err != nil {
fmt.Println("error writing response:", err)
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23
toolchain go1.23.1

require (
github.com/gorilla/mux v1.8.1
github.com/jmoiron/sqlx v1.4.0
github.com/stretchr/testify v1.9.0
github.com/vektra/mockery/v2 v2.46.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/gorilla/mux/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/gorilla/mux/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/gorilla/mux/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions vendor/github.com/gorilla/mux/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4207664

Please sign in to comment.