initial commit
This commit is contained in:
74
category/internal/model/categoriesmodel.go
Normal file
74
category/internal/model/categoriesmodel.go
Normal file
@ -0,0 +1,74 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ CategoriesModel = (*customCategoriesModel)(nil)
|
||||
|
||||
type (
|
||||
// CategoriesModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customCategoriesModel.
|
||||
CategoriesModel interface {
|
||||
categoriesModel
|
||||
WithSession(session sqlx.Session) CategoriesModel
|
||||
Transact(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
|
||||
FindBySystemParentName(ctx context.Context, systemID string, parentID string, name string) (*Categories, error)
|
||||
FindBySystemParentAlias(ctx context.Context, systemID string, parentID string, alias string) (*Categories, error)
|
||||
}
|
||||
|
||||
customCategoriesModel struct {
|
||||
*defaultCategoriesModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewCategoriesModel returns a model for the database table.
|
||||
func NewCategoriesModel(conn sqlx.SqlConn) CategoriesModel {
|
||||
return &customCategoriesModel{
|
||||
defaultCategoriesModel: newCategoriesModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customCategoriesModel) WithSession(session sqlx.Session) CategoriesModel {
|
||||
return NewCategoriesModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
|
||||
func (m *customCategoriesModel) Transact(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
return m.conn.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
}
|
||||
|
||||
// 根据 system_id + parent_id + name 查询
|
||||
func (m *customCategoriesModel) FindBySystemParentName(ctx context.Context, systemID string, parentID string, name string) (*Categories, error) {
|
||||
query := `
|
||||
SELECT * FROM categories
|
||||
WHERE system_id = $1
|
||||
AND COALESCE(parent_id, '') = COALESCE($2, '')
|
||||
AND name = $3
|
||||
`
|
||||
var resp Categories
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, systemID, parentID, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// 根据 system_id + parent_id + alias 查询
|
||||
func (m *customCategoriesModel) FindBySystemParentAlias(ctx context.Context, systemID string, parentID string, alias string) (*Categories, error) {
|
||||
query := `
|
||||
SELECT * FROM categories
|
||||
WHERE system_id = $1
|
||||
AND COALESCE(parent_id, '') = COALESCE($2, '')
|
||||
AND alias = $3
|
||||
`
|
||||
var resp Categories
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, systemID, parentID, alias)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
122
category/internal/model/categoriesmodel_gen.go
Normal file
122
category/internal/model/categoriesmodel_gen.go
Normal file
@ -0,0 +1,122 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.8.3
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
categoriesFieldNames = builder.RawFieldNames(&Categories{}, true)
|
||||
categoriesRows = strings.Join(categoriesFieldNames, ",")
|
||||
categoriesRowsExpectAutoSet = strings.Join(stringx.Remove(categoriesFieldNames, "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at"), ",")
|
||||
categoriesRowsWithPlaceHolder = builder.PostgreSqlJoin(stringx.Remove(categoriesFieldNames, "id", "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at"))
|
||||
)
|
||||
|
||||
type (
|
||||
categoriesModel interface {
|
||||
Insert(ctx context.Context, data *Categories) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id string) (*Categories, error)
|
||||
FindOneBySystemIdParentIdAlias(ctx context.Context, systemId string, parentId sql.NullString, alias sql.NullString) (*Categories, error)
|
||||
FindOneBySystemIdParentIdName(ctx context.Context, systemId string, parentId sql.NullString, name string) (*Categories, error)
|
||||
Update(ctx context.Context, data *Categories) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
defaultCategoriesModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
Categories struct {
|
||||
Id string `db:"id"`
|
||||
SystemId string `db:"system_id"`
|
||||
Name string `db:"name"`
|
||||
Alias sql.NullString `db:"alias"`
|
||||
ParentId sql.NullString `db:"parent_id"`
|
||||
Description sql.NullString `db:"description"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt sql.NullTime `db:"updated_at"`
|
||||
}
|
||||
)
|
||||
|
||||
func newCategoriesModel(conn sqlx.SqlConn) *defaultCategoriesModel {
|
||||
return &defaultCategoriesModel{
|
||||
conn: conn,
|
||||
table: `"public"."categories"`,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) Delete(ctx context.Context, id string) error {
|
||||
query := fmt.Sprintf("delete from %s where id = $1", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) FindOne(ctx context.Context, id string) (*Categories, error) {
|
||||
query := fmt.Sprintf("select %s from %s where id = $1 limit 1", categoriesRows, m.table)
|
||||
var resp Categories
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) FindOneBySystemIdParentIdAlias(ctx context.Context, systemId string, parentId sql.NullString, alias sql.NullString) (*Categories, error) {
|
||||
var resp Categories
|
||||
query := fmt.Sprintf("select %s from %s where system_id = $1 and parent_id = $2 and alias = $3 limit 1", categoriesRows, m.table)
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, systemId, parentId, alias)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) FindOneBySystemIdParentIdName(ctx context.Context, systemId string, parentId sql.NullString, name string) (*Categories, error) {
|
||||
var resp Categories
|
||||
query := fmt.Sprintf("select %s from %s where system_id = $1 and parent_id = $2 and name = $3 limit 1", categoriesRows, m.table)
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, systemId, parentId, name)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) Insert(ctx context.Context, data *Categories) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values ($1, $2, $3, $4, $5, $6)", m.table, categoriesRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Id, data.SystemId, data.Name, data.Alias, data.ParentId, data.Description)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) Update(ctx context.Context, newData *Categories) error {
|
||||
query := fmt.Sprintf("update %s set %s where id = $1", m.table, categoriesRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, newData.Id, newData.SystemId, newData.Name, newData.Alias, newData.ParentId, newData.Description)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCategoriesModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
category/internal/model/vars.go
Normal file
5
category/internal/model/vars.go
Normal file
@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
Reference in New Issue
Block a user