Skip to content

Commit

Permalink
Simplify to one table / struct
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Feb 13, 2021
1 parent ba492c0 commit 2c7c821
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 250 deletions.
35 changes: 0 additions & 35 deletions BE/cmd/api/handlers/creategroup/creategroup.go

This file was deleted.

48 changes: 0 additions & 48 deletions BE/cmd/api/handlers/getgroup/getgroup.go

This file was deleted.

28 changes: 0 additions & 28 deletions BE/cmd/api/handlers/getgroup/validaterequest.go

This file was deleted.

113 changes: 0 additions & 113 deletions BE/cmd/api/models/group.go

This file was deleted.

42 changes: 33 additions & 9 deletions BE/cmd/api/models/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,53 @@ package models

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

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

type FieldsSlice []Position

type Sheet struct {
ID int `json:"id"`
HasMetals bool `json:"has_metals"`
HasCrypto bool `json:"has_crypto"`
ID int `json:"id"`
Type string `json:"type"`
Fields FieldsSlice `json:"fields"`
}

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

func (s *FieldsSlice) 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 (s *Sheet) Create(ctx context.Context, app *application.Application) error {
stmt := `
INSERT INTO sheets (
has_metals,
has_crypto
type,
fields
)
VALUES ($1, $2)
RETURNING id
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
s.HasMetals,
s.HasCrypto,
s.Type,
s.Fields,
).Scan(&s.ID)

if err != nil {
Expand All @@ -46,8 +70,8 @@ func (s *Sheet) GetByID(ctx context.Context, app *application.Application) error
s.ID,
).Scan(
&s.ID,
&s.HasMetals,
&s.HasCrypto,
&s.Type,
&s.Fields,
)

if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package router

import (
"github.com/julienschmidt/httprouter"
"github.com/simplesheet/cmd/api/handlers/creategroup"
"github.com/simplesheet/cmd/api/handlers/createsheet"
"github.com/simplesheet/cmd/api/handlers/createuser"
"github.com/simplesheet/cmd/api/handlers/getgroup"
"github.com/simplesheet/cmd/api/handlers/getsheet"
"github.com/simplesheet/cmd/api/handlers/getuser"
"github.com/simplesheet/pkg/application"
Expand All @@ -16,11 +14,9 @@ func Get(app *application.Application) *httprouter.Router {

mux.POST("/users", createuser.Do(app))
mux.POST("/sheets", createsheet.Do(app))
mux.POST("/groups", creategroup.Do(app))

mux.GET("/users/:id", getuser.Do(app))
mux.GET("/sheets/:id", getsheet.Do(app))
mux.GET("/groups/:id", getgroup.Do(app))

return mux
}
1 change: 0 additions & 1 deletion BE/db/migrations/20210126103639_create_groups.down.sql

This file was deleted.

10 changes: 0 additions & 10 deletions BE/db/migrations/20210126103639_create_groups.up.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS public.sheets
(
id SERIAL PRIMARY KEY,
has_metals BOOLEAN NOT NULL,
has_crypto BOOLEAN NOT NULL
type VARCHAR(100) NOT NULL,
fields JSONB NOT NULL
);

0 comments on commit 2c7c821

Please sign in to comment.