Skip to content

Commit

Permalink
Introduction to sheets model and api method
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Feb 15, 2021
1 parent c2be024 commit 4b6d9b2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
46 changes: 46 additions & 0 deletions BE/cmd/api/handlers/getallsheets/getallsheets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package getallsheets

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"

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

func getAllSheets(app *application.Application) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

allsheets := &models.AllSheets{}

if err := allsheets.GetAll(r.Context(), app); err != nil {
if errors.Is(err, sql.ErrNoRows) {
w.WriteHeader(http.StatusPreconditionFailed)
fmt.Fprintf(w, "sheets does not exist")
return
}

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Oops")
return
}

w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(allsheets)
w.Write(response)
}
}

func Do(app *application.Application) httprouter.Handle {
mdw := []middleware.Middleware{
middleware.LogRequest,
}

return middleware.Chain(getAllSheets(app), mdw...)
}
48 changes: 48 additions & 0 deletions BE/cmd/api/models/sheets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package models

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

type SheetSlice []Sheet

type AllSheets struct {
Sheets SheetSlice `json:"sheets"`
}

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

func (s *SheetSlice) 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 *AllSheets) GetAll(ctx context.Context, app *application.Application) error {
stmt := `
SELECT *
FROM sheets
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
).Scan(
&s.Sheets,
)

if err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/simplesheet/cmd/api/handlers/createsheet"
"github.com/simplesheet/cmd/api/handlers/createuser"
"github.com/simplesheet/cmd/api/handlers/getallsheets"
"github.com/simplesheet/cmd/api/handlers/getsheet"
"github.com/simplesheet/cmd/api/handlers/getuser"
"github.com/simplesheet/pkg/application"
Expand All @@ -18,5 +19,7 @@ func Get(app *application.Application) *httprouter.Router {
mux.GET("/users/:id", getuser.Do(app))
mux.GET("/sheets/:id", getsheet.Do(app))

mux.GET("/sheets", getallsheets.Do(app))

return mux
}

0 comments on commit 4b6d9b2

Please sign in to comment.