Documentation ¶
Index ¶
- Variables
- type MySQLSavedItemsRepo
- type SavedItem
- type SavedItemsRepo
- type SavedItemsService
- func (s *SavedItemsService) Delete(r *http.Request) (int, interface{}, error)
- func (s *SavedItemsService) Get(r *http.Request) (int, interface{}, error)
- func (s *SavedItemsService) JSONEndpoints() map[string]map[string]server.JSONEndpoint
- func (s *SavedItemsService) JSONMiddleware(j server.JSONEndpoint) server.JSONEndpoint
- func (s *SavedItemsService) Middleware(h http.Handler) http.Handler
- func (s *SavedItemsService) Prefix() string
- func (s *SavedItemsService) Put(r *http.Request) (int, interface{}, error)
Constants ¶
This section is empty.
Variables ¶
var ( // technical issues. ServiceUnavailableErr = &jsonErr{"sorry, this service is currently unavailable"} // UnauthErr is a global error returned when the user does not supply the proper // authorization headers. UnauthErr = &jsonErr{"please include a valid USER_ID header in the request"} )
Functions ¶
This section is empty.
Types ¶
type MySQLSavedItemsRepo ¶
type MySQLSavedItemsRepo struct {
// contains filtered or unexported fields
}
MySQLSavedItemsRepo is an implementation of the repo interface built on top of MySQL.
func (*MySQLSavedItemsRepo) Delete ¶
func (r *MySQLSavedItemsRepo) Delete(userID uint64, url string) error
Delete will attempt to remove an item from a user's saved items.
type SavedItem ¶
type SavedItem struct { UserID uint64 `json:"user_id"` URL string `json:"url"` Timestamp time.Time `json:"timestamp"` }
SavedItem represents an article, blog, interactive, etc. that a user wants to save for reading later.
type SavedItemsRepo ¶
type SavedItemsRepo interface { Get(uint64) ([]*SavedItem, error) Put(uint64, string) error Delete(uint64, string) error }
SavedItemsRepo is an interface layer between our service and our database. Abstracting these methods out of a pure implementation helps with testing.
func NewSavedItemsRepo ¶
func NewSavedItemsRepo(cfg *mysql.Config) (SavedItemsRepo, error)
NewSavedItemsRepo will attempt to connect to to MySQL and return a SavedItemsRepo implementation.
type SavedItemsService ¶
type SavedItemsService struct {
// contains filtered or unexported fields
}
SavedItemsService will keep a handle on the saved items repository and implement the gizmo/server.JSONService interface.
func NewSavedItemsService ¶
func NewSavedItemsService(cfg *mysql.Config) (*SavedItemsService, error)
NewSavedItemsService will attempt to instantiate a new repository and service.
func (*SavedItemsService) Delete ¶
func (s *SavedItemsService) Delete(r *http.Request) (int, interface{}, error)
Delete is JSONEndpoint for deleting a saved item from a user's list.
func (*SavedItemsService) Get ¶
func (s *SavedItemsService) Get(r *http.Request) (int, interface{}, error)
Get is a JSONEndpoint to return a list of saved items for the given user ID.
func (*SavedItemsService) JSONEndpoints ¶
func (s *SavedItemsService) JSONEndpoints() map[string]map[string]server.JSONEndpoint
JSONEndpoints is the most important method of the Service implementation. It provides a listing of all endpoints available in the service with their routes and HTTP methods. This method helps satisfy the server.JSONService interface.
func (*SavedItemsService) JSONMiddleware ¶
func (s *SavedItemsService) JSONMiddleware(j server.JSONEndpoint) server.JSONEndpoint
JSONMiddleware provides a hook to add service-wide middleware for how JSONEndpoints should behave. In this example, we’re using the hook to check for a header to identify and authorize the user. This method helps satisfy the server.JSONService interface.
func (*SavedItemsService) Middleware ¶
func (s *SavedItemsService) Middleware(h http.Handler) http.Handler
Middleware provides a hook to add service-wide http.Handler middleware to the service. In this example we are using it to add GZIP compression to our responses. This method helps satisfy the server.Service interface.
func (*SavedItemsService) Prefix ¶
func (s *SavedItemsService) Prefix() string
Prefix is to implement gizmo/server.Service interface. The string will be prefixed to all endpoint routes.