diff --git a/BE/cmd/api/handlers/creategroup/creategroup.go b/BE/cmd/api/handlers/creategroup/creategroup.go new file mode 100644 index 0000000..39b5e0a --- /dev/null +++ b/BE/cmd/api/handlers/creategroup/creategroup.go @@ -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) +} diff --git a/BE/cmd/api/models/group.go b/BE/cmd/api/models/group.go new file mode 100644 index 0000000..e327cb4 --- /dev/null +++ b/BE/cmd/api/models/group.go @@ -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 +} diff --git a/BE/cmd/api/models/sheet.go b/BE/cmd/api/models/sheet.go index 25f0246..8a837e1 100644 --- a/BE/cmd/api/models/sheet.go +++ b/BE/cmd/api/models/sheet.go @@ -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 @@ -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) @@ -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 { diff --git a/BE/cmd/api/router/router.go b/BE/cmd/api/router/router.go index 3a00b62..a22321a 100644 --- a/BE/cmd/api/router/router.go +++ b/BE/cmd/api/router/router.go @@ -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" ) @@ -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 } diff --git a/BE/db/migrations/20210120114402_create_sheet_metals_crypt.down.sql b/BE/db/migrations/20210120114402_create_sheet_metals_crypt.down.sql index de0d024..61bd7d7 100644 --- a/BE/db/migrations/20210120114402_create_sheet_metals_crypt.down.sql +++ b/BE/db/migrations/20210120114402_create_sheet_metals_crypt.down.sql @@ -1,3 +1 @@ DROP TABLE public.sheets; -DROP TABLE public.metals; -DROP TABLE public.crypto; diff --git a/BE/db/migrations/20210120114402_create_sheet_metals_crypt.up.sql b/BE/db/migrations/20210120114402_create_sheet_metals_crypt.up.sql index 020e08f..8ac997b 100644 --- a/BE/db/migrations/20210120114402_create_sheet_metals_crypt.up.sql +++ b/BE/db/migrations/20210120114402_create_sheet_metals_crypt.up.sql @@ -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) -); diff --git a/BE/db/migrations/20210126103639_create_groups.down.sql b/BE/db/migrations/20210126103639_create_groups.down.sql new file mode 100644 index 0000000..3afaeaf --- /dev/null +++ b/BE/db/migrations/20210126103639_create_groups.down.sql @@ -0,0 +1 @@ +DROP TABLE public.groups; diff --git a/BE/db/migrations/20210126103639_create_groups.up.sql b/BE/db/migrations/20210126103639_create_groups.up.sql new file mode 100644 index 0000000..5624481 --- /dev/null +++ b/BE/db/migrations/20210126103639_create_groups.up.sql @@ -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) +);