rrest

package
v0.0.0-...-bac8e5f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2021 License: GPL-3.0 Imports: 11 Imported by: 0

README

package course

import (
	"fmt"
	"net/http"
	"roo.bo/appcourse/api/rres"
	"roo.bo/rlib/base"
)

type Course struct {
	ID             int    `db:"id" json:"id"`
	AppID          string `db:"appid" json:"appid"`
	Name           string `db:"name" json:"name" validate:"required"`
	Intro          string `db:"intro" json:"intro"`
	Icon           string `db:"icon" json:"icon" validate:"required"`
	LastModifiedBy string `db:"last_modified_by" json:"lastModifiedBy"`
	IsDeleted      int    `db:"is_deleted" json:"isDeleted"`
	CreatedAt      int    `db:"created_at" json:"createdAt"`
	UpdatedAt      int    `db:"updated_at" json:"updatedAt"`
	CopyFrom       int    `db:"copy_from" json:"copyFrom"`
	BatchNo        string `db:"batchNo" json:"batchNo"`
	Language       string `db:"language" json:"language"`
	IsPublic       int    `db:"is_public" json:"isPublic"`
}

func (*Course) PK() string {
	return "id"
}

func (*Course) TableName() string {
	return "course"
}

func List(w http.ResponseWriter, r *http.Request) {
	rres.List{
		Filters: []string{
			"appid__eq",
		},
		Force: map[string]string{
			"appid": "current appid",
		},
		Model: &[]Course{},
		R:     r,
		W:     w,
	}.Parse()
}

func Create(w http.ResponseWriter, r *http.Request) {
	rres.Create{
		Force: map[string]string{
			"last_modified_by": "current username",
		},
		Validate: map[string]rres.Validator{
			"language": func(k string, form *base.Struct) (string, bool) {
				v, _ := form.GetString(k)
				if v != "en" && v != "cn" {
					return "language only be en or cn", false
				}
				return "", true
			},
		},
		Model: &Course{},
		R:     r,
		W:     w,
	}.Parse()
}

func Detail(w http.ResponseWriter, r *http.Request) {
	rres.Detail{
		Model: &Course{},
		R:     r,
		W:     w,
	}.Parse()
}

func Delete(w http.ResponseWriter, r *http.Request) {
	rres.Delete{
		Force: map[string]string{
			"appid": "could only delete current appid",
		},
		Model: &Course{},
		R:     r,
		W:     w,
	}.Parse()
}

func Update(w http.ResponseWriter, r *http.Request) {
	rres.Update{
		Force: map[string]string{
			"last_modified_by": "current username",
		},
		Validate: map[string]rres.Validator{
			"language": func(k string, form *base.Struct) (string, bool) {
				v, _ := form.GetString(k)
				fmt.Printf("%+v", v)
				if v != "en" && v != "cn" {
					return "language only be en or cn", false
				}
				return "", true
			},
		},
		Model: &Course{},
		R:     r,
		W:     w,
	}.Parse()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	Handle(w http.ResponseWriter, r *http.Request)
}

type Create

type Create struct {
	One
}

func (*Create) Handle

func (one *Create) Handle(w http.ResponseWriter, r *http.Request)

type Data

type Data struct {
	Items      interface{} `json:"items"`
	Pagination *Pagination `json:"pagination"`
}

type Delete

type Delete struct {
	One
}

func (Delete) Handle

func (one Delete) Handle(w http.ResponseWriter, r *http.Request)

type Detail

type Detail struct {
	One
}

func (*Detail) Handle

func (one *Detail) Handle(w http.ResponseWriter, r *http.Request)

type FilterOp

type FilterOp struct {
	// contains filtered or unexported fields
}

func NewFilterOp

func NewFilterOp(filter string) *FilterOp

func (*FilterOp) Query

func (f *FilterOp) Query(sql *rsql.Builder, value interface{})

type List

type List struct {
	Action
	Model       rsql.IModel
	Filters     []string
	BeforeQuery QueryBuilder
}

func (*List) Handle

func (l *List) Handle(w http.ResponseWriter, r *http.Request)

type One

type One struct {
	Model         interface{}
	ValidateModel ValidateModelFunc
}

type Pagination

type Pagination struct {
	Page     int `json:"page"`
	PageSize int `json:"pageSize"`
	Total    int `json:"total"`
}

func ReqPagination

func ReqPagination(r *http.Request) *Pagination

type QueryBuilder

type QueryBuilder func(r *http.Request, sql *rsql.Builder) error

type Update

type Update struct {
	One
}

func (Update) Handle

func (one Update) Handle(w http.ResponseWriter, r *http.Request)

type ValidateModelFunc

type ValidateModelFunc func(r *http.Request, model rsql.IModel) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL