A small project that collects various nice things to get started with Go Web Development.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-web-starter-kit/data/models.go

57 lines
1.4 KiB

package data
import (
"reflect"
"github.com/guregu/null/v6"
)
type Login struct {
Username string `db:"username" validate:"required,max=30"`
Password string `db:"password" validate:"required,max=128"`
}
type User struct {
Id int `db:"id" validate:"numeric"`
Username string `db:"username" validate:"required,max=30"`
Email string `db:"email" validate:"required,email,max=128"`
Password string `db:"password" validate:"required,min=8,max=64"`
}
/*
* Example of using the null library to do optional fields.
*/
type NullExample struct {
Id int `db:"id" validate:"numeric"`
HasMaybe null.Int `db:"replying_to" validate:"omitempty,numeric"`
}
type Product struct {
Id int `db:"id" validate:"numeric"`
Title string `db:"title" validate:"required"`
Description string `db:"description" validate:"required"`
Price float64 `db:"price" validate:"required"`
Slug string `db:"slug" validate:"required"`
}
type CartItem struct {
Id int `db:"id" validate:"numeric"`
UserId int `db:"user_id" validate:"numeric,required"`
ProductId int `db:"product_id" validate:"required"`
Quantity int `db:"quantity" validate:"numeric"`
}
type Cart struct {
Items []CartItem
}
func Models() map[string]reflect.Type {
return map[string]reflect.Type{
"user": reflect.TypeFor[User](),
"product": reflect.TypeFor[Product](),
"cart_item": reflect.TypeFor[CartItem](),
"null_example": reflect.TypeFor[NullExample](),
}
}