mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
89
_examples/database/mysql/entity/category.go
Normal file
89
_examples/database/mysql/entity/category.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Category represents the categories entity.
|
||||
// Each product belongs to a category, see `Product.CategoryID` field.
|
||||
// It implements the `sql.Record` and `sql.Sorted` interfaces.
|
||||
type Category struct {
|
||||
ID int64 `db:"id" json:"id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Position uint64 `db:"position" json:"position"`
|
||||
ImageURL string `db:"image_url" json:"image_url"`
|
||||
|
||||
// We could use: sql.NullTime or unix time seconds (as int64),
|
||||
// note that the dsn parameter "parseTime=true" is required now in order to fill this field correctly.
|
||||
CreatedAt *time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName returns the database table name of a Category.
|
||||
func (c *Category) TableName() string {
|
||||
return "categories"
|
||||
}
|
||||
|
||||
// PrimaryKey returns the primary key of a Category.
|
||||
func (c *Category) PrimaryKey() string {
|
||||
return "id"
|
||||
}
|
||||
|
||||
// SortBy returns the column name that
|
||||
// should be used as a fallback for sorting a set of Category.
|
||||
func (c *Category) SortBy() string {
|
||||
return "position"
|
||||
}
|
||||
|
||||
// Scan binds mysql rows to this Category.
|
||||
func (c *Category) Scan(rows *sql.Rows) error {
|
||||
c.CreatedAt = new(time.Time)
|
||||
c.UpdatedAt = new(time.Time)
|
||||
return rows.Scan(&c.ID, &c.Title, &c.Position, &c.ImageURL, &c.CreatedAt, &c.UpdatedAt)
|
||||
}
|
||||
|
||||
// Categories a list of categories. Implements the `Scannable` interface.
|
||||
type Categories []*Category
|
||||
|
||||
// Scan binds mysql rows to this Categories.
|
||||
func (cs *Categories) Scan(rows *sql.Rows) (err error) {
|
||||
cp := *cs
|
||||
for rows.Next() {
|
||||
c := new(Category)
|
||||
if err = c.Scan(rows); err != nil {
|
||||
return
|
||||
}
|
||||
cp = append(cp, c)
|
||||
}
|
||||
|
||||
if len(cp) == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
*cs = cp
|
||||
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
/*
|
||||
// The requests.
|
||||
type (
|
||||
CreateCategoryRequest struct {
|
||||
Title string `json:"title"` // all required.
|
||||
Position uint64 `json:"position"`
|
||||
ImageURL string `json:"imageURL"`
|
||||
}
|
||||
|
||||
UpdateCategoryRequest CreateCategoryRequest // at least 1 required.
|
||||
|
||||
GetCategoryRequest struct {
|
||||
ID int64 `json:"id"` // required.
|
||||
}
|
||||
|
||||
DeleteCategoryRequest GetCategoryRequest
|
||||
|
||||
GetCategoriesRequest struct {
|
||||
// [limit, offset...]
|
||||
}
|
||||
)*/
|
||||
95
_examples/database/mysql/entity/product.go
Normal file
95
_examples/database/mysql/entity/product.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Product represents the products entity.
|
||||
// It implements the `sql.Record` and `sql.Sorted` interfaces.
|
||||
type Product struct {
|
||||
ID int64 `db:"id" json:"id"`
|
||||
CategoryID int64 `db:"category_id" json:"category_id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
ImageURL string `db:"image_url" json:"image_url"`
|
||||
Price float32 `db:"price" json:"price"`
|
||||
Description string `db:"description" json:"description"`
|
||||
CreatedAt *time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName returns the database table name of a Product.
|
||||
func (p Product) TableName() string {
|
||||
return "products"
|
||||
}
|
||||
|
||||
// PrimaryKey returns the primary key of a Product.
|
||||
func (p *Product) PrimaryKey() string {
|
||||
return "id"
|
||||
}
|
||||
|
||||
// SortBy returns the column name that
|
||||
// should be used as a fallback for sorting a set of Product.
|
||||
func (p *Product) SortBy() string {
|
||||
return "updated_at"
|
||||
}
|
||||
|
||||
// ValidateInsert simple check for empty fields that should be required.
|
||||
func (p *Product) ValidateInsert() bool {
|
||||
return p.CategoryID > 0 && p.Title != "" && p.ImageURL != "" && p.Price > 0 /* decimal* */ && p.Description != ""
|
||||
}
|
||||
|
||||
// Scan binds mysql rows to this Product.
|
||||
func (p *Product) Scan(rows *sql.Rows) error {
|
||||
p.CreatedAt = new(time.Time)
|
||||
p.UpdatedAt = new(time.Time)
|
||||
return rows.Scan(&p.ID, &p.CategoryID, &p.Title, &p.ImageURL, &p.Price, &p.Description, &p.CreatedAt, &p.UpdatedAt)
|
||||
}
|
||||
|
||||
// Products is a list of products. Implements the `Scannable` interface.
|
||||
type Products []*Product
|
||||
|
||||
// Scan binds mysql rows to this Categories.
|
||||
func (ps *Products) Scan(rows *sql.Rows) (err error) {
|
||||
cp := *ps
|
||||
for rows.Next() {
|
||||
p := new(Product)
|
||||
if err = p.Scan(rows); err != nil {
|
||||
return
|
||||
}
|
||||
cp = append(cp, p)
|
||||
}
|
||||
|
||||
if len(cp) == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
*ps = cp
|
||||
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
/*
|
||||
// The requests.
|
||||
type (
|
||||
CreateProductRequest struct { // all required.
|
||||
CategoryID int64 `json:"categoryID"`
|
||||
Title string `json:"title"`
|
||||
ImageURL string `json:"imageURL"`
|
||||
Price float32 `json:"price"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
UpdateProductRequest CreateProductRequest // at least 1 required.
|
||||
|
||||
GetProductRequest struct {
|
||||
ID int64 `json:"id"` // required.
|
||||
}
|
||||
|
||||
DeleteProductRequest GetProductRequest
|
||||
|
||||
GetProductsRequest struct {
|
||||
// [page, offset...]
|
||||
}
|
||||
)
|
||||
*/
|
||||
Reference in New Issue
Block a user