Skip to content

Commit

Permalink
Create get for sheet and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Jan 27, 2021
1 parent 7090613 commit f304cef
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 3 deletions.
48 changes: 48 additions & 0 deletions BE/cmd/api/handlers/getgroup/getgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package getgroup

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 getGroup(app *application.Application) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

id := r.Context().Value(models.CtxKey("groupid"))
group := &models.Group{ID: id.(int)}

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

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 {
mdw := []middleware.Middleware{
middleware.LogRequest,
validateRequest,
}

return middleware.Chain(getGroup(app), mdw...)
}
28 changes: 28 additions & 0 deletions BE/cmd/api/handlers/getgroup/validaterequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package getgroup

import (
"context"
"fmt"
"net/http"
"strconv"

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

func validateRequest(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
gid := p.ByName("id")

id, err := strconv.Atoi(gid)
if err != nil {
w.WriteHeader(http.StatusPreconditionFailed)
fmt.Fprintf(w, "malformed id")
return
}

ctx := context.WithValue(r.Context(), models.CtxKey("groupid"), id)
r = r.WithContext(ctx)
next(w, r, p)
}
}
48 changes: 48 additions & 0 deletions BE/cmd/api/handlers/getsheet/getsheet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package getsheet

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 getSheet(app *application.Application) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

id := r.Context().Value(models.CtxKey("sheetid"))
sheet := &models.Sheet{ID: id.(int)}

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

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 {
mdw := []middleware.Middleware{
middleware.LogRequest,
validateRequest,
}

return middleware.Chain(getSheet(app), mdw...)
}
28 changes: 28 additions & 0 deletions BE/cmd/api/handlers/getsheet/validaterequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package getsheet

import (
"context"
"fmt"
"net/http"
"strconv"

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

func validateRequest(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
sid := p.ByName("id")

id, err := strconv.Atoi(sid)
if err != nil {
w.WriteHeader(http.StatusPreconditionFailed)
fmt.Fprintf(w, "malformed id")
return
}

ctx := context.WithValue(r.Context(), models.CtxKey("sheetid"), id)
r = r.WithContext(ctx)
next(w, r, p)
}
}
5 changes: 3 additions & 2 deletions BE/cmd/api/models/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func (g *Group) GetByID(ctx context.Context, app *application.Application) error
g.ID,
).Scan(
&g.ID,
g.SheetId,
g.Positions,
&g.SheetId,
&g.Name,
&g.Positions,
)

if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import (
"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/getgroup"
"github.com/simplesheet/cmd/api/handlers/getsheet"
"github.com/simplesheet/cmd/api/handlers/getuser"
"github.com/simplesheet/pkg/application"
)

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))

mux.GET("/users/:id", getuser.Do(app))
mux.GET("/sheets/:id", getsheet.Do(app))
mux.GET("/groups/:id", getgroup.Do(app))

return mux
}

0 comments on commit f304cef

Please sign in to comment.