-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategory.go
38 lines (32 loc) · 1.36 KB
/
category.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"errors"
"github.com/resoursea/api"
)
// This Resource classifies the Books resource in categories
type Category struct {
CategoryID string
CategoryName string
CategorySlug string
Books *Books
}
// This resource is represents a list of Categories
// It forces the framework to catch an api.ID whenever a single Category was requested
type Categories []Category
// This error is returned when it can not recover the required category from the database
// It will enter in the pipeline in order be treated by any subsequent method with is requesting for the errors
type CategoryNotFoundError error
// The creator method for the Category resource
// This method will be called whenever the resource is requested
// It inject one Category or one CategoryNotFoundError in the pipeline
func (c *Category) New(id api.ID, db *DB) (*Category, error) {
// api.ID will never be null
// Because all routes that requires the Category resource are children of Category
// So to access them the client should use an route like: /api/category/:CategorySlug/...
// and the framework will inject the :CategorySlug as the api.ID requested by the Category resource
err := db.Get(c, "SELECT * FROM category WHERE categoryslug=?", id.String())
if err != nil {
return nil, CategoryNotFoundError(errors.New("Category '" + id.String() + "' not found!"))
}
return c, nil
}