-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
138 additions
and
35 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,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) | ||
} |
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,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 | ||
} |
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
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
2 changes: 0 additions & 2 deletions
2
BE/db/migrations/20210120114402_create_sheet_metals_crypt.down.sql
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
DROP TABLE public.sheets; | ||
DROP TABLE public.metals; | ||
DROP TABLE public.crypto; |
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
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 @@ | ||
DROP TABLE public.groups; |
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,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) | ||
); |