-
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
6 changed files
with
161 additions
and
3 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,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...) | ||
} |
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,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) | ||
} | ||
} |
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,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...) | ||
} |
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,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) | ||
} | ||
} |
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