-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/Kansuler/octobe/example | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429 // indirect | ||
github.com/go-chi/chi/v5 v5.0.11 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/Kansuler/octobe/v2 v2.0.0-20231222123441-5a4840494eaf h1:CLY87hdUnWdSttmalq8rPmLx0V6p5ZVGsy/nczx70Hg= | ||
github.com/Kansuler/octobe/v2 v2.0.0-20231222123441-5a4840494eaf/go.mod h1:eJtv4nmpMFBDun61Tmk/cYsCgCIqpRhbuCCzGL0P3Ig= | ||
github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429 h1:Ib6vxuOKU98tHCBWN8GNp8+nFAkT9v61ogGsew3cu2Y= | ||
github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429/go.mod h1:Jjj4AriFqMIDTlLZc/tSNu7mhXV5I0jEC/YT11WkWNc= | ||
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= | ||
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/Kansuler/octobe/example/query" | ||
"github.com/Kansuler/octobe/v2" | ||
"github.com/Kansuler/octobe/v2/driver/postgres" | ||
"github.com/go-chi/chi/v5" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type Container struct { | ||
Postgres postgres.Driver | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
cnt := Container{} | ||
var err error | ||
dsn := os.Getenv("DSN") | ||
if dsn == "" { | ||
panic("DSN is not set") | ||
} | ||
|
||
cnt.Postgres, err = octobe.New(postgres.Open(ctx, dsn)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
session, err := cnt.Postgres.Begin(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = postgres.Execute(session, query.Migration()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = router(cnt) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func router(cnt Container) error { | ||
r := chi.NewRouter() | ||
|
||
// Postgres | ||
r.Get("/postgres/product/{name}", func(w http.ResponseWriter, r *http.Request) { | ||
name := chi.URLParam(r, "name") | ||
session, err := cnt.Postgres.Begin(r.Context()) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
product, err := postgres.Execute(session, query.ProductByName(name)) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
_, err = w.Write([]byte(product.Name)) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
}) | ||
r.Post("/postgres/product/{name}", func(w http.ResponseWriter, r *http.Request) { | ||
name := chi.URLParam(r, "name") | ||
session, err := cnt.Postgres.Begin(r.Context(), postgres.WithTransaction(postgres.TxOptions{})) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
defer session.WatchRollback(func() error { | ||
return err | ||
}) | ||
|
||
product, err := postgres.Execute(session, query.AddProduct(name)) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
err = session.Commit() | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
_, err = w.Write([]byte(product.Name)) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
}) | ||
|
||
return http.ListenAndServe(":8080", r) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package query | ||
|
||
import ( | ||
"github.com/Kansuler/octobe/v2" | ||
"github.com/Kansuler/octobe/v2/driver/postgres" | ||
) | ||
|
||
func Migration() postgres.Handler[octobe.Void] { | ||
return func(builder postgres.Builder) (octobe.Void, error) { | ||
query := builder(` | ||
CREATE TABLE IF NOT EXISTS products ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT NOT NULL | ||
); | ||
`) | ||
_, err := query.Exec() | ||
return nil, err | ||
} | ||
} | ||
|
||
type Product struct { | ||
ID int | ||
Name string | ||
} | ||
|
||
func AddProduct(name string) postgres.Handler[Product] { | ||
return func(builder postgres.Builder) (Product, error) { | ||
var product Product | ||
query := builder(` | ||
INSERT INTO products (name) VALUES ($1) RETURNING id, name; | ||
`) | ||
|
||
query.Arguments(name) | ||
err := query.QueryRow(&product.ID, &product.Name) | ||
return product, err | ||
} | ||
} | ||
|
||
func ProductByName(name string) postgres.Handler[Product] { | ||
return func(builder postgres.Builder) (Product, error) { | ||
var product Product | ||
query := builder(` | ||
SELECT id, name FROM products WHERE name = $1; | ||
`) | ||
|
||
query.Arguments(name) | ||
err := query.QueryRow(&product.ID, &product.Name) | ||
return product, err | ||
} | ||
} |