-
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.
Introduction to sheets model and api method
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 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,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...) | ||
} |
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 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 | ||
} |
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