-
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.
Project initial commit / sheet api handler and model
- Loading branch information
root
authored and
root
committed
Jan 19, 2021
1 parent
bdf6077
commit 11112f4
Showing
32 changed files
with
154 additions
and
56 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 |
---|---|---|
@@ -1 +1 @@ | ||
main | ||
/BE/main |
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
File renamed without changes.
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 createsheet | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/simplesheet/BE/cmd/api/models" | ||
"github.com/simplesheet/BE/pkg/application" | ||
"github.com/simplesheet/BE/pkg/middleware" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
func createSheet(app *application.Application) httprouter.Handle { | ||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
defer r.Body.Close() | ||
|
||
sheet := &models.Sheet{} | ||
json.NewDecoder(r.Body).Decode(sheet) | ||
|
||
if err := sheet.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(sheet) | ||
w.Write(response) | ||
} | ||
} | ||
|
||
func Do(app *application.Application) httprouter.Handle { | ||
return middleware.Chain(createSheet(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
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
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
File renamed without changes.
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,58 @@ | ||
package models; | ||
|
||
import ( | ||
"context" | ||
"github.com/simplesheet/BE/pkg/application" | ||
) | ||
|
||
type Sheet struct { | ||
ID int `json:"id"` | ||
Field string `json:"field"` | ||
Value string `json:"value"` | ||
} | ||
|
||
func (s *Sheet) Create(ctx context.Context, app *application.Application) error { | ||
stmt := ` | ||
INSERT INTO sheets ( | ||
field, | ||
value | ||
) | ||
VALUES ($1, $2) | ||
RETURNING id | ||
` | ||
err := app.DB.Client.QueryRowContext( | ||
ctx, | ||
stmt, | ||
s.Field, | ||
s.Value, | ||
).Scan(&s.ID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Sheet) GetByID(ctx context.Context, app *application.Application) error { | ||
stmt := ` | ||
SELECT * | ||
FROM sheets | ||
WHERE id = $1 | ||
` | ||
err := app.DB.Client.QueryRowContext( | ||
ctx, | ||
stmt, | ||
s.ID, | ||
).Scan( | ||
&s.ID, | ||
s.Field, | ||
s.Value, | ||
) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package router | ||
|
||
import ( | ||
"github.com/simplesheet/BE/cmd/api/handlers/createuser" | ||
"github.com/simplesheet/BE/cmd/api/handlers/getuser" | ||
"github.com/simplesheet/BE/cmd/api/handlers/createsheet" | ||
"github.com/simplesheet/BE/pkg/application" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
func Get(app *application.Application) *httprouter.Router { | ||
mux := httprouter.New() | ||
mux.GET("/users/:id", getuser.Do(app)) | ||
mux.POST("/users", createuser.Do(app)) | ||
mux.POST("/sheets", createsheet.Do(app)) | ||
return mux | ||
} |
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
File renamed without changes.
File renamed without changes.
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.sheets |
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,6 @@ | ||
CREATE TABLE IF NOT EXISTS public.sheets | ||
( | ||
id SERIAL PRIMARY KEY, | ||
field VARCHAR(100) NOT NULL, | ||
value VARCHAR(100) NOT NULL | ||
); |
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
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
pkg/application/application.go → BE/pkg/application/application.go
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,6 +1,3 @@ | ||
### Golang API boilerplate | ||
### Golang API (psql) and React FE simple sheet for "instead of Excell" money management. | ||
|
||
Step by step instructions can be found in: | ||
- [Part 1](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-1-am733yi7) | ||
- [Part 2](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-2-wh2z3y5z) | ||
- [Part 3](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-3-pr453ylt) | ||
Based on go-lang api boilerplate. |
This file was deleted.
Oops, something went wrong.