Skip to content

Commit

Permalink
added proper example
Browse files Browse the repository at this point in the history
  • Loading branch information
Kansuler committed Dec 22, 2023
1 parent 8025817 commit 2074284
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 104 deletions.
8 changes: 8 additions & 0 deletions example/go.mod
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
)
6 changes: 6 additions & 0 deletions example/go.sum
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=
110 changes: 110 additions & 0 deletions example/main.go
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)
}
104 changes: 0 additions & 104 deletions example/postgres/postgres_test.go

This file was deleted.

50 changes: 50 additions & 0 deletions example/query/postgres.go
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
}
}

0 comments on commit 2074284

Please sign in to comment.