Skip to content

Commit

Permalink
Working insert of jsonb groups
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Jan 26, 2021
1 parent ad37de8 commit adb6c0b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 35 deletions.
35 changes: 35 additions & 0 deletions BE/cmd/api/handlers/creategroup/creategroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package creategroup

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

"github.com/simplesheet/cmd/api/models"
"github.com/simplesheet/pkg/application"
"github.com/simplesheet/pkg/middleware"
"github.com/julienschmidt/httprouter"
)

func createGroup(app *application.Application) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

group := &models.Group{}
json.NewDecoder(r.Body).Decode(group)

if err := group.Create(r.Context(), app); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Oops")
return
}

w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(group)
w.Write(response)
}
}

func Do(app *application.Application) httprouter.Handle {
return middleware.Chain(createGroup(app), middleware.LogRequest)
}
86 changes: 86 additions & 0 deletions BE/cmd/api/models/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package models;

import (
"database/sql/driver"
"encoding/json"
"context"
"errors"
"github.com/simplesheet/pkg/application"
)

type Position struct {
Field string `json:"field"`
Value string `json:"value"`
}

type PositionSlice []Position

type Group struct {
ID int `json:"id"`
SheetId int `json:"sheet_id"`
Name string `json:"name"`
Positions PositionSlice `json:"positions"`
}

func (a PositionSlice) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (s *PositionSlice) Scan(src interface{}) error {
switch v := src.(type) {
case []byte:
return json.Unmarshal(v, s)
case string:
return json.Unmarshal([]byte(v), s)
}
return errors.New("type assertion failed")
}

func (g *Group) Create(ctx context.Context, app *application.Application) error {

stmt := `INSERT INTO groups
(
sheets_id,
name,
positions
)
VALUES ($1, $2, $3)
RETURNING id`

err := app.DB.Client.QueryRowContext(
ctx,
stmt,
g.SheetId,
g.Name,
g.Positions,
).Scan(&g.ID)

if err != nil {
return err
}

return nil
}

func (g *Group) GetByID(ctx context.Context, app *application.Application) error {
stmt := `
SELECT *
FROM groups
WHERE id = $1
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
g.ID,
).Scan(
&g.ID,
g.SheetId,
g.Positions,
)

if err != nil {
return err
}

return nil
}
15 changes: 4 additions & 11 deletions BE/cmd/api/models/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ import (
"github.com/simplesheet/pkg/application"
)

type Position struct {
Field string
Value string
}

type Sheet struct {
ID int `json:"id"`
HasMetals bool `json:"has_metals"`
HasCrypto bool `json:"has_crypto"`
MetalsFields []Position `json:"metals_fields"`
CryptoFields []Position `json:"crypto_fields"`
}

func (s *Sheet) Create(ctx context.Context, app *application.Application) error {
stmt := `
stmt_sheet := `
INSERT INTO sheets (
has_metals,
has_crypto
Expand All @@ -29,7 +22,7 @@ func (s *Sheet) Create(ctx context.Context, app *application.Application) error
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
stmt_sheet,
s.HasMetals,
s.HasCrypto,
).Scan(&s.ID)
Expand All @@ -53,8 +46,8 @@ func (s *Sheet) GetByID(ctx context.Context, app *application.Application) error
s.ID,
).Scan(
&s.ID,
s.HasMetals,
s.HasCrypto,
&s.HasMetals,
&s.HasCrypto,
)

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/simplesheet/cmd/api/handlers/createuser"
"github.com/simplesheet/cmd/api/handlers/getuser"
"github.com/simplesheet/cmd/api/handlers/createsheet"
"github.com/simplesheet/cmd/api/handlers/creategroup"
"github.com/simplesheet/pkg/application"
"github.com/julienschmidt/httprouter"
)
Expand All @@ -13,5 +14,6 @@ func Get(app *application.Application) *httprouter.Router {
mux.GET("/users/:id", getuser.Do(app))
mux.POST("/users", createuser.Do(app))
mux.POST("/sheets", createsheet.Do(app))
mux.POST("/groups", creategroup.Do(app))
return mux
}
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
DROP TABLE public.sheets;
DROP TABLE public.metals;
DROP TABLE public.crypto;
22 changes: 0 additions & 22 deletions BE/db/migrations/20210120114402_create_sheet_metals_crypt.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,3 @@ CREATE TABLE IF NOT EXISTS public.sheets
has_metals BOOLEAN NOT NULL,
has_crypto BOOLEAN NOT NULL
);

CREATE TABLE IF NOT EXISTS public.metals
(
sheets_id SERIAL,
id SERIAL PRIMARY KEY,
field VARCHAR(100) NOT NULL,
value VARCHAR(100) NOT NULL,
CONSTRAINT fk_sheets
FOREIGN KEY(sheets_id)
REFERENCES sheets(id)
);

CREATE TABLE IF NOT EXISTS public.crypto
(
sheets_id SERIAL,
id SERIAL PRIMARY KEY,
field VARCHAR(100) NOT NULL,
value VARCHAR(100) NOT NULL,
CONSTRAINT fk_sheets
FOREIGN KEY(sheets_id)
REFERENCES sheets(id)
);
1 change: 1 addition & 0 deletions BE/db/migrations/20210126103639_create_groups.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE public.groups;
10 changes: 10 additions & 0 deletions BE/db/migrations/20210126103639_create_groups.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS public.groups
(
sheets_id INT,
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
positions JSONB NOT NULL,
CONSTRAINT fk_sheets
FOREIGN KEY(sheets_id)
REFERENCES sheets(id)
);

0 comments on commit adb6c0b

Please sign in to comment.