Skip to content

Commit

Permalink
Add readme links / reformat go files
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Jan 26, 2021
1 parent adb6c0b commit 7090613
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 50 deletions.
2 changes: 1 addition & 1 deletion BE/cmd/api/handlers/creategroup/creategroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"net/http"

"github.com/julienschmidt/httprouter"
"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 {
Expand Down
28 changes: 14 additions & 14 deletions BE/cmd/api/handlers/createsheet/createsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import (
"fmt"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/simplesheet/cmd/api/models"
"github.com/simplesheet/pkg/application"
"github.com/simplesheet/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()
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

sheet := &models.Sheet{}
json.NewDecoder(r.Body).Decode(sheet)
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
}
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)
}
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(sheet)
w.Write(response)
}
}

func Do(app *application.Application) httprouter.Handle {
Expand Down
2 changes: 1 addition & 1 deletion BE/cmd/api/handlers/createuser/createuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"net/http"

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

func createUser(app *application.Application) httprouter.Handle {
Expand Down
2 changes: 1 addition & 1 deletion BE/cmd/api/handlers/getuser/getuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"
"net/http"

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

func getUser(app *application.Application) httprouter.Handle {
Expand Down
2 changes: 1 addition & 1 deletion BE/cmd/api/handlers/getuser/validarequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"strconv"

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

func validateRequest(next httprouter.Handle) httprouter.Handle {
Expand Down
3 changes: 2 additions & 1 deletion BE/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"github.com/joho/godotenv"
"github.com/simplesheet/cmd/api/router"
"github.com/simplesheet/pkg/application"
"github.com/simplesheet/pkg/exithandler"
"github.com/simplesheet/pkg/logger"
"github.com/simplesheet/pkg/server"
"github.com/joho/godotenv"
)

func main() {
Expand All @@ -15,6 +15,7 @@ func main() {
}

app, err := application.Get()

if err != nil {
logger.Error.Fatal(err.Error())
}
Expand Down
34 changes: 17 additions & 17 deletions BE/cmd/api/models/group.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package models;
package models

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

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

type PositionSlice []Position

type Group struct {
ID int `json:"id"`
SheetId int `json:"sheet_id"`
Name string `json:"name"`
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)
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")
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 {
Expand All @@ -51,7 +51,7 @@ func (g *Group) Create(ctx context.Context, app *application.Application) error
ctx,
stmt,
g.SheetId,
g.Name,
g.Name,
g.Positions,
).Scan(&g.ID)

Expand All @@ -74,8 +74,8 @@ func (g *Group) GetByID(ctx context.Context, app *application.Application) error
g.ID,
).Scan(
&g.ID,
g.SheetId,
g.Positions,
g.SheetId,
g.Positions,
)

if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions BE/cmd/api/models/sheet.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package models;
package models

import (
"context"
"github.com/simplesheet/pkg/application"
)

type Sheet struct {
ID int `json:"id"`
ID int `json:"id"`
HasMetals bool `json:"has_metals"`
HasCrypto bool `json:"has_crypto"`
}

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

if err != nil {
Expand All @@ -46,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: 1 addition & 1 deletion BE/cmd/api/models/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models;
package models

import (
"context"
Expand Down
8 changes: 5 additions & 3 deletions BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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/getuser"
"github.com/simplesheet/cmd/api/handlers/createsheet"
"github.com/simplesheet/cmd/api/handlers/creategroup"
"github.com/simplesheet/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))
mux.POST("/groups", creategroup.Do(app))

return mux
}
2 changes: 1 addition & 1 deletion BE/pkg/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middleware
import (
"net/http"

"github.com/simplesheet/pkg/logger"
"github.com/julienschmidt/httprouter"
"github.com/simplesheet/pkg/logger"
)

func LogRequest(next httprouter.Handle) httprouter.Handle {
Expand Down
2 changes: 1 addition & 1 deletion BE/scripts/entrypoint.dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ echo "#################### downloading CompileDaemon"
GO111MODULE=off go get github.com/githubnemo/CompileDaemon

echo "#################### starting deamon"
CompileDaemon --build="go build -o main cmd/api/main.go" --command=./main
CompileDaemon -build="go build -o main cmd/api/main.go" --command=./main
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### Golang API (psql) and React FE simple sheet for "instead of Excell" money management.

Based on go-lang api boilerplate.
Based on go-lang [api boilerplate](http://github.com/Daniel1984/go-api-boilerplate).

Backage used:

1) BE
- [godotenv](https://github.com/joho/godotenv)
- [CompileDaemon](https://github.com/githubnemo/CompileDaemon)
- [httprouter](https://github.com/julienschmidt/httprouter)

0 comments on commit 7090613

Please sign in to comment.